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!