如何在android中设置ListFragment中所选项目的颜色?

如何在android中设置ListFragment中所选项目的颜色?,android,listview,android-listfragment,Android,Listview,Android Listfragment,我在ListFragment中有一个listview,它是从适配器填充的,我想突出显示列表中单击(或选中)的项目,并根据选择执行一些操作。我可以处理该事件,但如何设置列表中所选项目的颜色 只需创建一个包含所需状态的选择器文件。这将为您提供解决方案。例如— selector.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/a

我在
ListFragment
中有一个
listview
,它是从适配器填充的,我想突出显示列表中单击(或选中)的项目,并根据选择执行一些操作。我可以处理该事件,但如何设置列表中所选项目的颜色

只需创建一个包含所需状态的选择器文件。这将为您提供解决方案。例如—

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/bg_list_item_highlighted" /> <!--  @drawable/tab_focus -->

  <!-- Pressed -->
  <item 
    android:state_pressed="true" 
    android:drawable="@drawable/bg_list_item_pressed" /> <!--  @drawable/tab_press -->

</selector>
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator" <!--This is the important bit-->
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="?android:attr/listPreferredItemHeight" />
    <TextView
        android:text=""
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
<?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">
    <ListView xmlns:tools="http://schemas.android.com/tools"
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:divider="@drawable/divider"
        android:dividerHeight="0.5dp" />
    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="No data" />
</LinearLayout>
看看这些例子-


  • 在get view方法()中的适配器类中,您可以按如下方式执行此操作

    @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                View view = super.getView(position, convertView, parent);
                view.setTag(position);
    
                if(ur condition){
                    view.setBackgroundColor(Color.TRANSPARENT);
    
                }else {
                    view.setBackgroundColor(Color.GREEN);
                }
                return view;
            }
    

    为此,您必须制作一个可绘制的列表项作为背景。从lisView中“选择”任何项目后,将drawable设置为透明或#000.so,以便在项目选择选择器时显示

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://meta.android.com/apk/res/android">
    <item android:drawable="@color/transparent" />
    <item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
    </selector>
    

    如果使用的是
    列表片段
    ,则可能没有在XML中指定
    列表视图
    ,这意味着您需要在代码中设置
    列表选择器
    。不过,请注意不要在活动生命周期中过早地设置它,因为最终会出现
    非法状态异常

    级别列表选择器.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/level_gradient_bg" />
    
        <item android:state_pressed="true" android:drawable="@drawable/level_gradient_bg_hover" />
    
        <item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/level_gradient_bg_hover" />
    </selector>
    
    注意:如果对单元格行使用自定义布局,则还需要在该行上设置列表选择器

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/level_list_selector"
        android:padding="5dip" >
    ...
    </RelativeLayout>
    
    
    ...
    
    如果希望在ListFragment中使用标准ListView行为,可以将列表项行布局设置为使用?android:attr/activatedBackgroundIndicator作为背景。像这样:

    ListItem.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    
      <!-- Selected --> 
      <item 
        android:state_focused="true" 
        android:state_pressed="false" 
        android:drawable="@drawable/bg_list_item_highlighted" /> <!--  @drawable/tab_focus -->
    
      <!-- Pressed -->
      <item 
        android:state_pressed="true" 
        android:drawable="@drawable/bg_list_item_pressed" /> <!--  @drawable/tab_press -->
    
    </selector>
    
    <?xml version="1.0" encoding="utf-8"?>
    <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="wrap_content"
        android:background="?android:attr/activatedBackgroundIndicator" <!--This is the important bit-->
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="?android:attr/listPreferredItemHeight" />
        <TextView
            android:text=""
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minHeight="?android:attr/listPreferredItemHeight" />
    </LinearLayout>
    
    <?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">
        <ListView xmlns:tools="http://schemas.android.com/tools"
            android:id="@id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:divider="@drawable/divider"
            android:dividerHeight="0.5dp" />
        <TextView
            android:id="@id/android:empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="No data" />
    </LinearLayout>
    
    在代码中要创建适配器的某个位置,使用ListItem布局。在本例中,SimpleAdapter将数据映射到图标和文本视图。ListItem.xml中的backgound(?android:attr/activatedBackgroundIndicator)使项目的行为与您在普通列表中看到的一样:

    var list = new List<IDictionary<string, object>>(listOfStuff.Count);
    foreach (AMap map in listOfStuff) {
            var dictionary = new JavaDictionary<string, object> {
                {"text", map.Text}, {"icon", Resource.Drawable.SomeIcon}
            };
            list.Add(dictionary);
    }
    SimpleAdapter _adapter = new SimpleAdapter(Activity, list,
                                             Resource.Layout.ListItem,
                                             new string[] { "text", "icon" },
                                             new int[] { Resource.Id.text,
                                             Resource.Id.icon });
    
    var list=新列表(listofsuff.Count);
    foreach(listOfStuff中的AMap映射){
    var dictionary=newjavadictionary{
    {“text”,map.text},{“icon”,Resource.Drawable.SomeIcon}
    };
    列表.添加(字典);
    }
    SimpleAdapter\u adapter=新的SimpleAdapter(活动、列表、,
    Resource.Layout.ListItem,
    新字符串[]{“文本”,“图标”},
    新建int[]{Resource.Id.text,
    Resource.Id.icon});
    
    使用
    android:listselector
    查看此项。这可能对你有帮助,我用过这个,但它会永久设置颜色,我想在选择其他项目时删除颜色。谢谢,但我在哪里设置颜色?@AyushGoyal而不是突出显示的
    @dravable/bg\u list\u item\u
    和按下的
    @dravable/bg\u list\u item\u您可以在那里设置颜色。我很感谢您的努力,但我编辑了我的问题:这是一个列表片段,所以我没有任何xml文件可供选择信息技术一切都应该用java本身来处理。