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

时间:2012-08-21 05:30:39

标签: android listview android-listfragment

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

5 个答案:

答案 0 :(得分:7)

如果您使用的是ListFragment,那么您可能无法在XML中指定ListView,这意味着您需要在代码中设置listSelector。注意不要在活动生命周期中设置得太早,因为最终会得到IllegalStateException

level_list_selector.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>

片段:

public class LevelFragment extends ListFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       //Create view 
       ...
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getListView().setSelector(R.drawable.level_list_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>

答案 1 :(得分:2)

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

<强> 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>

ListView中将此选择器设置为listselector。前,

<ListView
    android:id="@+id/listView1"
    android:width="wrap_content"
    android:height="wrap_content"
    android:listSelector="@drawable/selector" />

看看这些例子 -

  1. ListSelector applies to the entire list

  2. ListView with selector

  3. Listview_and_list_selector

答案 2 :(得分:1)

为此,你必须制作一个drawable作为列表项的背景。并且在“选择”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>

答案 3 :(得分:1)

如果您想在ListFragment中使用标准的ListView行为,可以将列表项行布局设置为使用?android:attr / activatedBackgroundIndicator作为背景。像这样:

<强> ListItem.xml

<?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>

<强> ListFragment.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">
    <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>

在ListFragment片段代码中,使用自定义布局(这是单声道代码,但不要绝望):

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                base.OnCreateView(inflater, container, savedInstanceState);
                View view = inflater.Inflate(Resource.Layout.ListFragment, container, false);
                //... do stuff
    }

要在代码中创建适配器的某个位置,请使用ListItem布局。在这种情况下,SimpleAdapter将数据映射到Icon和Text视图。 ListItem.xml中的背景(?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 });

答案 4 :(得分:0)

在get视图方法()中的适配器类中,你可以这样做..

@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;
        }