简体   繁体   English

ListView OnItemClickListener并不总是触发

[英]ListView OnItemClickListener is not always firing

It seems like my ListView OnItemClickListener does not always seem to be called. 似乎我的ListView OnItemClickListener似乎并不总是被调用。 The ListView uses embedded TextViews. ListView使用嵌入式TextViews。

If you click at the top or bottom 3px of each item then the listener does fire. 如果单击每个项目的顶部或底部3px,则监听器会触发。

If you click on the text of the TextView then the listener does not fire as though it is consuming/blocking the click. 如果单击TextView的文本,则监听器不会像消耗/阻止单击那样触发。

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. 我已经尝试了将focusable="false"clickable="false"focusableInTouchMode="false"添加到TextView并将android:descendantFocusability="beforeDescendants"到根视图的许多组合。

activity_local_explorer.xml 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. 我将TextView从activity_local_explorer.xml移出到其自己的xml布局文件中,并在创建适配器时使用了其他构造函数。

activity_local_explorer.xml 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 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();
        }    
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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