我有以下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;
            }
        });


绝对不会在相对布局或视图触发器上调用代码,但在图像视图上会调用它。我应该怎么做才能使其在相对布局或视图脚蹼上工作?

谢谢

最佳答案

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

@Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
    //do your stuff here
    return super.onInterceptTouchEvent(ev);
}

关于java - setOnTouchListener不适用于相对布局或viewFlipper,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18304059/

10-09 05:43