我正在尝试使用BaseAdapter创建ListFragment。但是当我使用自定义适配器时,我的onItemClick事件无法正常工作。我确实得到了我想要的视图,但我的on item click没有使用CustomAdapter触发。
我正在使用以下代码
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//following code do not fire on click event
setListAdapter(new TestListAdapter(getActivity(), test)); //test is a Arraylist
//following code perfectly fine
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[] { "One", "Two", "Three"}));
}
这是我的TestListAdapter
class TestListAdapter extends BaseAdapter {
private LayoutInflater inflater=null;
private ArrayList<Xyz> tests;
private Context mContext;
TestListAdapter(Context context, ArrayList<Xyz> tests){
//super(context, tests);
mContext=context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.tests=tests;
}
public int getCount() {
// TODO Auto-generated method stub
return tests.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return tests.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view=convertView;
if(convertView==null)
view = inflater.inflate(R.layout.my_list_row, null);
TextView tvName = (TextView)view.findViewById(R.id.testName); // title
return view;
}
}
我已经覆盖了以下功能,这就是我知道如何使用ArrayAdapter。
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("XYZ","click.......");
}
答案 0 :(得分:1)
您的列表视图有一个TextView。所以onListItemClick()不会触发。
将TextView(或其他listview子项)的可聚焦可点击属性设置为false。