OnItemClickListener on a ListView inside a Fragment not working

后端 未结 7 1452
粉色の甜心
粉色の甜心 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:33

    If you want to pass data from fragment to any activity on Listview click then you can modify your code like...

    class HistoryFragment extends Fragment {  ListView historyListView;
    public HistoryFragment() {
        // Required empty public constructor
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        View v= inflater.inflate(R.layout.fragment_history, container, false);
        historyListView= (ListView) v.findViewById(R.id.historyDataListView);
    
        sendRequest();  //it is my meathod to load the listview and set the adapter
        return  v;
    }
    
    public void onStart() {
        super.onStart();
        historyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    
                Intent intent=new Intent(getActivity(), DisplayDetails.class);
    
                intent.putExtra("text", historyListView.getItemAtPosition((int) l).toString());
                startActivity(intent);
              //  Toast.makeText(getActivity(),"Hello.. "+historyListView.getItemAtPosition((int) l).toString(),Toast.LENGTH_LONG).show();
            }
        });
    }}
    
    0 讨论(0)
  • 2020-12-29 09:35

    Just put

    android:focusable="false"
    android:clickable="false"
    

    in layout. For all textviews,buttons etc.

    0 讨论(0)
  • 2020-12-29 09:35

    This may be helpful Answer by raghunanadan in below link solved my problem

    listview OnItemClick listner not work in fragment

    Add this to the layout

    android:descendantFocusability="blocksDescendants"

    0 讨论(0)
  • 2020-12-29 09:36

    Here is an overview of the workflow, create your ListView and it's corresponding Adapter(used to map your underlying data to the items in the ListView), set the adapter to the ListView, and then add an OnItemClickListener to it.

    More details and sample code can be found at: http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

    0 讨论(0)
  • 2020-12-29 09:37

    Here's a code snippet that'll do what you want.

    ListView lv;
    
    //code to get the listView instance using findViewByID etc
    
    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        { 
            Toast.makeText(EnclosingActivity.this, "Stop Clicking me", Toast.LENGTH_SHORT).show();
        }
    });
    

    People usually trip on this, see if you have got this covered:

    All clicks and call backs (eg: the menu/actionbar callbacks) are sent to the activity the fragment is bound to, so they must be in the activity class and not the fragment class.

    0 讨论(0)
  • 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);
            } 
    
    0 讨论(0)
提交回复
热议问题