简体   繁体   中英

ListView selection custom color

I've made my own custom theme but there are a few bits that the theme didn't color such as the ListView I'm working on so I'm doing those piece by piece. I've done my homework with this and know about creating the drawable background selector XML file, but it's not working right. Further I tried some other ways to force it but can't get it to work. I'll show you the problem then follow that with my code.

The "Pressed" color is working. In this screenshot, I'm pressing and holding down an item:

按ListView项目

However when I remove my finger, the selected item doesn't remain colored. Note that checklist 1 is selected and displayed but no highlight:

移开手指,选择了ListView项,但没有颜色

This is my background drawable that I apply to each list item view:

<?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="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@android:color/holo_orange_dark" />
<item android:state_selected="true" android:state_pressed="false"
    android:drawable="@android:color/holo_orange_light" />
</selector>

You'll note that "selected" should show "holo_orange_light" but does not. The following code is where I assign the background. I initially was instantiating an off the shelf ArrayAdapter. In order to apply this background I descended from ArrayAdapter and added the code to the method that builds the view.

Here is the customized ArrayAdapter:

private class CustomArrayAdapter extends ArrayAdapter<Checklist> {

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId,
            List<Checklist> objects) {
        super(context, resource, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
        convertView.setBackgroundResource(R.drawable.listview_background);
        return convertView;
    }

}

FWIW this is the call creating the adapter so you can see the parameters I used:

setListAdapter(new CustomArrayAdapter(getActivity(),
                android.R.layout.simple_list_item_activated_1, android.R.id.text1,
                KnowUrStuffApp.getDbHelper().getChecklists()));

So that code left me where I am now. However I attempted to force it to color in a couple of redundant ways. I created a routine called setSelectionHilight that explicitly colors the view background to holo orange. I then created a onItemSelectedListener assigned to the ListView that called setSelectionHilight. Then I flat out called the routine explicitly once everything loads. None of these had any effect:

private void setSelectionHilight(ListView listView, View selectedView) {
    for (int i = 0; i < listView.getChildCount(); i++) {
        View child = listView.getChildAt(i);
        if (child == selectedView)
            child.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_orange_light));
        //else
            //child.setBackgroundResource(0);
    }
}


private OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {



    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        setSelectionHilight((ListView) parent, view);

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        setSelectionHilight((ListView) parent, null);

    }

};

Again, I was just experimenting with the chunk directly above. You can safely ignore that in considering a solution to my problem, but I include it for a complete picture. Thanks for any help on solving this. Another bit of ancillary information is that the ListView is encapsulated in a ListFragment and I am using the default layout that comes baked in with that. I don't have an explicit XML element for the ListView.

I wanted to do something similar and developed my own logic to implement it. My code is for highlighting the currently selected ChildView and if you see the code, you can implement it for both ParentView and ChildViews.

    public int _groupPosition = -1;
    View _lastColored;
    private int _highlightedGroup = -1;
    private int _highlightedChild;



    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {

        _groupPosition = groupPosition;
        _highlightedGroup = groupPosition;
        _highlightedChild = childPosition;
        if (_lastColored != null) {
            _lastColored.setBackgroundColor(Color.TRANSPARENT);
        }
        _lastColored = v;
        v.setBackgroundColor(Color.rgb(214, 214, 214)); // whatever colour you want to set
             ..... // your code here
}

No need to use any selectors in your XML file. This code does it all. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM