ListView控件无法点击,在所有行小部件的TextView控件、部件、ListView、TextView

2023-09-06 05:01:39 作者:小熊探险日记

我有一个简单的行模式,在一个片段一个ListView; ListView中被正确填充,所有正确的尺寸,颜色等问题是,点击某一行没有触发事件。

I have a simple row model and a ListView in a Fragment; the ListView gets properly populated, with all the correct dimensions, colors, etc. Problem is, clicking on a row fires no event.

在我的片段code是:

The code in my fragment is:

MyAdapter mAdapter = new MyAdapter(getActivity().getApplicationContext(), R.layout.list_row, strings);
mList.setAdapter(mAdapter);

mList.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d("LIST", "Selected item # " + position);
    }
}); 

片段布局(一块吧)是:

The fragment layout (a piece of it) is:

<ListView
    android:id="@+id/m_list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_margin="20dp"
    android:clickable="true"
    android:longClickable="true">
</ListView>

该行布局是:

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

    <TextView
        android:id="@+id/txt_surname"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="@string/surname"
        style="@style/ListStyleXLarge"/>

    <!-- 4 more TextView just as the first one -->
</LinearLayout>

风格很简单:

    <style name="ListStyleXLarge">
    <item name="android:textSize">
        @dimen/text_size_large
    </item>
    <item name="android:textStyle">
        bold
    </item>
    <item name="android:textColor">
        @color/list_item_text_color
    </item>
</style>

有什么可以解决这个问题?我已经尝试设置

What can fix this problem? I have already tried to set the

android:clickable="false"
android:focuseable="false"
android:focuseableInTouchMode="false"

所有TextViews的,但没有那么远。

to all of the TextViews, but nothing so far.

推荐答案

在您MyAdapter类:

In your MyAdapter class:

在getView方法使用:

In the getView method use:

convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Log.d("LIST", "Selected item # " + position);

            }
        });