【问题标题】:Android : ViewPager in ScrollView not scrolling if item exceed in ViewPagerAndroid:如果项目超出 ViewPager,则 ScrollView 中的 ViewPager 不滚动
【发布时间】:2016-12-15 06:41:46
【问题描述】:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_first_time_profile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    tools:context="com.vikas.donna.ui.activities.FirstTimeProfileActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="284dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shape_rectangle_fill_gradient_primary_white">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_first_time_profile"
                android:layout_width="match_parent"
                android:layout_height="40dp" />


            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/iv_profile_image"
                android:layout_width="130dp"
                android:layout_height="130dp"
                android:layout_below="@id/toolbar_first_time_profile"
                android:layout_centerHorizontal="true"
                android:src="@drawable/userpic" />

            <com.vikas.donna.customcomponents.customfonts.CustomTextView
                android:id="@+id/tv_profile_name"
                style="@style/font_work_sans_medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/iv_profile_image"
                android:layout_centerHorizontal="true"
                android:text="Vikas Rohilla"
                android:textColor="@android:color/white"
                android:textSize="22sp" />

            <com.vikas.donna.customcomponents.customfonts.CustomTextView
                android:id="@+id/tv_tag_line"
                style="@style/font_work_sans_medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_profile_name"
                android:layout_centerHorizontal="true"
                android:alpha="0.80"
                android:text="I am software engineer"
                android:textColor="@android:color/white"
                android:textSize="11sp" />


            <com.vikas.donna.customcomponents.customfonts.CustomTextView
                android:id="@+id/customTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tv_tag_line"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="12dp"
                android:background="@drawable/shape_rectangle_fill_primary_trans"
                android:drawableLeft="@drawable/red_dot"
                android:drawablePadding="9dp"
                android:paddingBottom="5dp"
                android:paddingLeft="24dp"
                android:paddingRight="24dp"
                android:paddingTop="5dp"
                android:text="Busy in meeting till 5.00 pm"
                android:textColor="@android:color/white"
                android:textSize="12sp" />

        </RelativeLayout>
    </FrameLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_profile"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:tabIndicatorColor="@android:color/transparent"
        app:tabPaddingEnd="0dp"
        app:tabPaddingStart="0dp">

    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager_profile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

如果视图分页器中的片段包含长列表,则它不会滚动。即使在添加 Scrollview 之后,它也会显示静止屏幕。 如果在 ViewPager 子片段中添加 Scrollview 而不是活动,则它仅滚动片段部分而不是上方部分。 我现在该怎么办

【问题讨论】:

  • 您不能使用 ViewPager 和 Scrollview 滚动两侧。它会导致性能问题,也是一种不好的做法。尝试使用嵌套滚动视图或尝试手动处理。
  • 把你的viewpager放在NestedScrollView中
  • 你看到我的回答了吗?你在哪里接触到这个?

标签: android android-fragments android-viewpager scrollview


【解决方案1】:
public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mGestureDetector = new GestureDetector(context, new YScrollDetector());
    setFadingEdgeLength(0);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return super.onInterceptTouchEvent(ev)
            && mGestureDetector.onTouchEvent(ev);
}

// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        if (Math.abs(distanceY) > Math.abs(distanceX)) {
            return true;
        }
        return false;
    }
}
}



public class NestingViewPager extends ViewPager {

public NestingViewPager(final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

public NestingViewPager(final Context context) {
    super(context);
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v != this && v instanceof ViewPager) {
        return true;
    }
    return super.canScroll(v, checkV, dx, x, y);
}
}

【讨论】:

    【解决方案2】:

    据我所知,您已将水平 ViewPager 放在垂直 ScrollView 中,这本身就是一个有问题的排列:手势是要滚动 ViewPager 还是 @ 987654324@? ViewPagerFragment 中的ListView 使问题更加复杂:当用户触摸列表时,焦点应该放在ListView 还是ScrollView

    您可以尝试以下方法之一:

    1. 尝试使用RecyclerViewNestedScrollView 而不是ListViewScrollView

    2. 或者,定义一个interface,在被触摸时将焦点锁定在ListView(反之亦然):

    public interface FocusLock {
        void lock();
        void unlock();
    }
    

    然后创建一个自定义的ListView 并覆盖其dispatchTouchEvent() 方法,如下所示:

    public class NestedListView extends ListView{
    
        private FocusLock mCallbackControl;
    
        public void setCallback(FocusLock callbackControl) {
            this.mCallbackControl = callbackControl;
        }
    
        ....
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    mCallbackControl.lock();
                    break;
    
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mCallbackControl.unlock();
                    break;
            }
    
            return super.dispatchTouchEvent(event);
        }
    }
    

    在您的Fragment 中,您必须按如下方式初始化ListView

    NestedListView mNestedListView = findViewById(R.id.list);
    mNestedListView.setCallback(new FocusLock() {
    
                    @Override
                    public void lock() {
                        mScrollView.requestDisallowInterceptTouchEvent(true);
                    }
    
                    @Override
                    public void unlock() {
                        mScrollView.requestDisallowInterceptTouchEvent(false);
                    }
                });
    

    希望这是有道理的。

    试试这个。其中一种方法应该可行。

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2012-01-12
      • 2018-02-11
      • 1970-01-01
      相关资源
      最近更新 更多