带手势检测的线性布局:不允许单击ImageButton:为什么?

时间:2014-06-19 14:56:04

标签: android android-fragments android-linearlayout android-gesture android-actionbaractivity

以下扩展的linearlayout使用SimpleGestureListener来检测滑动事件。

public class MyLinearLayout extends LinearLayout implements SimpleGestureListener {
private SimpleGestureFilter detector;
private SwipeEvent listener;

@SuppressLint("NewApi")
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    detector = new SimpleGestureFilter((Activity) context, this);
    // TODO Auto-generated constructor stub
}

public MyLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    detector = new SimpleGestureFilter((Activity) context, this);
    // TODO Auto-generated constructor stub
}

public MyLinearLayout(Context context) {
    super(context);
    detector = new SimpleGestureFilter((Activity) context, this);

    // TODO Auto-generated constructor stub
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    this.detector.onTouchEvent(ev);
    boolean ret = super.dispatchTouchEvent(ev);
    Log.e("MLL","ret = "+String.valueOf(ret));
    return /*super.dispatchTouchEvent(ev);*/ ret;
}
/*@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    this.detector.onTouchEvent(ev);
    return super.onInterceptTouchEvent(ev);
}*/

@Override
public void onSwipe(int direction) {
    // TODO Auto-generated method stub

    Log.e("LinearLayout","Inside onSwipe");
    switch (direction) {

    case SimpleGestureFilter.SWIPE_RIGHT:
//      Toast.makeText(getContext(), "Swipe Right", Toast.LENGTH_SHORT).show();
        listener.onRightSwipe();

        break;
    case SimpleGestureFilter.SWIPE_LEFT:
//      Toast.makeText(getContext(), "Swipe SWIPE_LEFT", Toast.LENGTH_SHORT).show();
        listener.onLeftSwipe();

        break;
    case SimpleGestureFilter.SWIPE_DOWN:
//      Toast.makeText(getContext(), "Swipe SWIPE_DOWN", Toast.LENGTH_SHORT).show();
        listener.onDownSwipe();
        //TODO As playerControls becomes null on each orientation switch, let's change playerControls to playerControlFragments

        break;
    case SimpleGestureFilter.SWIPE_UP:
//      Toast.makeText(getContext(), "Swipe SWIPE_UP", Toast.LENGTH_SHORT).show();
        listener.onUpSwipe();

        break;

    }

}

@Override
public void onDoubleTap() {
    // TODO Auto-generated method stub

}
static public interface SwipeEvent {
    public void onRightSwipe();
    public void onLeftSwipe();
    public void onDownSwipe();
    public void onUpSwipe();
}
public void setSwipeEventListener(SwipeEvent listener) {
    // TODO Auto-generated method stub
    this.listener = listener;

}

}

问题是我无法使用放置在此布局中的Imagebutton的Onclick侦听器

<com.example.player.MyLinearLayout
        android:id="@+id/details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dip"
        android:orientation="vertical"
        android:animateLayoutChanges="true"
        android:descendantFocusability="afterDescendants"
        android:padding="3dip" >
        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dip"
        android:orientation="horizontal"
        android:animateLayoutChanges="true"
        android:padding="3dip"
            >
            <TextView
            android:id="@+id/firstTitle"
            android:layout_width="0dip"
            android:layout_weight="0.8"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="First"
            android:clickable="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

            <ImageButton
                android:id="@+id/hideControl"
                android:layout_weight="0.2"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:background="@android:color/transparent"
                android:rotation="90"
                android:src="@drawable/hide"
                android:clickable="true"

                 />

        </LinearLayout>


        <View
            android:layout_width="match_parent"
            android:layout_height="2dip" />

        <TextView
            android:id="@+id/secondTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left|bottom"
            android:text="Second"
            android:clickable="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </com.example.player.MyLinearLayout>

我正在听hideControls,但不知道为什么它没有被调用......

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
//      Log.e(TAG, "Inside fragment'sonCreateView");
    View rootView = inflater.inflate(R.layout.frag_layout,
            container, false);
hideControl = (ImageButton) rootView.findViewById(R.id.hideControl);
    hideControl.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                Log.e(TAG,"PlayerControl clicked via listener");

        }
    });

其中frag_layout.xml包含MyLinearLayout,例如

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="horizontal" >

    <LinearLayout
        ... >

    </LinearLayout>
    <com.example.player.MyLinearLayout
        ... />
    </com.example.player.MyLinearLayout>
</LinearLayout>

<RelativeLayout
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="horizontal" >

    ...

</RelativeLayout>


<LinearLayout
    ... />

</LinearLayout>

0 个答案:

没有答案