ListView中选定的项目绘制项目、ListView

2023-09-05 02:48:17 作者:愚剧

我在我的ListView的一个问题。我想更改选定项的背景,当我选择它以一个自定义绘制的,我有,但它不工作。

我想有这种效果的图像是将selectedItem

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_selected =真
          机器人:可绘制=@可绘制/ selected_item/>
    <项目机器人:可绘制=@机器人:彩色/透明/>
< /选择器>
 

这是list_row的文字

 <! - 这个布局是用来查看列表和RESTURANT名单列 - >
< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / txt_category_row
    机器人:重力=中心
    机器人:layout_width =match_parent
    机器人:文字颜色=@彩色/黑白
    机器人:layout_height =55dp
    机器人:填充=10dp
    机器人:文本=@字符串/你好
    机器人:layout_marginTop =5DP

    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?
 

这在ListView code.xml

 < ListView的机器人:ID =@ + ID / list_category
    机器人:layout_alignParentLeft =真
    机器人:layout_height =FILL_PARENT
    机器人:layout_width =184dp
    机器人:dividerHeight =2px的
    机器人:背景=@机器人:彩色/透明
    机器人:cacheColorHint =@机器人:彩色/透明
    机器人:layout_marginTop =5DP
    机器人:listSelector =@可绘制/ list_selector
    机器人:分隔=@彩色/灰度>
 
28个采购管理工具,你了解几个

解决方案

在触摸模式下没有选择或聚焦状态。

不过,你可以有一个选中状态(即使没有复选框),并使用在选项来更改属性。当您设置为显示您的列表中添加此行后您定义的列表视图你的java code:

  getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE)。
 

然后在您的国家列表中的XML改变这样的:

 安卓state_selected =真
 

这样:

 安卓state_activated =真
 

所以,现在你应该有:

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_activated =真
        机器人:可绘制=@可绘制/ selected_item/>
    <项目机器人:可绘制=@机器人:彩色/透明/>
< /选择器>
 

最后,设置背景为你的行以指向您选择的文件:

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / txt_category_row
    机器人:重力=中心
    机器人:layout_width =match_parent
    机器人:文字颜色=@彩色/黑白
    机器人:layout_height =55dp
    机器人:填充=10dp
    机器人:文本=@字符串/你好
    机器人:layout_marginTop =5DP
    机器人:textAppearance =机器人:ATTR / textAppearanceLarge
    机器人:背景=@可绘制/ item_selector/>
 

您没有给你的状态列表的XML,所以我只是用item_selector的名称。您应该替换与任何该文件的名称实际上是。

I have a problem in my ListView. I want to change the background of selected item when I select it to a custom drawable that I have, but it doesn't work.

I want to have this effect as in the image to be the selecteditem

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" 
          android:drawable="@drawable/selected_item"/>
    <item android:drawable="@android:color/transparent" />
</selector>

And this is the text of list_row

<!-- this layout is used to view row of category list and resturant list  -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txt_category_row" 
    android:gravity="center"
    android:layout_width="match_parent" 
    android:textColor="@color/BLACK"
    android:layout_height="55dp" 
    android:padding="10dp"
    android:text="@string/hello" 
    android:layout_marginTop="5dp"

    android:textAppearance="?android:attr/textAppearanceLarge" />

And this the ListView code.xml:

<ListView android:id="@+id/list_category" 
    android:layout_alignParentLeft="true" 
    android:layout_height="fill_parent" 
    android:layout_width="184dp" 
    android:dividerHeight="2px"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:layout_marginTop="5dp"  
    android:listSelector="@drawable/list_selector"
    android:divider="@color/Gray"  >

解决方案

In touch mode there is no selected or focused state.

However, you can have a checked state (even without a checkbox) and use that to change properties upon "selection". In your java code where you set up to display your list add this line after you define the listview:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Then in your state list XML change this:

android:state_selected="true"

to this:

android:state_activated="true"

So now you should have:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_activated="true"  
        android:drawable="@drawable/selected_item"/> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

And finally, set the background for your row view to point to your selector file:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/txt_category_row"  
    android:gravity="center" 
    android:layout_width="match_parent"  
    android:textColor="@color/BLACK" 
    android:layout_height="55dp"  
    android:padding="10dp" 
    android:text="@string/hello"  
    android:layout_marginTop="5dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:background="@drawable/item_selector" /> 

You didn't give a name for your state list XML so I just used "item_selector". You should replace that with whatever the name of that file actually is.