listview OnItemClick listener not work in fragment

后端 未结 4 872
予麋鹿
予麋鹿 2020-12-18 07:51

I used listview in fragment but I used list onItemClick listener not working. My code below and how to perfect solution.

 public class StoreProf         


        
相关标签:
4条回答
  • 2020-12-18 08:33

    Add these properties in your custom xml file

     android:focusableInTouchMode="false"
     android:focusable="false"
    

    for your TextView and ImageButton.

    0 讨论(0)
  • 2020-12-18 08:36

    whenever you list item contains something like button then this type of problem comes. you should use a view holder in you adapter class.

     private class ViewHolder{
               ImageButton imagebutton;
               TextView textView;        
    
             }
    

    use it in your getview method. hope this will work for you.

    0 讨论(0)
  • 2020-12-18 08:38

    Add this

    android:descendantFocusability="blocksDescendants"
    

    to the RelativeLayout in listItem.xml.

    I guess ImageButton takes focus when you click on list row.

    Edit:

    Consider using a ViewHolder pattern

    Reference:

    http://developer.android.com/training/improving-layouts/smooth-scrolling.html

    0 讨论(0)
  • 2020-12-18 08:48

    Here you go: Use onActivityCreated() instead and use the code here:

    @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
    
            ListView lv1 = (ListView)getActivity().findViewById(R.id.myStore_listview);
            adtstore = new MyListAdapter(getActivity().getApplicationContext());
            lv.setAdapter(adtstore);
            lv1.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                        long arg3) {
    
                    Toast.makeText(getActivity(), "Item clicked : " + position, Toast.LENGTH_SHORT).show();
    
                }
    
            });
    
        }
    

    Should work now.. :)

    0 讨论(0)
提交回复
热议问题