带有自定义适配器的ListView,在Fragment内部不会触发onItemClick事件

时间:2013-05-10 09:39:38

标签: android android-listview fragment onitemclicklistener

我有一个用于Fragment内容的ViewProvider类,它返回一个片段的Lis​​tView。 ListView有一个扩展BaseAdapter类的自定义适配器。当我尝试实现项目点击监听器时,它只是不起作用。我有这个代码给听众:

lv.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View vi, int pos,
                long id) {
        try {
            Object o = lv.getItemAtPosition(pos);
            Event obj_itemDetails = (Event) o;
            Toast.makeText(context, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            Log.d("xxx", "output: "+obj_itemDetails.getName());
        } catch(Exception e) {
            Log.d(Log.TAG, "Something is wrong...");
        }
    }
});

......甚至没有例外。在同一个ViewProvider类中,我添加了一个带有SimpleAdapter的列表,它运行得很好。我将列表初始化相同:

EventAdapter sta = new EventAdapter(context, events);
final ListView lv = new ListView( context );
lv.setTextFilterEnabled(true);
lv.setAdapter(sta);

和另一个:

final ListView v = new ListView( context );
String[] from = new String[] { "str" };
int[] to = new int[] { android.R.id.text1 };
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
for ( int i = 0; i < 20; i++ )...//set the items here
SimpleAdapter adapter = new SimpleAdapter( context, items,
        android.R.layout.simple_list_item_1, from, to );
v.setAdapter( adapter );

我错过了什么,我应该在哪里看?

这是我的EventAdapter:

public class EventAdapter extends BaseAdapter {

private LayoutInflater mInflater;

private List<Event> items = new ArrayList<Event>();

Context ctx;

public EventAdapter(Context context, List<Event> items) {
    mInflater = LayoutInflater.from(context);
    this.items = items;
    this.ctx = context;
}

public int getCount() {
    return items.size();
}

public Event getItem(int position) {
    return items.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    final Event s = items.get(position);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.eventTitle);
        holder.timestamp = (TextView) convertView.findViewById(R.id.eventTime);
        holder.location = (TextView) convertView.findViewById(R.id.eventLocation);
        holder.pic = (ImageView) convertView.findViewById(R.id.eventLogo);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(s.getName());
    holder.timestamp.setText(s.getTimestamp());
    holder.location.setText(s.getLocation());
    if (s.getImage() != null) {
        holder.pic.setImageBitmap(s.getImage());
    } else {
        // MY DEFAULT IMAGE
        holder.pic.setImageResource(R.drawable.ic_launcher);
    }
    return convertView;
}

static class ViewHolder {
    TextView name;
    TextView timestamp;
    TextView location;
    ImageView pic;
}

}

2 个答案:

答案 0 :(得分:6)

如果您在自定义列表行中有任何可聚焦项目,则项目点击侦听器上的ListView不会触发。也就是说,如果您有任何按钮或进度条或任何需要关注的控件,onitemclick将不会触发

答案 1 :(得分:1)

我想建议你Activity和Fragment几乎相同,但不同之处在于我们使用“this”或“ActivityName.this”我们必须用getActivity()代替它,并通过使用膨胀我们的视图来声明变量班级布局:

确保您使用像这样的ID查找视图。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
   // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.yourlayoutfile, container,
                    false);

    listView = (ListView) v
                    .findViewById(R.id.your_listview_id);

    listView.setOnItemClickListener(YourFragmentClassName.this);
}

通过实现onItemClickListener,尝试默认使用onItemClick侦听器。

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        Log.d("OnItemClicked>>", "called");
 }

希望它会对你有所帮助。