OnItemClickListener on a ListView inside a Fragmen

2019-02-03 08:02发布

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 checkboxes, switches or anything like that of ur list. And done =)

Ok so, here's my problem.

I wrote a app that uses tabs and fragments, and it all goes the way I want except for the thing that when I try to capture a onItemClick on a listView it does not even mark the row as touched/pressed/selected.

I've been reading a little bit about and many people have the same issue, but I did not found any responses that helped me at all.

I don't want to implement a ListFragment, in fact I don't even know how/why I should, and since all my code is already working, I don't know if implementing one will give me much more work to do, so, here it is:

Is it possible to implement a listener for a click on a listView, inside a fragment? and if it is, HOW?

PD: minSDK=14, tatgetSDK=15

7条回答
祖国的老花朵
2楼-- · 2019-02-03 08:06

the button will affect the listener, try avoid using button and re compile, it should work

查看更多
Viruses.
3楼-- · 2019-02-03 08:12

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();
        }
    });
}}
查看更多
Fickle 薄情
4楼-- · 2019-02-03 08:15

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"

查看更多
Summer. ? 凉城
5楼-- · 2019-02-03 08:23

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.

查看更多
别忘想泡老子
6楼-- · 2019-02-03 08:25

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);
        } 
查看更多
爷的心禁止访问
7楼-- · 2019-02-03 08:27

Just put

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

in layout. For all textviews,buttons etc.

查看更多
登录 后发表回答