我正在使用带有列表视图布局的导航抽屉。列表视图中要填充的项目是文本视图、单选按钮和复选框。xml布局文件如下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="Settings"
android:textSize="24dp"
android:gravity="center" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group" >
<RadioButton
android:id="@+id/rbs1"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="Male"/>
<RadioButton
android:id="@+id/rbs2"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="Female"/>
<RadioButton
android:id="@+id/rbs3"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="All"
android:checked="true"/>
</RadioGroup>
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="Experience"
android:textSize="24dp"
android:gravity="center"/>
<CheckBox
android:id="@+id/cbs1"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text="1 yr"/>
<CheckBox
android:id="@+id/cbs2"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text=">1 yr"/>
<CheckBox
android:id="@+id/cbs3"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text=">2 yr"/>
<CheckBox
android:id="@+id/cbs4"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:text=">5 yr"/>
</LinearLayout>
为此,我将适配器设置为
listView.setAdapter(new DrawerAdapter(this,R.layout.draw_list_item));
适配器的代码如下。
package com.example.navdrawer;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class DrawerAdapter extends BaseAdapter implements ListAdapter {
private Context context;
private int drawListItem;
ViewHolder viewHolder;
public DrawerAdapter(Context context, int drawListItem) {
// TODO Auto-generated constructor stub
this.context = context;
this.drawListItem = drawListItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return drawListItem;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return drawListItem;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return drawListItem;
}
static class ViewHolder {
TextView txt1;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
TextView txt2;
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
CheckBox cb4;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
viewHolder = null;
if(convertView == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(drawListItem, parent, false);
viewHolder = new ViewHolder();
viewHolder.txt1 = (TextView) convertView.findViewById(R.id.tv1);
viewHolder.rb1 = (RadioButton) convertView.findViewById(R.id.rbs1);
viewHolder.rb2 = (RadioButton) convertView.findViewById(R.id.rbs2);
viewHolder.rb3 = (RadioButton) convertView.findViewById(R.id.rbs3);
viewHolder.txt2 = (TextView) convertView.findViewById(R.id.tv2);
viewHolder.cb1 = (CheckBox) convertView.findViewById(R.id.cbs1);
viewHolder.cb2 = (CheckBox) convertView.findViewById(R.id.cbs2);
viewHolder.cb3 = (CheckBox) convertView.findViewById(R.id.cbs3);
viewHolder.cb4 = (CheckBox) convertView.findViewById(R.id.cbs4);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
我需要的是我想在导航抽屉的列表视图中显示这些项目。但问题是所有这些项目都在导航抽屉中重复。 请帮我解决。提前谢谢。