【问题标题】:view pager and gridview can be made scrollable togetherview pager 和 gridview 可以一起滚动
【发布时间】:2019-12-04 13:55:34
【问题描述】:

我的布局中有 ViewPager 和网格视图。现在只有网格视图在 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中的帮助但没有任何工作所以帮助我。

【问题讨论】:

  • 请提供截图。
  • 我加了ss看看
  • 单张图片也可以做水平滚动视图,而 GridView 可以做垂直滚动视图。我说得对吗!
  • 请看我的代码希望它对你有帮助,如果你有任何问题请问我。

标签: android android-studio android-layout scrollview


【解决方案1】:

在你的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;
    }

}

希望对你有帮助...!继续编码。

【讨论】:

  • 您找到答案或需要帮助了吗?
  • 嘿..实际上我的gridview适配器扩展了baseadapter..我在baseadapter的getView()下获取数据..当我扩展gridview时我应该如何获取我的数据?
【解决方案2】:

要垂直滚动查看分页器和网格,可以使用recyclerview 代替gridview :Please check this stack answer

在 XML 和 java 文件中尝试以下代码: 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="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 文件:

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

希望对你有用

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多