我正在尝试使用ViewFlipper,并将其表现为主屏幕(布局将用手指移动)。 看看这个 例如。我想用一个仅包含两个孩子的ViewFlipper来做到这一点,因此,应根据用户移动手指的方式在当前视图的两侧显示相反的视图。该代码起作用,但一次仅适用于1个方向。这在Ontouchevent中。

case MotionEvent.ACTION_MOVE: 

leftView.setVisibility(View.VISIBLE);
rightView.setVisibility(View.VISIBLE);
// move the current view to the left or right.
currentView.layout((int) (touchEvent.getX() - oldTouchValue),
    currentView.getTop(),
    (int) (touchEvent.getX() - oldTouchValue) + 320,
    currentView.getBottom());

// place this view just left of the currentView
leftView.layout(currentView.getLeft() - 320, leftView.getTop(),
    currentView.getLeft(), leftView.getBottom());

// place this view just right of the currentView
rightView.layout(currentView.getRight(), rightView.getTop(), 
    currentView.getRight() + 320, rightView.getBottom());

我将最后一个方向放置的最底层的哪一条线都可以正常工作,而另一个则不会。

这是我设置LeftView和Rightview的方式:

final View currentView = myFlipper.getCurrentView();
final View leftView, rightView;
if (currentView == meView) {
    Log.d("current layout: ", "me");
    leftView = youView;
    rightView = youView;
} else if (currentView == youView) {
    Log.d("current layout: ", "you");
    leftView = meView;
    rightView = meView;
} else {
    leftView = null;
    rightView = null;
}

是否有可能将其设置,以便在当前视图的两侧显示相同的视图?

有帮助吗?

解决方案

感谢Stealthcopter

如果有人有兴趣,这里有效的是新代码。

        if (touchEvent.getX() < oldTouchValue){
             // place this view just right of the currentView
            rightView.layout(currentView.getRight(), rightView.getTop(), 
                            currentView.getRight() + 320, rightView.getBottom());

        }else if (touchEvent.getX() > oldTouchValue) {
            // place this view just left of the currentView
            leftView.layout(currentView.getLeft() - 320, leftView.getTop(),
                            currentView.getLeft(), leftView.getBottom());

        }

我还将setVisibility()呼叫移到了MotionEvent.action_down,以摆脱视图的闪烁。这有帮助,但我仍然有点。

其他提示

我可能不是很有建设性的建议,但是如果您希望它像主屏幕一样,为什么不想看 SRC的, 并根据您的需求进行修改?

要摆脱闪烁,请设置 setVisibility(View.INVISIBLE)MotionEvent.ACTION_DOWN. 。默认值在视图上“消失”。这意味着您无法设置 Layout() 从“可见”或“看不见”的视图中。因此,三个步骤:

  1. 在视图上设置了看不见的可见性
  2. 在视图上设置布局()
  3. 在视图中设置可见的可见性

如果我理解您的要求,现在应该使用 查看Pager

看起来这样:

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top