【问题标题】:How to make listview selected item selectable?如何使列表视图选定的项目可选择?
【发布时间】:2014-12-19 10:19:58
【问题描述】:

我正在实现一个无穷无尽的 ListView(就像在 Facebook 应用程序中一样)。我想将项目设为我选择的选择(暂时将背景显示为灰色)。当我单击列表视图时,什么也没有发生(未显示为灰色)。我尝试为我的列表视图内容设置android:focusable="false"android:cickable="false"。但我没有注意到任何变化我的错是什么?任何一位帮助我。提前致谢。

【问题讨论】:

    标签: android xml listview android-listview


    【解决方案1】:

    您可以使用选择器来完成此操作,您可以将其设置为列表项的背景。

    drawable/background_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/gray" android:state_pressed="true" />
        <item android:drawable="@color/gray" android:state_selected="true" />
        <item android:drawable="@color/gray" android:state_activated="true" />
        <item android:drawable="@color/default_color" /> 
    </selector>
    

    布局/list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_list_item"
        android:orientation="vertical">
    
        <... />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      如果您想将单击的选项显示为突出显示。不要将 clicklistener 放到 convetview 中。使用

            ListView.SetOnclicklistener(..){
              ...
            }
      

      这是消除问题的最佳方法。

      【讨论】:

        【解决方案3】:

        试试这个作为背景:

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        
            <item android:drawable="@color/someGreyColor" android:state_activated="true"/>
            <item android:drawable="@color/anyColor" android:state_pressed="true"/>
            <item android:drawable="@color/backgrounColor"/>
        
        </selector>
        

        【讨论】:

          猜你喜欢
          • 2015-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-25
          • 2012-04-04
          • 2013-11-09
          • 1970-01-01
          相关资源
          最近更新 更多