gpt4 book ai didi

Android:VerticalViewPager 中的垂直 ScrollView 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 14:13:25 24 4
gpt4 key购买 nike

这是我的 VerticalViewPager:

    public class VerticalViewPager extends ViewPager {

public interface OnTouchListener {
void onTouchDown();
void onTouchUp();
void onTouchMove();
}

private OnTouchListener mListener;

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);
}

public void setOnTouchListener(OnTouchListener listener) {
mListener = listener;
}

private class VerticalPageTransformer implements ViewPager.PageTransformer {

@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 <= 1) { // [-1,1]
view.setAlpha(1);

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

//set Y setNum to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);

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

/**
* 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) {

switch (ev.getAction() & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mListener.onTouchDown();
break;
case MotionEvent.ACTION_UP:
mListener.onTouchUp();
break;
case MotionEvent.ACTION_MOVE:
mListener.onTouchMove();
break;
}

return super.onTouchEvent(swapXY(ev));
}
}

我在这个 ViewPager 中放了一些 fragment 。在最后一个 fragment 中有一个 ScrollView:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

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

...

<ScrollView
android:id="@+id/vertical_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/keyline_4">

....

</LinearLayout>

</ScrollView>

</LinearLayout>

</RelativeLayout>

在最后一个 fragment 中,我可以正常滚动此页面。但是如果我想在 VerticalViewPager 中滑动到上一页,我不能,没有任何反应。

先行人员致谢!

最佳答案

尝试使用 NestedScrollView 而不是 ScrollView

关于Android:VerticalViewPager 中的垂直 ScrollView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38967110/

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