Onclicklistner not working in fragment listview

前端 未结 10 1285
不知归路
不知归路 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:19

    Adapter has method areAllItemsEnabled (), it might be useful.

    public abstract boolean areAllItemsEnabled ()

    Indicates whether all the items in this adapter are enabled. If the value returned by this method changes over time, there is no guarantee it will take effect. If true, it means all items are selectable and clickable (there is no separator.)

    0 讨论(0)
  • 2020-12-11 07:24

    I had a similar issue, it took me a very long time to figure out what was wrong. Previously my fragment's onListItemClick() opened new activity, but after returning to the fragment the onListItemClick() listener did not work anymore.

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // additional code (e.g. open new activity)
    }
    

    This issue was solved by creating a listener inside my custom ArrayAdapter instead. I placed this code inside of getView():

        rowView.setOnClickListener(new View.OnClickListener() {
            final int p = position;
            @Override
            public void onClick(View v) {
                Log.d("FeedListAdapter", "onClick()");
                openPost(p);    // for example
            }
        });
    

    I've stayed up all night fixing this (it's 8am), I thought I should post my solution for anyone else who is in a similar spot :)

    0 讨论(0)
  • 2020-12-11 07:26
    @Override
    
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
    }
    
    0 讨论(0)
  • 2020-12-11 07:27

    finally solved the issue when all the controls(Buttons,textviews) in listview set to focusable false.

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