1

在我的 android 应用程序中,我有一个带有 ViewFlipper 的 Activity。在 Viewflipper 内部,我有 3 个线性布局。

 <ViewFlipper
    android:id="@+id/viewFlipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dip"
            android:text="@string/publicas"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ListView
            android:id="@+id/listViewDemandasPublicas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dip"
            android:text="@string/pendientes"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ListView
            android:id="@+id/listViewDemandasPendientes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dip"
            android:text="@string/pendientes"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ListView
            android:id="@+id/listViewDemandasFinalizadas"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ViewFlipper>

所以我想更改水平滚动时包含的 ViewFlippers。所以我实现了一个手势检测器和一个手势监听器。即使我有一个 ListView 里面(我已经在另一个活动中完成),它也能正常工作。我的 onScroll 方法是下一个:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
 ViewFlipper vf = (ViewFlipper) getWindow().findViewById(R.id.viewFlipper);
    View view = null;
    if ((e1.getX() < e2.getX()) && (distanceX < -20)) { // dedo a la derecha
        Log.i("ListaDemandas", "Muevo a la derecha SCROLL");
        if (esPendientes) {
            esPendientes = false;
            esEntrada = true;
            verPublicas(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_out));
            vf.showPrevious();
        } else if (esFinalizadas) {
            esFinalizadas = false;
            esPendientes = true;
            verPendientes(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_right_out));
            vf.showPrevious();
        }
    } else if ((e1.getX() > e2.getX()) && (distanceX > 20)) { 
        Log.i("ListaDemandas", "Muevo a la izquierda SCROLL");
        if (esPendientes) {
            esPendientes = false;
            esFinalizadas = true;
            verFinalizadas(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_left_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), .anim.push_left_out));
            vf.showNext();
        } else if (esEntrada) {
            esEntrada = false;
            esPendientes = true;
            verPendientes(view);
            vf.setInAnimation(AnimationUtils.loadAnimation(getParent(), R.anim.push_left_in));
            vf.setOutAnimation(AnimationUtils.loadAnimation(getParent(), .anim.push_left_out));
            vf.showNext();
        }
    }
    return true;
}

我的问题是每次滚动距离大于 20 时都会调用 onScroll (当然)。因此,如果我进行长滚动,则每次滚动移动大于 20 时都会调用该方法。因此,如果我的 Scroll 的距离为 50,则该方法将在 21、22、23 中调用......

我该如何管理它以使该方法仅被调用一次?我一直在阅读 API 并且一直在尝试控制 MotionEvent 时间,但这不是一个好的解决方案,因为我可以快速滚动两次。我也见过 addBatch 方法。这是我的解决方案?任何人都知道我怎样才能将所有电话批量处理?任何“手工”方式?

谢谢

==================================================== ==================================================== =========

编辑:

我以手工方式解决了它。用真正的 newScroll 初始化的布尔值。因此,当我制作 Scroll 时,我将其设置为 false 并且 onDown() 我再次将其设置为 true。所以:

  @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    ViewFlipper vf = (ViewFlipper) getWindow().findViewById(R.id.viewFlipper);
    if (scrollNuevo) {

        if ((e1.getX() < e2.getX()) && (distanceX < -20)) {                 
            Log.i("ListaDemandas", "Muevo a la derecha SCROLL");
            scrollNuevo = false; Log.i("ListaDemandas","ScrollNuevo=false");
            if (esPendientes) {
     ...

以及 onDown 方法:

@Override
public boolean onDown(MotionEvent e) {
    scrollNuevo = true;Log.i("ListaDemandas","ScrollNuevo=true");
    return true;
}
4

1 回答 1

1

我以手工方式解决了它。用真正的 newScroll 初始化的布尔值。因此,当我制作 Scroll 时,我将其设置为 false 并且 onDown() 我再次将其设置为 true。所以:

  @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    ViewFlipper vf = (ViewFlipper) getWindow().findViewById(R.id.viewFlipper);
    if (scrollNuevo) {

        if ((e1.getX() < e2.getX()) && (distanceX < -20)) {                 
            Log.i("ListaDemandas", "Muevo a la derecha SCROLL");
            scrollNuevo = false; Log.i("ListaDemandas","ScrollNuevo=false");
            if (esPendientes) {
     ...

以及 onDown 方法:

@Override
public boolean onDown(MotionEvent e) {
scrollNuevo = true;Log.i("ListaDemandas","ScrollNuevo=true");
return true;
}
于 2012-12-12T11:18:37.477 回答