view pager and gridview can be made scrollable together

半腔热情 提交于 2019-12-25 17:46:12

问题


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.


回答1:


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.




回答2:


To vertical scroll view pager and grid together,you can use the recyclerview instead of gridview :Please check this stack answer

Try below code in XML and java file: XML file:

<?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="match_parent"
    tools:context=".ViewPagerActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <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"/>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            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">
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

Java file:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(new RecyclerViewAdapter(this,20));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setFocusable(false);

I hope it works for you



来源:https://stackoverflow.com/questions/59177661/view-pager-and-gridview-can-be-made-scrollable-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!