开发者

Need help with color selector for checked ListView items

I have a ListView with custom layout for its items and need to specify the separate background color for checked items using selector. Here is selector for odd items:

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

and here is selector for even items:

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

ListView item template looks like this:

<?xml version="1.0" encoding="utf-8"?>
<com.androidhub.core.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="40dip">
    <TextView 
        android:text="TextView" 
        android:id="@+id/textView1" 
        android:layout_height="fill_parent" 
        android:layout_width="wrap_content" 
        android:gravity="center_vertical" 
        android:textColor="@color/list_item"></TextView>
</com.androidhub.core.CheckableLinearLayout>

The root element of list item is CheckableLinearLayout: http://paste.org/pastebin/view/28711

ListView has single choice mode (I also tried multiple choice mode but it also does not work for me)

public class MainActivity extends ListActivity {

    List<String> items = new ArrayList<String>();

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

        items.add("test 1");
        items.add("test 2");
        items.add("test 3");
        items.add("test 4");
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setAdapter(new TestAdapter(this, items));
        getListView().setBackgroundColor(getResources().
            getColor(android.R.color.white));
    }
}

In adapter for this ListView I call setChecked() method of CheckableLinearLayout but it does not change its color:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());
    View v = convertView;
    if(v == null) {
        v = inflater.inflate(R.layout.list_item, null);
    }

    if(position % 2 != 0) {
        v.setBackgroundResource(R.drawable.list_item_even);
    }
    else {
        v.setBackgroundResource(R.drawable.list_item_odd);
    }

    if(parent instanceof ListView) {
        ListView lv = (ListView)parent;
        if(v instanceof CheckableLinearLayout) {
            CheckableLinearLayout cll = (CheckableLinearLayout)v;
            cll.setChecked(lv.isItemChecked(position));
        }
    }

    return v;
}

What I'm doing wrong? I can see that state_pressed item from selector works fine but 开发者_C百科state_checked does not. What will be the right approach to setup state_checked background from XML, not from code?


I think you are looking for this attribute:

android:duplicateParentState="true"

Try adding it to your CheckableLinearLayout tag in your cell layout.

The key press/release is the only event that is propagated to the child views of a ViewGroup, so each view will manage it's own state unless you explicitly tell it to use it's parents'.

Also, after this change I think you can just remove the entire block of code that forces the CheckableLinearLayout to be checked:

if(parent instanceof ListView)...


Try implementing getListView().setOnItemClickListener like this:

OnItemClickListener listener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        CheckableLinearLayout r;
        for(int i = 0; i< lv.getChildCount(); i++) {
            r= (CheckableLinearLayout)lv.getChildAt(i);
            if(i==position)
                r.setChecked(true);
            else
                r.setChecked(false);
        }

    }
};


Have you tried creating an XML to hold your ListView similar to this? There's some wizardry around the @+id/android:list tag.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@drawable/megaphone_320x480">
        <ListView 
                android:id="@+id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:background="@drawable/megaphone_320x480"
                android:cacheColorHint="@drawable/transparent"
                />
        <TextView 
                android:id="@id/android:empty" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:textColor="@drawable/red"
                android:gravity="center_vertical|center_horizontal"
                android:textStyle="bold"
                android:text="@string/error_no_groups" 
                />
</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜