简体   繁体   中英

ListView OnItemClickListener is not always firing

It seems like my ListView OnItemClickListener does not always seem to be called. The ListView uses embedded TextViews.

If you click at the top or bottom 3px of each item then the listener does fire.

If you click on the text of the TextView then the listener does not fire as though it is consuming/blocking the click.

I have tried many combinations of adding focusable="false" , clickable="false" and focusableInTouchMode="false" to the TextView and adding android:descendantFocusability="beforeDescendants" to the root view.

activity_local_explorer.xml

<?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"
android:descendantFocusability="beforeDescendants"
tools:context="my.package.FileBrowser">

<ListView
    android:id="@+id/local_file_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:clickable="true" />

<TextView
    android:id="@+id/local_file_view_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textSize="25sp"/>

</android.support.constraint.ConstraintLayout>

Activity

public class LocalExplorer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_explorer);

    String[] values = { "Hello", "My", "Friend"};

    ListView listView = (ListView) findViewById(R.id.local_file_view);
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.activity_local_explorer, R.id.local_file_view_text, values));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            String selectedValue = getItemAtPosition(position);
            Snackbar.make(listView, selectedValue, 2).show();
        }    
    });
}

Any help would be much appreciated as I have been spending hours on this simple activity!

First I'd like to thank those who tried to help me answer my question, your time was much appreciated.

OK I got this working at long last by slightly changing the approach. I moved the TextView out of the activity_local_explorer.xml into its own xml layout file and used a different constructor when creating the adapter.

activity_local_explorer.xml

<?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"
android:descendantFocusability="blocksDescendants"
tools:context="my.package.FileBrowser">

    <ListView
        android:id="@+id/local_file_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.support.constraint.ConstraintLayout>

local_explorer_row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

Activity

public class LocalExplorer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_explorer);

    String[] values = { "Hello", "My", "Friend"};

    ListView listView = (ListView) findViewById(R.id.local_file_view);
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.local_explorer_row, values));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            String selectedValue = getItemAtPosition(position);
            Snackbar.make(listView, selectedValue, 2).show();
        }    
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM