Android persistent gridview selector on CAB

拟墨画扇 提交于 2019-12-23 05:31:37

问题


I have a GridView using a custom adapter which consists of a relativelayout containing an imageview. I have enabled MultiChoiceMode on the gridview and enabled CAB (Contextual Action Bar). This works great, however I am stumped on how I will show a persistent selector around my grid items as they are selected. My selector works on the initial press, but I have yet to find a way in which it will persist until the item is unselected.

Expectation: User long presses an item, the item is selected and the application enters CAB mode. As each item is pressed a purple frame (as defined by my selector statement in the XML) will appear around each object until they are unselected.

Result: User long presses an item, the item is briefly showing the selector, it disappears, and user gets no visual feedback to selecting or unselecting items, even though it's registered in code.

I have tried every single selector event but none seems to handle this. Any workarounds? Anything I'm missing?

An example of what I am trying to achieve is presented by trying to long press an item in the Android 4.0 or 4.1 Gallery application

---> API 15 <---


回答1:


Try modifying the layout for your list items to have a different background attribute:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItemRelativeLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent"
    android:background="?android:attr/activatedBackgroundIndicator">

    ...

</RelativeLayout>

That Android attribute references a selector that's used when you call listView.setItemChecked(int index, boolean checked), and for some reason it seems to work this way.

Also, verify that you call this on the list view:

listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);


来源:https://stackoverflow.com/questions/11731857/android-persistent-gridview-selector-on-cab

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