繁体   English   中英

ListView OnItemClickListener并不总是触发

[英]ListView OnItemClickListener is not always firing

似乎我的ListView OnItemClickListener似乎并不总是被调用。 ListView使用嵌入式TextViews。

如果单击每个项目的顶部或底部3px,则监听器会触发。

如果单击TextView的文本,则监听器不会像消耗/阻止单击那样触发。

我已经尝试了将focusable="false"clickable="false"focusableInTouchMode="false"添加到TextView并将android:descendantFocusability="beforeDescendants"到根视图的许多组合。

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>

活动

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

由于我一直在这个简单的活动上花费数小时,因此将不胜感激!

首先,我要感谢那些试图帮助我回答我的问题的人,感谢您的宝贵时间。

好吧,我终于通过稍微改变方法来完成了这项工作。 我将TextView从activity_local_explorer.xml移出到其自己的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

<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>

活动

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