簡體   English   中英

AdapterView.OnItemClickListener() 未觸發

[英]AdapterView.OnItemClickListener() not fired

我的適配器設置如下:

userList = (ListView) activity.findViewById(R.id.userList);
userListAdapter = new UserListAdapter(this, R.layout.user_list, getUserList());
userList.setAdapter(userListAdapter);

函數 getUserList() 返回 ArrayList UserList

就像這樣,列表顯示沒有問題。

但是當我這樣做時:

userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("MyTag","kkkkkkkkkkkkkkkkk");
            }
        });

Log.d 沒有被解雇。 我在控制台了一下是顯示D/AbsListView: onTouchUp() mTouchMode : 1

我的 Xmls activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/userList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="MainActivity"
    tools:showIn="@layout/activity_main">
</ListView>

用戶列表.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:weightSum="60"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/nom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="20"/>

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="20"/>

    <Button
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:hint="Upd"/>

    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:hint="Del"/>
</LinearLayout>

在您的適配器中制作一個用於處理單擊事件的接口,如下面的代碼..

onItemClickListner onItemClickListner;

public void setOnItemClickListner(UserAdapter.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner{
   public void onItemClick();
}

當任何控件用於單擊事件時,例如在適配器中的代碼下方使用的孔布局單擊事件..在適配器 getView() 方法中

 LinearLayout linearLayout=view.findViewById(R.id.linearLayout);
    linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onItemClick();
        }
    });

在列表視圖中綁定適配器之后,然后在下面調用之后......

        userListAdapter.setOnItemClickListner(new UserAdapter.onItemClickListner() {
        @Override
        public void onItemClick() {
            Toast.makeText(getApplicationContext(),"Hello world",Toast.LENGTH_SHORT).show();
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM