可以使view pager和gridview一起滚动 [英] view pager and gridview can be made scrollable together

查看:46
本文介绍了可以使view pager和gridview一起滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中有ViewPager和grid视图.现在只有View视图滚动到ViewPager页面的下方.如何使两者都垂直滚动?我使用嵌套可滚动但没有用. >

布局:

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

以上代码有效: 两者都可以正常工作...但是我只能滚动gridview项,垂直滚动,使ViewPager保持在其位置(不向上).我在nestedscrollview中需要帮助,但没有任何帮助,所以对我有帮助.

解决方案

在您的 MainActivity.java 类方法onCreate中编写代码:

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

在您的activity_main.xml文件中实现该功能.

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

创建自定义类,该类是 MyGridView.java 文件:

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

根据我的代码,我的 ImageAdapter.java 类:

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

解决方案

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.

这篇关于可以使view pager和gridview一起滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆