简体   繁体   English

更改项目上的外部TextView的内容,请在ListView中单击

[英]Changing the contents of an external TextView on item click within ListView

I am using a custom adapter for a ListView that displays a list of items that can be clicked and should change the text of an description TextView below the ListView. 我正在为ListView使用自定义适配器,该适配器显示可以单击的项目列表,并且应该更改ListView下面的描述TextView的文本。 The description should display a descriptive text about the currently highlighted item. 该描述应显示有关当前突出显示的项目的描述性文本。

In my Fragment containing the ListView and the TextView I used the following code to register an OnItemClickListener 在包含ListView和TextView的片段中,我使用以下代码注册了OnItemClickListener

Java Code: 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);
        }
    });

Fragment xml: 片段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>

Item xml: 项目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>

The problem is: after I click on an item, the text view content gets updated as intended. 问题是:单击项目后,文本视图内容将按预期更新。 However, I don't see any visual highlight of the selected item in my list view. 但是,我在列表视图中看不到所选项目的任何视觉亮点。 Only if I click on the same item a second time my selection becomes visible as the item gets highlighted. 仅当我第二次单击同一项目时,当该项目被突出显示时,我的选择才可见。 The strange thing is: If I comment out the line 奇怪的是:如果我注释掉该行

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

and therefore skip the update of the TextView, the selection immediately shows with the first click of an item. 因此,跳过TextView的更新,选择的内容会在第一次单击项目后立即显示。

Is there anything wrong with accessing other UI elements in an OnItemClickListener? 访问OnItemClickListener中的其他UI元素有什么问题吗? Do I have to trigger some layout update after updating the TextView? 更新TextView后是否必须触发一些布局更新?

Update: Inserted Sweety's suggestions into my code, added XML code 更新:将Sweety的建议插入我的代码中,添加了XML代码

Try to set all items inside your list view to non focusable. 尝试将列表视图内的所有项目设置为非焦点化。

final ListView list = (ListView) findViewById(R.id.list); 最后的ListView list =(ListView)findViewById(R.id.list); list.setItemsCanFocus(false); list.setItemsCanFocus(false);

Also, make sure that for items inside list item set focusable false 另外,请确保对于列表项内的项设置可聚焦的false

android:focusable="false" android:focusableInTouchMode="false" android:focusable =“ false” android:focusableInTouchMode =“ false”
Also view.setFocusable(true) before setText. 在setText之前还要view.setFocusable(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM