ListFragment onItemClickListener not working

拟墨画扇 提交于 2019-12-03 05:20:18

If you are using ListFragment then you can simply use its override method onListItemClick()

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
    }

It is due to custom list item. The default focus is with custom list item (button/textview). It causes this issue.

Please add android:descendantFocusability="blocksDescendants" in root layout of list element. Hope this will help someone.

change setListAdapter(adapter); to newsList.setAdapter(adapter);

If you wanted to create or re-use or an existing handler that implements AdapterView.OnItemClickListener, rather than implementing onListItemClick() within the ListFragment, you can do so by getting a reference to the ListView of the ListFragment and setting its listener. In your ListFragment.onResume() you could do:

@Override
public void onResume() {
    super.onResume();
    ListView listView = getListView();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("TAG", "Stop touching me");
        }
    });
}

In my case, I can use the same listener from an Activity with a ListView, a ListActivity or a ListFragment, e.g.

listView.setOnItemClickListener(new MyListViewOnClickListener(getActivity()));

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!