【问题标题】:View Pager Scrolling not working inside Scroll view in AndroidView Pager Scrolling 在 Android 的 Scroll 视图中不起作用
【发布时间】:2021-02-26 06:34:18
【问题描述】:

我使用带有选项卡布局的视图分页器,每个选项卡都有片段(带有列表视图的片段)。 在视图寻呼机内滚动不起作用。我只想在视图寻呼机内启用滚动功能。 请给我建议。在这里,我还想通知您,Listview 的大小无法修复。

       <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/_25sdp"
            android:fillViewport="true"
            android:paddingHorizontal="@dimen/_10sdp"
            android:paddingTop="@dimen/_15sdp"
            android:scrollbars="none">

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/_10sdp"
                    android:paddingLeft="@dimen/_5sdp"
                    android:text="@string/dashboard"
                    android:textColor="@color/black"
                    android:textSize="@dimen/_14ssp" />

                <GridView
                    android:id="@+id/gv_dashboard"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_175sdp"
                    android:horizontalSpacing="@dimen/_5sdp"
                    android:listSelector="@android:color/transparent"
                    android:numColumns="2"
                    android:scrollbars="none"
                    android:verticalSpacing="@dimen/_5sdp" />

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tl_comp_category"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_25sdp"
                    android:layout_gravity="center"
                    android:layout_marginHorizontal="@dimen/_4sdp"
                    android:layout_marginVertical="@dimen/_10sdp"
                    app:tabBackground="@drawable/tab_background"
                    app:tabIndicator="@android:color/transparent"
                    app:tabMode="auto"
                    app:tabPaddingEnd="@dimen/_5sdp"
                    app:tabPaddingStart="@dimen/_5sdp"
                    app:tabTextAppearance="@style/ComCatTabText" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/vp_tab"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </ScrollView>

【问题讨论】:

  • 您的布局没有为我显示任何内容,可能是因为外部LinearLayout。如果我将其设为android:layout_height="match_parent" 或将其移除时,我会看到布局和功能完善的ViewPager,它可以水平和垂直滚动

标签: android android-fragments android-viewpager android-scrollview android-nestedscrollview


【解决方案1】:

这个解决方案对我有用。

第 1 步:创建一个扩展 viewPager 的类。

第 2 步:在您的 XML 中使用此类

第 3 步:在扩展 FragmentPagerAdapter 的 ViewPager 适配器类中使用 setPrimaryItem() 函数

步骤 - 1

public class CustomViewPager extends ViewPager {
private View mCurrentView;
public CustomViewPager(@NonNull Context context) {
    super(context);
}

public CustomViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mCurrentView == null) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        return;
    }
    int height = 0;
    mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, 
    MeasureSpec.UNSPECIFIED));
    int h = mCurrentView.getMeasuredHeight();
    if (h > height) height = h;
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
  public void measureCurrentView(View currentView) {
     mCurrentView = currentView;
     requestLayout();
 }

 public int measureFragment(View view) {
    if (view == null)
        return 0;
    view.measure(0, 0);
    return view.getMeasuredHeight();
 }
}

步骤 - 2

   <com.appName.widget.CustomViewPager
                    android:id="@+id/vp_tab"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="none"
                    android:overScrollMode="never"/>

步骤 - 3

  public class VPAdapter extends FragmentPagerAdapter {

 //   YOUR OTHER CODE 

 @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
         super.setPrimaryItem(container, position, object);
      if (position != mCurrentPosition) {
        Fragment fragment = (Fragment) object;
        CustomViewPager pager = (CustomViewPager) container;
        if (fragment != null && fragment.getView() != null) {
            mCurrentPosition = position;
            pager.measureCurrentView(fragment.getView());
        }
    }
  }
}

【讨论】:

    【解决方案2】:

    Scroll View 必须仅与孩子一起使用。尝试删除滚动上方的线性

    Documentation about Scroll View

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多