gpt4 book ai didi

android - 如何在 ListView 中使用viewpager?

转载 作者:行者123 更新时间:2023-11-29 01:42:02 25 4
gpt4 key购买 nike

我正在尝试在 listview 中添加 View 寻呼机,但是当我尝试更改 View 寻呼机页面时,它会影响 Listview 手势我的意思是 listview 和寻呼机的手势正在一起工作

但我想要那个,如果我正在翻转 viewpgaer 的寻呼机,则 listview 不会滚动是否有任何解决方案

我已经成功地在 ListView 中添加了 View 寻呼机,但请解决这个问题:(。我也在尝试添加手势但没有成功

谢谢

最佳答案

此处可能的解决方案:http://dallasgutauckis.com/2013/04/30/android-putting-a-viewpager-inside-a-listview/

The problem with putting a ViewPager inside a ListView is that it takes only a small amount of vertical (Y) delta before the ListView begins to consume touch events instead of delegating to the child in order to enable the scrolling of the ListView. That’s fine, but the problem often occurs after the ViewPager has already started processing a page change (when dragging horizontally), producing a poor user experience resulting in the improper pagination and scrolling of the two views.

There are a few ways to try to handle this.

  1. 你根本做不到(感谢 Dianne 和 Mark)
  2. 您可以在 ListView 上设置触摸监听器并尝试委托(delegate)触摸基于您认为需要触摸的 View 和根据这些触摸需要采取什么行动。
  3. 您还可以将触摸监听器与 GestureDetector 结合使用,以尝试使实现不那么麻烦。问题是在某些 ListView 中,我们的布局比单项文本行。如果项目布局有例如按钮,然后我们需要正确地将触摸事件委托(delegate)给按钮。这显然对实现有巨大的影响和测试。为了避免上述恶作剧,我将 OnPageChangeListener 添加到 ViewPager 并基于寻呼机的状态,强制从 Activity 进行触摸委托(delegate)。

Contrived example:

class MyActivity extends Activity {
private ViewPager mViewPager;
private View mTouchTarget;

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.example);

mViewPager = (ViewPager) findViewById(R.id.view_pager);

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

private int mPreviousState = ViewPager.SCROLL_STATE_IDLE;

@Override
public void onPageSelected(int position) {
// NO-OP
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// NO-OP
}

@Override
public void onPageScrollStateChanged(int state) {
// All of this is to inhibit any scrollable container from consuming our touch events as the user is changing pages
if (mPreviousState == ViewPager.SCROLL_STATE_IDLE) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
mTouchTarget = mViewPager;
}
} else {
if (state == ViewPager.SCROLL_STATE_IDLE || state == ViewPager.SCROLL_STATE_SETTLING) {
mTouchTarget = null;
}
}

mPreviousState = state;
}
});
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mTouchTarget != null) {
boolean wasProcessed = mTouchTarget.onTouchEvent(ev);

if (!wasProcessed) {
mTouchTarget = null;
}

return wasProcessed;
}

return super.dispatchTouchEvent(ev);
}
}

Now, we have a ViewPager that retains control of touch events once the page starts changing and releases it when paging is idle or settling.

关于android - 如何在 ListView 中使用viewpager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23967633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com