ListView is not clickable, all widgets in rows are

2019-05-26 15:34发布

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.

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>

The row layout is:

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

The style is simply:

    <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"

to all of the TextViews, but nothing so far.

2条回答
闹够了就滚
2楼-- · 2019-05-26 16:02

In your MyAdapter class:

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);

            }
        });
查看更多
女痞
3楼-- · 2019-05-26 16:14

In your list_item xml put android:descendantFocusability="blocksDescendants" in the LinearLayout or RelativeLayout you have.

查看更多
登录 后发表回答