java - 如何清除ListView选择?

标签 java android android-listview android-xml

TL;DR: You choose an option from (a) my listview. Then, you change your mind and type something in (b) my edit text. How do I clear your listview selection and only show your edittext? (and vice versa)



我有一个带有选项列表 View 的应用程序以及一个用于创建自己的选项的edittext。我需要用户选择或创建一个选项,但不要同时选择两者。这是我的布局图:

enter image description here

每当用户从列表 View 中选择一个选项时,我都会通过将其设置为绿色来将其设置为“选定”,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@color/colorPrimary"/>
    <item
        android:state_selected="false"
        android:drawable="@color/windowBackground" />
</selector>

(这被设置为我的列表 View 的背景)

问题:如果用户决定键入自己的选项,因为他们只能有一个选项,我想取消选择listview选项。
  • 用户从列表 View 中选择一个选项
  • 用户决定要使用edittext
  • 创建自己的选项
  • 当他们开始键入自己的
  • 时,未选中listview选项

    我尝试做following,但是没有取消选择。
    e.setOnEditorActionListener(new TextView.OnEditorActionListener()
            {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    
                    for(int i=0; i<=5; i++){
                                    listView.setItemChecked(i, false);
                    }
                    listView.clearChoices();
                    listView.requestLayout()
                    adapter.notifyDataSetChanged()
                 }
            }
    

    非常令人困惑的困境,我们将不胜感激!

    编辑:这是edittext的布局:
    <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/textView4"
                    android:layout_alignBottom="@+id/textView4"
                    android:layout_toEndOf="@+id/textView4"
                    android:layout_toRightOf="@+id/textView4"
                    android:color="@color/colorPrimary"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:textColor="@color/textColorPrimary"
                    android:textColorHint="@color/colorPrimary" />
    

    编辑:这是列表 View 的布局:
        <ListView
            android:id="@+id/listview"
            android:layout_width="wrap_content"
            android:layout_height="250dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/toolbar"
            android:background="@drawable/bg_key"
            android:choiceMode="singleChoice"
            android:listSelector="@color/colorPrimary">
    
        </ListView>
    

    最佳答案

    长话短说

  • ListView选择器(android:listSelector)用于指示单击事件,但不指示所选项目。
  • 如果绘制了ListView选择器(在第一次单击后),则不会在ListView
  • 中发生重大变化的情况下消失
  • 因此,如果未将任何状态应用于ListView选择器,则仅使用具有透明背景的可绘制对象。 不要为此使用纯色资源,也不要混淆自己
  • 使用ListView选择模式(android:choiceMode)指示所选项目。
  • ListView通过在根 View 上设置android:state_activated来告知选择了哪一行。为您的适配器提供相应的布局/ View ,以正确表示所选项目。


  • 理论

    好吧,乍看之下ListView中的内置选择非常棘手。但是,您应牢记有两个主要区别,以避免这种混淆:列表 View 选择器和选择模式。

    ListView选择器

    ListView选择器是一种可绘制资源,假定表示单击列表项的事件。您可以通过XML属性 android:listSelector 或使用 setSelector() 方法来指定它。我无法在文档中找到它,但我的理解是该资源不应为纯色,因为在绘制之后,如果不对 View 进行剧烈更改(例如设置适配器,则该资源可能会消失)就不会消失。导致出现一些小故障),因此,仅当应用了特定状态(例如 android:state_pressed )时,此类drawable才应可见。这是可以用作“列表 View ”选择器的可绘制对象的简单示例
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_pressed="true"
            android:drawable="@android:color/darker_gray" />
        <item
            android:drawable="@android:color/transparent" />
    </selector>
    

    无论出于何种原因,您都不能将Color State List用作“列表 View ”选择器,但仍可以使用纯色(大多数情况下不合适)和State List可绘制对象。这使事情有些困惑。
    第一次单击“列表 View ”后,您将无法轻松地从“列表 View ”中删除“列表 View ”选择器。

    这里的主要思想是列表 View 选择器并非旨在指示所选项目

    ListView选择模式

    假定ListView选择模式为表示所选项目。如您所知,在ListView中主要可以使用两种选择模式-单选和多选。它们允许跟踪分别选择的单个或多个行。您可以通过 android:choiceMode XML属性或 setChoiceMode() 方法设置它们。
    ListView本身将选定的行保留在其中,并通过设置行根 View 的 android:state_activated 属性让他们知道在任何给定时刻选择了哪一行。为了使您的行反射(reflect)此状态,其根 View 必须具有相应的可绘制集,例如作为背景。这是此类可绘制对象的示例:
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:state_activated="true"
            android:drawable="@android:color/holo_green_light" />
        <item
            android:drawable="@android:color/transparent" />
    </selector>
    

    您可以使用 setItemChecked() 方法以编程方式选择/取消选择行。如果希望ListView清除所有选定的项目,则可以使用 clearChoices() 方法。您还可以使用以下方法系列检查所选项目: getCheckedItemCount() getCheckedItemIds() getCheckedItemPosition() (用于单选模式), getCheckedItemPositions() (用于多选模式)

    结论

    如果您想保持简单,请勿使用“列表 View ”选择器指示选定的项目

    解决问题

    选项1.脏修复-隐藏选择器

    除了可以实际删除选择器,更改布局并实现可靠的方法外,我们还可以在需要时隐藏选择器可绘制,并在以后单击ListView项时将其显示:
        public void hideListViewSelector() {
            mListView.getSelector().setAlpha(0);
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mListView.getSelector().getAlpha() == 0) {
                mListView.getSelector().setAlpha(255);
            }
        }
    

    选项2。周到的方式

    让我们遍历您的代码,使其逐步遵循我描述的规则。

    修复ListView布局

    在ListView布局中,选择器设置为纯色,因此,在单击项目时,项目将由此着色。用作ListView背景的drawable没有影响,因为单击行时ListView状态不会改变,因此ListView始终只有@color/windowBackground背景。

    要解决您的问题,首先需要从ListView布局中删除选择器:

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/toolbar"
        android:listSelector="@color/colorPrimary"
        android:background="@color/windowBackground"
        android:choiceMode="singleChoice"/> 


    使您的行反射(reflect)激活状态

    在注释中,给适配器如下:
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, text1, listOfThings);
    

    您还问我是否可以继续使用标准适配器来实现所需的行为。我们可以肯定,但是无论如何都需要进行一些更改。对于这种情况,我可以看到3个选项:

    1.使用标准的android检查布局

    您可以只指定一个对应的标准布局-使用 CheckedTextView 且未更改背景可绘制对象作为根组件的任何布局,或使用 activatedBackgroundIndicator 作为其背景可绘制对象的布局。对于您的情况,最合适的选项应该是 simple_list_item_activated_1 。像这样在您的 ArrayAdapter 构造函数中设置它:
    final ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_activated_1, android.R.id.text1, listOfThings);

    这个选项最接近我对“标准”适配器的理解。

    2.自定义适配器

    您可以使用标准布局,并且大多数情况下可以使用标准适配器,除了可以获取商品 View 外,还可以使用一点异常(exception)。只需引入一个匿名类并重写 getView() 方法,即可为行 View 提供相应的背景可绘制对象:
    final ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, listOfThings) {
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            final View view = super.getView(position, convertView, parent);
            if (convertView == null) {
                view.setBackgroundResource(R.drawable.list_item_bg);
            }
            return view;
        }
    };

    3.自定义布局

    解决此问题的最常用方法当然是为项目 View 引入自己的布局。这是我的简单示例:
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/list_item_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="16dp">
    
        <TextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </FrameLayout>
    

    我将其保存在文件/res/layout/list_view_item.xml中。不要忘记在适配器中设置以下布局:
    final ArrayAdapter<String> adapter = new ArrayAdapter(this, R.layout.list_view_item, android.R.id.text1, listOfThings);

    必要时清除选择

    之后,您的行将在单击时反射(reflect)所选状态,并且您可以通过调用ListView和后果 clearChoices() 来要求ListView重绘自己,从而轻松清除requestLayout()的所选状态。

    这里有一点评论,如果要在用户开始键入时取消选择该项目,而不是在用户实际单击返回(完成)按钮时取消选择该项目,则需要使用 TextWatcher 回调:
        mEditText.addTextChangedListener(new TextWatcher(){
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mListView.getCheckedItemCount() > 0) {
                    mListView.clearChoices();
                    mListView.requestLayout();
                }
            }
    
            @Override
            public void afterTextChanged(Editable s) {}
        });
    

    希望它有所帮助。

    关于java - 如何清除ListView选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253761/

    相关文章:

    android - Flutter abiFilters 不为所有架构生成 libflutter.so

    java - Android ListView 不刷新

    android - 如何根据对象的字段设置列表项的不同顶行和底行?

    java - 使用 Netbeans 在本地 Java 服务器上进行开发

    java - 为什么 A.class.getClassLoader() 不返回 loader.loadClass ("A"使用的加载器)

    android - Xamarin Android fragment ZXing - ClassCastException

    java - 如何获取ListView中显示的Spinners的值?

    java - 允许在 Ubuntu 17.10 上对 Spring Boot Jar 进行低级端口访问

    java - 冒泡排序比较计数始终相同

    Android 2.3 wifi热点API