【问题标题】:how to make ViewPager and gridview scroll vertical at a same time如何使 ViewPager 和 gridview 同时垂直滚动
【发布时间】:2020-03-25 04:30:38
【问题描述】:

我的活动中有 ViewPager 和网格视图。当我在屏幕上的任何位置滚动时,ViewPager 和网格视图应该同时滚动。这是我的 ss :image。现在只有少数图像显示在网格视图我有 63 张图片,但在网格中只显示了 9 张..2)ss main

xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginBottom="8dp"/>


<LinearLayout
    android:id="@+id/SliderDots"
    android:layout_below="@+id/viewPager"
    android:orientation="horizontal"
    android:gravity="center_vertical|center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="15dp"/>


<GridView
    android:id="@+id/mGridView"
    android:layout_width="match_parent"
    android:layout_height="554dp"
    android:background="#ffffff"
    android:columnWidth="172dp"
    android:gravity="center"
    android:horizontalSpacing="5dp"
    android:numColumns="auto_fit"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingRight="5dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp"
    />
</LinearLayout>
 </androidx.core.widget.NestedScrollView>

【问题讨论】:

  • 您不想在 ViewPager 中使用 GridView 吗?而不是下
  • 不,我想要它们分开......并同时垂直滚动......看看我添加的截图链接
  • 嘿,我编辑了我的代码..现在网格中只显示了几张图像,但滚动工作......但我希望显示图像
  • 但我希望所有图像都显示在网格中
  • 那你为什么要使用 ViewPager 呢?

标签: android android-studio android-layout android-viewpager scrollview


【解决方案1】:

使用此代码解决您的问题

<androidx.core.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="wrap_content">


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

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="8dp"/>


<LinearLayout
android:id="@+id/SliderDots"
android:layout_below="@+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="15dp"/>


<GridView
android:id="@+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:columnWidth="172dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />

</LinearLayout>

</androidx.core.widget.NestedScrollView>

【讨论】:

  • 不行不行...现在开始两个图像的网格视图只会显示
  • android:fastScrollEnabled="true" 在嵌套滚动视图中
  • 在嵌套滚动视图下?
  • 您好,如果网格视图不起作用,您可以使用 recyclerview 网格视图,它可以正常工作
  • recyclegridview?我认为网格视图本身是一个回收视图,因为它使用适配器...
【解决方案2】:

您必须使用 CUSTOM viewpager 来实现 viewpager 的垂直滚动,因为 default viewpager 不支持与其他组件垂直滚动。

检查我下面的代码。

public class CustomViewPager extends ViewPager {

    private View mCurrentView;

    public CustomViewPager(Context context) {
        super(context);
    }

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

    @Override
    public 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.AT_MOST);

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

将这个 Class 而不是 default ViewPager 应用到您的片段中。就是这样!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2011-08-07
    • 1970-01-01
    • 2017-12-06
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多