自定义ListView setOnItemClickListener无法正常工作

时间:2015-10-17 15:41:28

标签: java android listview

我正在尝试将Custom ListView实现到我的Android应用程序中。但它不起作用。我试图通过添加以下几行来解决这个问题,但它并不适用于我。

android:descendantFocusability="blocksDescendants"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

代码如下 -

my_list.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="match_parent"
    android:orientation="horizontal"
    android:background="#00aee5">

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/laptop"
        android:layout_gravity="center_vertical"
        android:descendantFocusability="blocksDescendants"/>

    <TextView
        android:id="@+id/mytxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Demo"
        android:textColor="#fff"
        android:textSize="18sp"
        android:layout_margin="5dp"
        android:layout_gravity="center_vertical"/>

</LinearLayout>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Laptop"
        android:background="#53e1e9"
        android:textColor="#fff"
        android:textSize="20sp"
        android:padding="10dp"
        android:gravity="center"/>

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="2dp">
    </ListView>

</LinearLayout>

MainActivity.java

package com.android.imran.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView list;

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

        list=(ListView) findViewById(R.id.mylist);

        final String[] my_laptops={"Asus","Acer","Lenovo","HP","DELL","Surface","MacBook"};

        ArrayAdapter<String> my_adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.my_list, R.id.mytxt,my_laptops);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "U clicked on "+my_laptops[position], Toast.LENGTH_LONG).show();
            }
        });

        list.setAdapter(my_adapter);
    }
}

导致问题的原因是什么?我该如何解决?

2 个答案:

答案 0 :(得分:2)

试试这个布局:

<?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="match_parent"
              android:orientation="horizontal"
              android:background="#00aee5"
              android:descendantFocusability="blocksDescendants">

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_gravity="center_vertical"
        />

    <TextView
        android:id="@+id/mytxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Demo"
        android:textColor="#fff"
        android:textSize="18sp"
        android:layout_margin="5dp"
        android:layout_gravity="center_vertical"/>

</LinearLayout>

答案 1 :(得分:1)

如果您在列表项中使用图像按钮,请尝试以下操作:

android:descendantFocusability="blocksDescendants"

mylist 或行活动的主要布局中。

现在,对于 mylist 中的图片按钮,请尝试以下操作:

android:focusableInTouchMode="true"

让我知道,如果它适合你。!