Android自定义ListView项setOnItemClickListener无法正常工作

时间:2015-12-24 08:43:49

标签: android listview

我有一个自定义ListView项目。 setOnClickListener不起作用。我一直在寻找解决方案几个小时,但找不到任何东西。我认为这个问题不是'设定可集中的错误'的事情,因为我已经做到了。求你帮帮我。

payments_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="12dp"
    android:paddingBottom="12dp"
    android:focusable="false"
    android:focusableInTouchMode="false">




<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="12dp"
    android:paddingLeft="10dp">

    <ImageView
        android:layout_width="46dp"
        android:layout_height="35.3dp"
        android:textColor="#000000"
        android:id="@+id/payment_item_name2"
        android:layout_gravity="center_horizontal|start"
        android:src="@drawable/payments_internet"
        android:textSize="20sp"
        android:text="Test"
        android:textStyle="bold"
        android:capitalize="words" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#8c8c8c"
    android:id="@+id/payment_item_name"
    android:layout_gravity="center_horizontal|center"
    android:textSize="20sp"
    android:layout_marginLeft="10dp"
    android:text="Test"
    android:textStyle="bold"
    android:capitalize="words" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="8dp"
    android:layout_marginTop="12dp"
    android:paddingRight="10dp">

    <ImageView
        android:layout_width="15.3dp"
        android:layout_height="20.1dp"
        android:src="@drawable/arrow_right_red"
        android:layout_gravity="center_vertical|right"
        android:id="@+id/backButton"
        android:textAllCaps = "true"
        />
</LinearLayout>

这是我的活动onCreate:

ListView paymentsList = (ListView) findViewById(R.id.paymentsList);                    
paymentsList.setAdapter(testAdapter);
paymentsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         Log.w("Clicked23", "CLicked23");
     }
});

`

我的适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(getContext());
    View payments_list_item = inflater.inflate(R.layout.payments_list_item, parent, false);
    String payment = getItem(position);
    TextView payment_item_name = (TextView) payments_list_item.findViewById(R.id.payment_item_name);
    payment_item_name.setText(payment.toUpperCase());
    return payments_list_item;
}

//修改 终于解决了我的问题 感谢@ mustafasevgi的回答:

我添加了

android:descendantFocusability =“blocksDescendants”到我的自定义ListView项目布局。

5 个答案:

答案 0 :(得分:0)

检查以下代码:

SELECT * FROM (SELECT * FROM tags ORDER by id DESC LIMIT 50 AS result) a WHERE a.result IS NOT NULL;

答案 1 :(得分:0)

试试这个:

   paymentsList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
           Log.w("Clicked23", "CLicked23");
           }
        });

答案 2 :(得分:0)

你能添加这段代码吗?

  

android:descendantFocusability =“blocksDescendants”

<强> payment_list_item.xml

        GetCitiesRequest request = new GetCitiesRequest();
        CityServicePortClient port = new CityServicePortClient();

        GetCitiesResponse getCitiesResponse = port.GetCities(request);

        var list = getCitiesResponse.cities;

答案 3 :(得分:0)

从xml

中删除它
android:focusable="false"
android:focusableInTouchMode="false"

答案 4 :(得分:-1)

如何将payments_list_item修改为此

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="12dp"
    android:paddingTop="12dp">

    <ImageView
        android:id="@+id/payment_item_name2"
        android:layout_width="46dp"
        android:layout_height="35.3dp"
        android:src="@drawable/payments_internet"/>

    <TextView
        android:id="@+id/payment_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="Test"
        android:textColor="#8c8c8c"
        android:textSize="20sp"
        android:textStyle="bold" />

   <ImageView
        android:id="@+id/backButton"
        android:layout_width="15.3dp"
        android:layout_height="20.1dp"
        android:layout_gravity="center_vertical|right"
        android:src="@drawable/arrow_right_red"/>

</LinearLayout>

你写的非常复杂的布局..