Android 单页日历视图
Android Single page calendarview
我正在尝试开发一个日历应用程序,它可以在一个 activity 中显示一年中的所有月份。我在使用它时遇到了麻烦。 This是我想做的观点。我迷路了:/
列表视图适配器:
package com.example.calendar15;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class CalendarListAdapter extends BaseAdapter {
private Context mContext;
String[] months;
public String[] numbers;
// Constructor
public CalendarListAdapter(Context c,String[] months) {
mContext = c;
this.months=months;
}
public int getCount() {
return months.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid, parent, false);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
Log.d("months[position]",""+months[position]);
tvTitle.setText(months[position]);
/**/
String[] numbers = { "1", "2", "3", "4", "5", "6", "7","8","9","10","11","12"};
/*TextView tvDate1 = (TextView) convertView.findViewById(R.id.text1);
Log.d("numbers[position]",""+numbers[position]);
tvDate1.setText(numbers[position]);*/
GridView gridview = (GridView)convertView.findViewById(R.id.gridview);
CalendarGridAdapter calendarGridAdapter = new CalendarGridAdapter(mContext, numbers);
gridview.setAdapter(calendarGridAdapter);
return convertView;
}
}
网格视图适配器:
package com.example.calendar15;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CalendarGridAdapter extends BaseAdapter {
Context mContext;
String[] number;
CalendarGridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.number = numbers;
Log.d("number.length",""+number.length);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return number.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return number[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_item, parent, false);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
Log.d("number[position]",""+number[position]);
tvTitle.setText(number[position]);
return convertView;
}
}
Grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#727272"
android:textSize="16sp"
android:gravity="center"
android:text="date"
android:textStyle="normal" />
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="@android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" />
</GridLayout>
calendar_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Datelayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical"
android:padding="2dip" >
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="date"
android:textColor="#727272"
android:textSize="16sp"
android:textStyle="normal" >
</TextView>
</LinearLayout>
解决方案
我将 Gridview 的高度从 wrap_content 更改为大约 200dp - 要膨胀的元素的高度乘以总行数。 (您可以根据您的要求动态设置或在 xml 中设置)。我希望这对其他人也有帮助
我正在尝试开发一个日历应用程序,它可以在一个 activity 中显示一年中的所有月份。我在使用它时遇到了麻烦。 This是我想做的观点。我迷路了:/
列表视图适配器:
package com.example.calendar15;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class CalendarListAdapter extends BaseAdapter {
private Context mContext;
String[] months;
public String[] numbers;
// Constructor
public CalendarListAdapter(Context c,String[] months) {
mContext = c;
this.months=months;
}
public int getCount() {
return months.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid, parent, false);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
Log.d("months[position]",""+months[position]);
tvTitle.setText(months[position]);
/**/
String[] numbers = { "1", "2", "3", "4", "5", "6", "7","8","9","10","11","12"};
/*TextView tvDate1 = (TextView) convertView.findViewById(R.id.text1);
Log.d("numbers[position]",""+numbers[position]);
tvDate1.setText(numbers[position]);*/
GridView gridview = (GridView)convertView.findViewById(R.id.gridview);
CalendarGridAdapter calendarGridAdapter = new CalendarGridAdapter(mContext, numbers);
gridview.setAdapter(calendarGridAdapter);
return convertView;
}
}
网格视图适配器:
package com.example.calendar15;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CalendarGridAdapter extends BaseAdapter {
Context mContext;
String[] number;
CalendarGridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.number = numbers;
Log.d("number.length",""+number.length);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return number.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return number[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_item, parent, false);
}
// Lookup view for data population
TextView tvTitle = (TextView) convertView.findViewById(R.id.date);
Log.d("number[position]",""+number[position]);
tvTitle.setText(number[position]);
return convertView;
}
}
Grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#727272"
android:textSize="16sp"
android:gravity="center"
android:text="date"
android:textStyle="normal" />
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="@android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" />
</GridLayout>
calendar_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Datelayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical"
android:padding="2dip" >
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="date"
android:textColor="#727272"
android:textSize="16sp"
android:textStyle="normal" >
</TextView>
</LinearLayout>
解决方案
我将 Gridview 的高度从 wrap_content 更改为大约 200dp - 要膨胀的元素的高度乘以总行数。 (您可以根据您的要求动态设置或在 xml 中设置)。我希望这对其他人也有帮助