ListFragment OnListItemClick not being called

后端 未结 11 1413
盖世英雄少女心
盖世英雄少女心 2020-11-27 05:53

I have a class that extends ListFragment, and it overrides the OnListItemClick method. I am also doing this in another ListFragment the same way (and the method gets called)

相关标签:
11条回答
  • 2020-11-27 06:14

    I found out that setting the property android:focusable to false on any child view of a list item prevents the click handler from being fired. The following setup works with a ListFragment.

    navigation_fragment.xml:

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/navigationEntries"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
    

    navigation_entry.xml:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/navigationEntry"
                android:orientation="vertical"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                <!-- enhance this -->
                <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:background="@android:color/darker_gray" />
    

    0 讨论(0)
  • 2020-11-27 06:16

    Similar to others, I have a CustomAdapter with my ListFragment, which overrides getView() to provide the View for each row. Setting the listeners on the view which is returned worked for me. Example:

    public class MyAdapter extends ArrayAdapter<SomeType> {
    
    private Context context;
    private List<SomeType> objects;
    
    public MyAdapter(Context context, int resource, List<SomeType> objects) {
        super(context, resource, objects);
        this.context = context;
        this.objects = objects;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_subscription_item, null);
    
        TextView subTo = (TextView)rowView.findViewById(R.id.itemName);
    
        SomeType sub = objects.get(position);
    
        subTo.setText(sub.getName() + sub.getId());
    
        rowView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(context, "from long click", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "from click", Toast.LENGTH_SHORT).show();
            }
        });
        return rowView;
    }
    }
    
    0 讨论(0)
  • 2020-11-27 06:18

    Only workaround so far, use a Fragment and a ListView then use setOnItemClickListener() which would make the ListFragment, and all its' convenience obsolete... After I created a new class containing exactly the same code (only the class name changed) and built the project on a different machine it "magically" worked (Still same API-Level). Below the code that worked. I also tried project->clean which usually fixes most problems, but that did not work eiter.

    public class ContainerFragment extends ListFragment {
    
        private ContainerFragmentListener listener;
    
        public ContainerFragment(ContainerFragmentListener listener) {
            super();
            this.listener = listener;
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ContainerAdapter(getActivity()));
        }
    
         @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Container container = (Container) getListAdapter().getItem(position);
            listener.containerSelected(container.id);
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 06:19

    Here is an elegant solution which allows onListItemClick to fire the way you think it should, and also allows any child views to receive events (or not, depending on how you want to set it.) For the question posed, this would allow for keeping the checkbox subview. Please take care to consider whether this is an intuitive experience for users.

    On the root ViewGroup of all list items, set the descendantFocusability attribute in XML:

    android:descendantFocusability="blocksDescendants"
    

    Android SDK documentation of descendantFocusability. Has been part of the SDK since Android 1.0. Other settings for android:descendantFocusability include "beforeDescendants" and "afterDescendants".

    Example cluster_layout.xml which sets descendantFocusability (List items applied to cluster_list.xml via an ArrayAdapter):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/cluster_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#e0e0e0"
        android:clickable="false"
        android:descendantFocusability="blocksDescendants" >
    
        <FrameLayout
            android:id="@+id/cluster_sentiment_handle"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:background="@color/handle_red" />
        <!-- other elements -->
    </LinearLayout>
    

    Example cluster_list.xml, which has no bearing on this solution other than to show that it's intended for use in a ListFragment by having a ListView element with the id @id/android:list:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="8dp"
        android:background="#f6f6f6" >
        <include layout="@layout/cluster_header" />
        <ListView
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top|center_horizontal"
            android:gravity="center_horizontal"
            android:divider="@android:color/transparent"
            android:dividerHeight="8dp"
            android:fastScrollEnabled="true" />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-27 06:19

    I also encountered the same problem. Using ListFragment which was inflated as a tab content of a tab inside a TabHost.

    One of my TabHost has 3 tabs, the onListItemClick() was called correctly.

    But the other TabHost has only 2 tabs, the onListItemClick() was not called.

    So I decided to implement the work around that Marcel and H9kDroid mentioned above and it worked fine. Thanks for your answer.

    Update: After messing around for a few hours, it seems that the problem related to the layout of the list item and the adapter of the ListView.

    Inside my list item layout, there is one check box and the state of the check box is toggled inside the adapter. I think after the toggling, the ListView messed up and cannot deliver the call to onListItemClick().

    I just changed the layout, remove the check box and it works fine.

    Cheers.

    0 讨论(0)
  • 2020-11-27 06:24

    I had called listfragment.getListView().setOnItemClickListener() and also overridden onListItemClick() on the ListFragment and that resulted in onListItemClick not being called. I deleted the setOnItemClickListener() method and the listFragment's onListItemClick method was called instead.

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