GridView item list selector for multiple items not working in Android

半腔热情 提交于 2019-12-01 03:09:08

I'm not sure it's the right solution, but I set this for the android:background on my list items:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:exitFadeDuration="@android:integer/config_mediumAnimTime" >

    <!-- I never see this one - the grid items are not focusable -->
    <item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/list_focused" />

    <!-- while pressed -->
    <item android:state_pressed="true" android:drawable="@drawable/pressed_background" />

    <!-- while selected in actionmode -->
    <item android:state_activated="true" android:drawable="@color/pressed" />

    <item android:drawable="@android:color/transparent" />
</selector>

I did not set the android:listSelector attribute on the grid view.

Give padding in your grid item. Maintain bean class for each object in grid adapter to track object is selected or unselected. Based on this value you can update your grid item background color acc to state selected/unselected. Update the object value in onitemclieck listner and also view background.

Swati

this can be achieved by tracking the selected items and changing their backgrounds inside the adapter.

I guess you will get little help from this post

I m suggested to implement custom drawables as per your requirement.In long press listener ,you can apply it whatever state you are being required. This is one of the way to write code from our own scratch.

https://github.com/rameshkec85/Example-Android-CustomDrawableStates

Munish Katoch

For this you need to change Grid View Item, BackgroundColor on single/long click.

Below is the reference code, for the same:

GridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mAdapter.setSelected(position,true);                                          
        view.setBackgroundColor(Color.XYZ);
    }
}

Hope this help's. Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!