2

I have a linear layout which holds inner (child) custom image view.

My goal is to set a background selector on the image view, and also set a listener to the linear layout.

however, I can't make the selector and the listener work together. I don't know how to handle the events.

If I make a quick tap it is working, but If I drag my finger on the views, the image view remains in selected state.

Here's the activity which has a reference to the linear layout and its listener.

public class EventDispatchingDemoActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.my_layout);

        linearLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Here's the custom view (Its a code I found somewhere, credits for the author)

public class ImageSelector extends ImageView
{

    public ImageSelector(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setBackgroundDrawable(new NavStateListDrawable(getContext(), 0));
        setClickable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        super.onTouchEvent(event);
        return false;
    }

    public class NavStateListDrawable extends StateListDrawable
    {

        private int level;

        public NavStateListDrawable(Context context, int level)
        {

            this.level = level;
            // int stateChecked = android.R.attr.state_checked;
            int stateFocused = android.R.attr.state_focused;
            int statePressed = android.R.attr.state_pressed;
            int stateSelected = android.R.attr.state_selected;

            addState(new int[] { stateSelected }, context.getResources().getDrawable(R.drawable.slide_feedback));
            addState(new int[] { statePressed }, context.getResources().getDrawable(R.drawable.slide_feedback));
            addState(new int[] { stateFocused }, context.getResources().getDrawable(R.drawable.slide_feedback));
            addState(new int[] { -stateFocused, -statePressed, -stateSelected }, context.getResources().getDrawable(android.R.color.transparent));
        }

        @Override
        protected boolean onStateChange(int[] stateSet)
        {

            boolean nowstate = super.onStateChange(stateSet);

            try
            {
                LayerDrawable defaultDrawable = (LayerDrawable) this.getCurrent();

                LevelListDrawable bar2 = (LevelListDrawable) defaultDrawable.findDrawableByLayerId(R.id.element_contact_image_selector);
                bar2.setLevel(level);
            }
            catch (Exception exception)
            {

            }

            return nowstate;
        }
    }
}

And Here's the xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#acf1fa"
    android:gravity="center"
    android:orientation="vertical" >

    <!-- outer layout -->

    <LinearLayout
        android:id="@+id/my_layout"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#00ff00"
        android:gravity="center" >

        <!-- inner layout -->

        <dor.event.dispathing.ImageSelector
            android:id="@+id/inner_layout"
            android:layout_width="150dp"
            android:layout_height="150dp" />
    </LinearLayout>

</LinearLayout>

I Uploaded the source: http://www.filefactory.com/file/5ikfwyk9j5bx/n/EventDispatchingDemo.rar

Thanks in advance for your help!

dor506
  • 5,246
  • 9
  • 44
  • 79

1 Answers1

1

I remember having a similar issue when I was writing my application.

1) Are you trying to capture if a row in your listview is clicked? I think you want `listView.setonItemClickListener' which will capture clicks for each individual row as opposed to the entire listview.

2) I believe when you have a listview with clickable items inside it, the clickable items inside it take priority in terms of capturing clicks. More than taking priority, I believe they block any click events from being sent back to the parent's event handlers.

I believe a somewhat working solution is to set the focusability of those clickable items to false. Once those clickable items are not longer focusable, the listview row will get focus back. The other solution is to work with the descendantFocusability property which controls who (Entire row or Image) gets first crack and picking up events triggered within the row. Here are some other questions that seem to have what you need:

How to fire onListItemClick in Listactivity with buttons in list?

Android ListView with clickable items in it's rows causes problems continuing scrolling

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14