1

我制作了一个示例应用程序来在 viewflipper 中翻阅不同的布局。

XML基本上是(伪代码)

<ViewFlipper>
<LinearLayout><TextView text:"this is the first page" /></LinearLayout>
<LinearLayout><TextView text:"this is the second page" /></LinearLayout>
<LinearLayout><TextView text:"this is the third page" /></LinearLayout>
</ViewFlipper>

在 Java 代码中,

public boolean onTouchEvent(MotionEvent event)
case MotionEvent.ACTION_DOWN {
   oldTouchValue = event.getX()
} case MotionEvent.ACTION_UP {
   //depending on Direction, do viewFlipper.showPrevious or viewFlipper.showNext
   //after setting appropriate animations with appropriate start/end locations
} case MotionEvent.ACTION_MOVE {
   //Depending on the direction
   nextScreen.setVisibility(View.Visible)
   nextScreen.layout(l, t, r, b) // l computed appropriately
   CurrentScreen.layout(l2, t2, r2, b2) // l2 computed appropriately
}

当在屏幕上拖动时(就像主屏幕一样),上面的伪代码可以很好地在 viewflipper 内移动线性布局。

问题是当我执行 nextScreen.setVisibility(View.VISIBLE) 时。当下一个屏幕设置为可见时,它会在屏幕上闪烁,然后再移动到适当的位置。(我猜它在 0 位置可见。)

有没有办法加载下一个屏幕而不让它在屏幕上闪烁?我希望它在屏幕外加载(可见),这样它就不会闪烁。

非常感谢您的时间和帮助!

4

1 回答 1

3

+1. I'm having the exact same problem. I tried switching the layout() and setVisible() calls to no effect.

Update: The problem turns out to be the correct sequence in setting the visibility of the nextScreen view. If you set the visibility to VISIBLE before calling layout(), you get the flicker at position 0 as you noticed. But if you call layout() first, it gets ignored because the visibility is GONE. I did two things to fix this:

  1. Set the visibility to INVISIBLE before first layout() call. This differs from GONE in that the layout() is executed - you just don't see it.
  2. Set the visibility to VISIBLE asynchronously, so the layout() and related messages are processed first

In code:

case MotionEvent.ACTION_DOWN:
    nextScreen.setVisibility(View.INVISIBLE); //from View.GONE

case MotionEvent.ACTION_MOVE:
    nextScreen.layout(l, t, r, b);
    if (nextScreen.getVisibility() != View.VISIBLE) {
    //the view is not visible, so send a message to make it so
    mHandler.sendMessage(Message.obtain(mHandler, 0));
}

private class ViewHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        nextScreen.setVisibility(View.VISIBLE);
    }
}

More elegant/easier solutions are welcome!

于 2010-09-13T08:24:18.273 回答