视图翻转器中的列表视图

Listview inside viewflipper

我在 viewFlipper 中有一个 listView 时遇到问题。 listview 将手势作为普通点击操作,并像 listview item onClickListener 一样操作。

我使用以下代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.slideanimation.MainActivity">

<ViewFlipper
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Flipper">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#aaff"
            android:id="@+id/view1"/>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#aaff22"
            android:id="@+id/view3"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/MyListView"
            />
    </RelativeLayout>

</ViewFlipper>

</RelativeLayout>

MainActivity.java

package com.test.slideanimation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

ListView MyList;
ViewFlipper viewFlipper;
float lastX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyList = (ListView) findViewById(R.id.MyListView);
    viewFlipper = (ViewFlipper) findViewById(R.id.Flipper);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    MyList.setAdapter(adapter);


}

// Using the following method, we will handle all screen swaps.
public boolean onTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {

        case MotionEvent.ACTION_DOWN:
            lastX = touchevent.getX();
            break;
        case MotionEvent.ACTION_UP:
            float currentX = touchevent.getX();

            // Handling left to right screen swap.
            if (lastX < currentX) {

                // If there aren't any other children, just break.
                if (viewFlipper.getDisplayedChild() == 0)
                    break;

                // Next screen comes in from left.
                viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
                // Current screen goes out from right.
                viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);

                // Display next screen.
                viewFlipper.showNext();
            }

            // Handling right to left screen swap.
            if (lastX > currentX) {

                // If there is a child (to the left), kust break.
                if (viewFlipper.getDisplayedChild() == 1)
                    break;

                // Next screen comes in from right.
                viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
                // Current screen goes out from left.
                viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);

                // Display previous screen.
                viewFlipper.showPrevious();
            }
            break;
    }
    return false;
}

}

希望有人帮忙谢谢。

它通过使用 ViewPager 而不是 ViewFlipper 完美运行感谢@Mike M.for 该信息。

https://developer.android.com/training/animation/screen-slide.html