Listview里面的viewflipper

时间:2016-09-19 23:00:00

标签: android listview viewflipper

我在viewFlipper中有一个listView时遇到问题。 listview以手势作为普通点击进行操作,其行为类似于列表视图项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;
}

}

希望有人帮助谢谢。

1 个答案:

答案 0 :(得分:0)

通过使用ViewPager而不是ViewFlipper,它完美无缺。感谢@Mike M.提供的信息。

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