OnItemCLickListener not working in listview

前端 未结 24 1932
悲&欢浪女
悲&欢浪女 2020-11-22 11:21

Activity class code:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  Conver         


        
相关标签:
24条回答
  • 2020-11-22 11:44

    Faced same problem, tried for hours. If you have tried all of the above than try changing layout_width of Listview and list item to match_parent from wrap_content.

    0 讨论(0)
  • 2020-11-22 11:45

    I just found a solution from here, but by deep clicking.

    If any row item of list contains focusable or clickable view then OnItemClickListener won't work.

    The row item must have a param like android:descendantFocusability = "blocksDescendants".

    Here you can see an example of how your list item should look like. Your list item xml should be... row_item.xml (your_xml_file.xml)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:descendantFocusability="blocksDescendants"
        android:gravity="center_vertical" >
    
        // your other widgets here
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 11:46

    I solved it with the help of this answer

    1.Add the following in Linear Layout of list_items.xml android:descendantFocusability="blocksDescendants"

    2.Child Views of LinearLayout in list_items.xml

     android:focusable="false" 
    
    0 讨论(0)
  • 2020-11-22 11:47

    if you have textviews, buttons or stg clickable or selectable in your row view only

    android:descendantFocusability="blocksDescendants"
    

    is not enough. You have to set

    android:textIsSelectable="false"
    

    to your textviews and

    android:focusable="false"
    

    to your buttons and other focusable items.

    0 讨论(0)
  • 2020-11-22 11:48

    The thing that worked for me was to add the below code to every subview inside the layout of my row.xml file:

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

    So in my case:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <TextView
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/testingId"
            android:text="Name"
           //other stuff 
            />
    
        <TextView
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/dummyId"
            android:text="icon"
            //other stuff 
            />
    
        <TextView
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/assignmentColor"
           //other stuff 
           />
    
        <TextView
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/testID"
            //other stuff 
            />
    
        <TextView
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="TextView"
            //other stuff 
            />
    
    </android.support.constraint.ConstraintLayout>
    

    And this is my setOnItemClickListener call in my Fragment subclass:

    CustomListView = (PullToRefreshListCustomView) layout.findViewById(getListResourceID());
            CustomListView.setAdapter(customAdapter);
            CustomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d("Testing", "onitem click working");
                  //  other code
                }
    
            });
    

    I got the answer from here!

    0 讨论(0)
  • 2020-11-22 11:49

    you need to do 2 steps in your listview_item.xml

    1. set the root layout with: android:descendantFocusability="blocksDescendants"
    2. set any focusable or clickable view in this item with:
      android:clickable="false"
      android:focusable="false"
      android:focusableInTouchMode="false"

    Here is an example: listview_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants">
    
        <RadioButton
            android:id="@+id/script_name_radio_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="#000"
            android:padding="5dp"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            />
    
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题