【问题标题】:Changing the contents of an external TextView on item click within ListView在 ListView 中单击项目时更改外部 TextView 的内容
【发布时间】:2025-12-06 02:00:02
【问题描述】:

我正在为 ListView 使用自定义适配器,该适配器显示可以单击的项目列表,并且应该更改 ListView 下方的描述 TextView 的文本。描述应显示有关当前突出显示的项目的描述性文本。

在包含 ListView 和 TextView 的 Fragment 中,我使用以下代码注册了 OnItemClickListener

Java 代码:

    mTextView = (TextView) myParentView.findViewById(R.id.fragment_select_option_desc_text_view);

    mListView = (ListView) myParentView.findViewById(R.id.fragment_select_option_list_view);
    mListView.setItemsCanFocus(false);

    mListView.setAdapter(new MyItemAdapter(getActivity(), mItems));
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            int itemId = SelectStateFragment.this.mItems[position].getId();
            String description = DataStore.get().getItemDescription(itemId);

            SelectItemFragment.this.mTextView.setFocusable(true);
            SelectItemFragment.this.mTextView.setText(Html.fromHtml(description)); 

            SelectItemFragment.this.mSelectedPosition = position;

            view.setSelected(true);
        }
    });

片段 xml:

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

    <ListView
        android:id="@+id/fragment_select_option_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/list_view_background" >
    </ListView>

    <ScrollView
        android:id="@+id/fragment_select_option_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:stackFromBottom="true" 
        android:choiceMode="singleChoice"
        android:fillViewport="false" >

        <TextView
            android:id="@+id/fragment_select_option_desc_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3sp" />
    </ScrollView>

</LinearLayout>

项目 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/selectable_item_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:padding="3sp"
        android:textColor="@android:color/black"
        android:focusable="false" 
        android:focusableInTouchMode="false"
        android:background="@drawable/selectable_item_background"/>
</RelativeLayout>

问题是:点击一个项目后,文本视图内容会按预期更新。但是,我在列表视图中看不到所选项目的任何视觉突出显示。只有当我再次单击同一个项目时,我的选择才会在项目突出显示时变得可见。奇怪的是:如果我注释掉这一行

            SelectItemFragment.this.mTextView.setText(Html.fromHtml(description)); 

因此跳过 TextView 的更新,第一次单击项目时选择立即显示。

访问 OnItemClickListener 中的其他 UI 元素有什么问题吗?更新 TextView 后是否必须触发一些布局更新?

更新:在我的代码中插入了 Sweety 的建议,添加了 XML 代码

【问题讨论】:

    标签: android listview onitemclicklistener


    【解决方案1】:

    尝试将列表视图中的所有项目设置为不可聚焦。

    最终 ListView 列表 = (ListView) findViewById(R.id.list); list.setItemsCanFocus(false);

    另外,确保列表项内的项目设置焦点为 false

    android:focusable="假" 安卓:focusableInTouchMode="假"
    还有 view.setFocusable(true) 在 setText 之前。

    【讨论】:

    • 不幸的是,问题仍然存在 :( 我将您的更改合并到我发布的代码中,希望我能正确理解所有内容。
    最近更新 更多