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