这个示例日历使用gridview
,现在我想在日历“日”中添加新内容,所以我想我可以在一天中使用listview
布局,现在我有问题是我如何在gridvie适配器中使用listview
!
这是我的日历适配器部件代码
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView)v.findViewById(R.id.date);
listitem=(ListView)v.findViewById(R.id.listView1);
// disable empty days from the beginning
if(days[position].equals("")) {
dayView.setClickable(false);
dayView.setFocusable(false);
}
else {
// mark current day as focused
if(month.get(Calendar.YEAR)== selectedDate.get(Calendar.YEAR) && month.get(Calendar.MONTH)== selectedDate.get(Calendar.MONTH) && days[position].equals(""+selectedDate.get(Calendar.DAY_OF_MONTH))) {
v.setBackgroundResource(R.drawable.item_background_focused);
}
else {
v.setBackgroundResource(R.drawable.list_item_background);
}
}
dayView.setText(days[position]);
for(int t=0;t<date.size();t++){
if(days[position]== date.get(t).get(2)){
listitem.setAdapter(new ArrayAdapter<String>(mContext,
android.R.layout.simple_list_item_1));
}
}
// create date string for comparison
String date = days[position];
if(date.length()==1) {
date = "0"+date;
}
String monthStr = ""+(month.get(Calendar.MONTH)+1);
if(monthStr.length()==1) {
monthStr = "0"+monthStr;
}
// show icon if date is not empty and it exists in the items array
// ImageView iw = (ImageView)v.findViewById(R.id.date_icon);
/* if(date.length()>0 && items!=null && items.contains(date)) {
iw.setVisibility(View.VISIBLE);
}
else {
iw.setVisibility(View.INVISIBLE);
}*/
return v;
}
答案 0 :(得分:0)
嗯,我想这不应该那么难。
您需要使用listview为xml充气,而不是常规的gridview子项。我想是这样的。
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (showListView)
v = vi.inflate(R.layout.listview_item, null);
else
v = vi.inflate(R.layout.calendar_item, null);
比这样的东西
if(showListView){
ListView listview = (ListView) v.findViewById(R.id.listview);
listview.setAdapter(new SuperListviewAdapter());}
else{
//show regular item
}
return v;
我没试过这个,但你可能会遇到触摸事件的一些问题(比如gridview会消耗listview触摸事件吗?)。