how to make ViewPager and gridview scroll vertical

2020-05-05 18:16发布

问题:

I have ViewPager and grid view in my activity.i want when I scroll anywhere on screen both ViewPager and grid view should scroll at a same time.here is my ss :image.now the only few images are displayed in grid view I have 63 images but shows only 9 in grid..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>

回答1:

Use this code to resolve your problem

<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>


回答2:

You must be use CUSTOM viewpager to achieve vertical scrolling of viewpager because default viewpager doesn't support vertical scrolling with other components.

Check my below code.

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

Apply this Class instead of default ViewPager into your fragment. THAT'S IT!!