开发者

How to avoid selector while dragging image in Gallery in Android

开发者 https://www.devze.com 2023-03-17 14:03 出处:网络
I have a series of images shown in a Gallery. WHen the user clicks on one, they are taken to a different view.

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"...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号