GridView item list selector for multiple items not working in Android

后端 未结 5 817
粉色の甜心
粉色の甜心 2021-01-08 01:30

I want to draw selector on long press as shown in the picture. When I do long press on one item, the CAB menu is activated. But the list selector indicator goes off once aft

相关标签:
5条回答
  • 2021-01-08 01:48

    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.

    0 讨论(0)
  • 2021-01-08 02:06

    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

    0 讨论(0)
  • 2021-01-08 02:07

    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.

    0 讨论(0)
  • 2021-01-08 02:12

    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

    0 讨论(0)
  • 2021-01-08 02:15

    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.

    0 讨论(0)
提交回复
热议问题