How to avoid selector while dragging image in Gallery in Android

邮差的信 提交于 2019-12-11 02:04:41

问题


I have a series of images shown in a Gallery. WHen the user clicks on one, they are taken to a different view. I want to give some feedback to the user when they perform the click, just before the view changes.

I defined a selector thus:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pic_frame_pressed" />
    <item android:drawable="@drawable/pic_frame" /> <!-- default -->
</selector>

This works. When the user clicks on an image in the gallery a frame is displayed...

But, it is also shown when the user is dragging the gallery back and fore using an image.

I looked at the different states I could find for a selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

and I tried them but couldn't find a combination that only fired when the user clicked on the image (e.g. "selected" always fires for the image that is in the center of the Gallery)

I need a kind of "state_clicked"...


回答1:


I face the same situation here
The only solution I found is to handle the background switch myself on the OnItemClick event (which is not elegant :/) :

            mGallery.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View clickedView, int position, long id) {
                for(int i = 0; i < parent.getChildCount(); i++){
                    parent.getChildAt(i).setBackgroundResource(R.drawable.bg_menu);
                    ((TextView) parent.getChildAt(i)).setTextColor(getResources().getColor(R.color.white));
                }
                clickedView.setBackgroundResource(R.drawable.bg_menu_selected);
                ((TextView) clickedView).setTextColor(getResources().getColor(R.color.dark_blue));
                ((SectionAdapter) mGallery.getAdapter()).setSelected(position);
            }
        });

As you can see I store the selected position on my adapter in order to display the items with the correct layout when they are redraw (lose focus)
I really wanted to find another solution but I don't yet

If anyone got it I'll be pleased to hear :D



来源:https://stackoverflow.com/questions/6580374/how-to-avoid-selector-while-dragging-image-in-gallery-in-android

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