gpt4 book ai didi

android - Viewpager 滚动问题中的 Viewpager

转载 作者:行者123 更新时间:2023-11-29 02:28:50 27 4
gpt4 key购买 nike

在我的主要 Activity 中,我有一个水平 viewpager。在 viewpager 的一个 fragment 中,我有另一个垂直 viewpager。两者都很好。但是对于水平 viewpager,我需要从屏幕边缘滚动。但我希望它可以从屏幕的任何位置滚动。

我的水平 viewpager 设置:

viewPager = (ViewPager) findViewById(R.id.viewpager);
adapterViewPager = new HorizontalViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapterViewPager);
viewPager.setCurrentItem(1);

水平 viewpager 适配器:

public class HorizontalViewPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;

public HorizontalViewPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return MoreFragment.newInstance("Fragment MoreFragment", "HA HA HA");
case 1:
return NewsFragment.newInstance("Fragment 1", "HA HA HA");
case 2:
return WebViewFragment.newInstance("Fragment Webview", "HA HA HA");
default:
return null;
}
}

@Override
public int getCount() {
return NUM_ITEMS;
}

@Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
}

在我的新闻 fragment 中,我有另一个 viewpager。

viewPager = view.findViewById(R.id.shortpager);
adapterViewPager = new VerticalPagerAdapter(getActivity().getSupportFragmentManager(), newsList);
viewPager.setAdapter(adapterViewPager);

我的垂直 viewpager 类:

public class VerticalViewPager extends ViewPager {

public VerticalViewPager(Context context) {
super(context);
init();
}

public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}

/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();

float newX = (ev.getY() / height) * width;
float newY = (ev.getX() / width) * height;

ev.setLocation(newX, newY);

return ev;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}

private class VerticalPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;

@Override
public void transformPage(View view, float position) {

if (position <= -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);

} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left/top page
view.setAlpha(1);
ViewCompat.setElevation(view, 1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
view.setTranslationY(0);

//set Y position to swipe in from top
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);

} else if (position <= 1) { // [0,1]
view.setAlpha(1);
ViewCompat.setElevation(view, 2);

// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
view.setTranslationY(position * view.getHeight());

// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(1);
view.setScaleY(1);

} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}

}
}

}

垂直 viewpager 适配器:

public class VerticalPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;
private final ArrayList<News> newsList;

public VerticalPagerAdapter(FragmentManager fm, ArrayList<News> newsList) {
super(fm);
this.newsList = newsList;
}

@Override
public Fragment getItem(int position) {
return ShortFragment.newInstance("Fragment 1", "HA HA HA", newsList.get(position));
}

@Override
public int getCount() {
return this.newsList.size();
}

@Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
}

垂直 viewpager 滚动流畅。目前对于水平 viewpager,我必须从屏幕边缘滚动。但我需要让它在屏幕的任何地方像垂直滚动一样滚动。

最佳答案

您的代码有问题是您的 VerticalViewPager

如果你注意到代码,

@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}

第 1 步,使用交换手势调用 super。
第 2 步,使用当前手势在垂直 View 寻呼机上执行操作。

如果您特别看到注释,//将触摸坐标返回到任何 subview 的原始引用框架,这清楚地表明手势首先由 subview 使用。

因此在您的垂直 View 寻呼机中,由于您正在交换手势,它甚至在将水平手势传递给父级之前就在使用水平手势。这就是为什么你的水平 View 寻呼机没有得到手势。

如果您将 Vertical View Pager 与任何冲突的手势实现(如 ScrollView、Lists、RecyclerView、ViewPager 等)一起使用,请使用以下链接中的一个。它已被弃用,但适用于所有手势冲突 View 。

https://github.com/kaelaela/VerticalViewPager

关于android - Viewpager 滚动问题中的 Viewpager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50737842/

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