Search code examples
androidandroid-studioandroid-layoutscrollview

view pager and gridview can be made scrollable together


I have ViewPager and grid view in my layout.for now only grid view is scrolling below the page ViewPager.how can I make both scrollable as vertical ?I used nested scrollable but nothing worked..so guide me..

layout:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity"
xmlns:android="http://schemas.android.com/apk/res/android">
 <androidx.viewpager.widget.ViewPager
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:id="@+id/viewpagermain"
    android:layout_gravity="center_horizontal">

  </androidx.viewpager.widget.ViewPager>

<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:paddingTop="5dp"
  android:paddingLeft="5dp"
  android:paddingRight="5dp"
  android:numColumns="auto_fit"
  android:columnWidth="172dp"
  android:horizontalSpacing="5dp"
  android:verticalSpacing="5dp"
  android:gravity="center"
  android:stretchMode="columnWidth"
  android:background="#ffffff">
</GridView>
</LinearLayout>

above code working: both are working fine individually...but I can scroll only gridview items scroll vertical where ViewPager remain at its position (not going up).I need help in nestedscrollview I have but nothing worked so help me.


Solution

  • In your MainActivity.java class method onCreate write a code:

    GridView gridView = (GridView) findViewById(R.id.grid_view);
    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));
    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    // Create ScreenSlidePagerAdapter class and then implement viewPager.
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(pagerAdapter);
    

    In your activity_main.xml file to implement this thing.

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity4">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            android:orientation="vertical">
            <android.support.v4.view.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="140sp"
                android:layout_marginBottom="10dp"
                android:layout_gravity="center_horizontal"/>
            <com.xx.xxx.MyGridView
                android:id="@+id/grid_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="auto_fit"
                android:columnWidth="90dp"
                android:horizontalSpacing="10dp"
                android:verticalSpacing="10dp"
                android:gravity="center"
                android:stretchMode="columnWidth"
                android:layout_marginBottom="10dp"/>
        </LinearLayout>
    </ScrollView>
    

    Create custom class which is MyGridView.java file:

    public class MyGridView extends GridView {
        public MyGridView(Context context) {
            super(context);
        }
    
        public MyGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyGridView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int heightSpec;
    
            if (getLayoutParams().height == LayoutParams.MATCH_PARENT) {
                // The great Android "hackatlon", the love, the magic.
                // The two leftmost bits in the height measure spec have
                // a special meaning, hence we can't use them to describe height.
                heightSpec = MeasureSpec.makeMeasureSpec(
                        Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            }
            else {
                // Any other height should be respected as is.
                heightSpec = heightMeasureSpec;
            }
    
            super.onMeasure(widthMeasureSpec, heightSpec);
        }
    }
    

    Edited:

    As per my code my ImageAdapter.java class :

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
    
        // Keep all Images in array
        public Integer[] mThumbIds = {
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download, R.drawable.download,
                R.drawable.download
        };
    
        // Constructor
        public ImageAdapter(Context c){
            mContext = c;
        }
    
        @Override
        public int getCount() {
            return mThumbIds.length;
        }
    
        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;
        }
    
    }
    

    I hope it'll help you...! keep coding.

    enter image description here