0

我有这个 XML 代码:

<RelativeLayout
        android:id="@+id/home_fragment_riddle_flipper_holder_RelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true"
        android:focusable="true" 
        android:clickable="false"
        android:layout_below="@+id/relativeLayout1" >

        <ViewFlipper
            android:id="@+id/home_fragment_riddle_flipper_ViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true" >

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF"
                    android:orientation="vertical" >
                </LinearLayout>
            </ScrollView>

            <ScrollView
                android:id="@+id/scrollView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#000000"
                    android:orientation="vertical" >
                </LinearLayout>
            </ScrollView>
        </ViewFlipper>
    </RelativeLayout>

这个java/android代码:

public class HomeFragment extends Fragment implements OnGestureListener 
{
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_TRESHOLD_VELOCITY = 200;

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        thisFragment = LayoutInflater.from(getActivity()).inflate(R.layout.home_fragment, null);

        //views
        riddleFlipper = (ViewFlipper) thisFragment.findViewById(R.id.home_fragment_riddle_flipper_ViewFlipper);
        userImage = (ImageView) thisFragment.findViewById(R.id.home_fragment_user_image_ImageView);
        FlipperHolder = (RelativeLayout) thisFragment.findViewById(R.id.home_fragment_riddle_flipper_holder_RelativeLayout);

        slideLeftIn = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_out);

        //sliding
        detecture = new GestureDetector(getActivity(), this);

        FlipperHolder.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View view, MotionEvent event)
            {
                detecture.onTouchEvent(event);
                return true;
            }
        });


        return thisFragment;
    }

@Override
    public boolean onDown(MotionEvent e)
    {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if(Math.abs(e1.getY() - e2.getY()) > 250)
            return false;
        if((e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_TRESHOLD_VELOCITY)
        {
            Toast.makeText(getActivity(), "a", Toast.LENGTH_SHORT).show();
            riddleFlipper.setInAnimation(slideLeftIn);
            riddleFlipper.setOutAnimation(slideLeftOut);
            riddleFlipper.showNext();
        }
        else if((e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_TRESHOLD_VELOCITY)
        {
            Toast.makeText(getActivity(), "b", Toast.LENGTH_SHORT).show();
            riddleFlipper.setInAnimation(slideRightIn);
            riddleFlipper.setOutAnimation(slideRightOut);
            riddleFlipper.showNext();
        }

        return false;
    }

这:

FlipperHolder.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View view, MotionEvent event)
            {
                detecture.onTouchEvent(event);
                return true;
            }
        });

代码永远不会在相对布局或视图翻转器上被调用,而是在图像视图上被调用。我应该怎么做才能使其在相对布局或视图翻转器上工作?

谢谢

4

1 回答 1

1

我认为对于包含其他视图的视图,您应该使用onInterceptTouchEvent代替:

@Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
    //do your stuff here
    return super.onInterceptTouchEvent(ev);
}
于 2013-09-08T09:55:09.063 回答