OnItemClickListener on a ListView inside a Fragment not working

后端 未结 7 1507
粉色の甜心
粉色の甜心 2020-12-29 09:11

EDIT: SOLVED. If there\'s anything focusable in the XML of the items, it will break the touch of the list, in other words, android:focusable=false to all the checkbo

7条回答
  •  渐次进展
    2020-12-29 09:41

    Two awesome solutions were this, if your extending ListFragment from a fragment, know that mListView.setOnItemClickListener wont be called before your activity is created, As @dheeraj-bhaskar implied. This solution ensured it is set when activity has been created

    @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            mListView.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView adapterView, View view, int position, long rowId) {
                    // Do the onItemClick action
    
                    Log.d("ROWSELECT", "" + rowId);
                }
            });
        }
    

    While looking at the source code for ListFragment, I came across this

        public class ListFragment extends Fragment {
            ...........................................
         ................................................
    
            final private AdapterView.OnItemClickListener mOnClickListener
                    = new AdapterView.OnItemClickListener() 
    {
                public void onItemClick(AdapterView parent, View v, int position, long id) 
    {
                    onListItemClick((ListView)parent, v, position, id);
                }
            };
           .................................................................
        ........................................................................
    
        public void onListItemClick(ListView l, View v, int position, long id) 
    {
            }
    }
    

    An onItemClickListener object is attached and it calls onListItemClick() As such the other similar solution, which works in the exact same way is to override onListItemClick()

     @Override
            public void onListItemClick(ListView l, View v, int position, long rowId) {
                super.onListItemClick(l, v, position, id);
               // Do the onItemClick action
    
                        Log.d("ROWSELECT", "" + rowId);
            } 
    

提交回复
热议问题