ViewFlipper未检测到列表视图上的手势

时间:2014-07-22 15:55:06

标签: java android listview viewflipper

我有一个带有两个线性布局的viewflipper。如果我在每个线性布局中只有一些文本,我可以在布局之间轻扫。我在第一个布局中添加了一个列表视图,当我尝试在此列表视图上滑动到下一个布局时,它没有检测到它。我怎样才能解决这个问题?谢谢!

活动

public class MainActivity extends Activity {
private ViewFlipper vf;
private float lastX;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vf = (ViewFlipper) findViewById(R.id.view_flipper);

    ListView listView = (ListView) findViewById(R.id.listView1);
    String[] values = new String[] { "1", "2", "3", "4", "5", "6", "7", "8" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listView.setAdapter(adapter);
}

@Override
public boolean onTouchEvent(MotionEvent touchevent) {
    switch (touchevent.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        lastX = touchevent.getX();
        break;
    }
    case MotionEvent.ACTION_UP: {
        float currentX = touchevent.getX();
        if (lastX < currentX) {
            if (vf.getDisplayedChild() == 0)
                break;
            vf.setInAnimation(this, R.anim.in_from_left);
            vf.setOutAnimation(this, R.anim.out_to_right);
            vf.showNext();
        }
        if (lastX > currentX) {
            if (vf.getDisplayedChild() == 1)
                break;
            vf.setInAnimation(this, R.anim.in_from_right);
            vf.setOutAnimation(this, R.anim.out_to_left);
            vf.showPrevious();
        }
        break;
    }
    }
    return false;
}
}

布局

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="View Flipper Test" />

<ViewFlipper
    android:id="@+id/view_flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="6dip" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="0dp"
            android:dividerHeight="10.0sp" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Blank Layout!"
            android:textColor="#00ff00"
            android:textSize="14sp"
            android:textStyle="bold" >
        </TextView>
    </LinearLayout>
</ViewFlipper>

</LinearLayout>

0 个答案:

没有答案