Onclicklistner not working in fragment listview

前端 未结 10 1315
不知归路
不知归路 2020-12-11 06:49

I have a listview with custom adapter in listfragment and also set onclicklistner for listview. But Onclicklistner does not work.

Here is my code:

p         


        
10条回答
  •  孤城傲影
    2020-12-11 07:18

    Put the following code in onViewCreated() method:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    
        ListView list = (ListView) view.findViewById(android.R.id.list);
    
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
                          Toast t = Toast.makeText(getActivity(), "Message",
                                 Toast.LENGTH_SHORT);
                          t.show();
            }
        }); 
    
    }
    

    The problem may lie in the fact that the view is not constructed yet in onViewCreate(), so it's not advisable to set the listeners there.

提交回复
热议问题