【问题标题】:android layout scrolling with gridview and RecyclerViewandroid布局滚动与gridview和RecyclerView
【发布时间】:2016-06-01 03:56:34
【问题描述】:

我有 RecyclerView 水平滚动和 gridview 垂直滚动的布局。 但 recyclerview 不会在活动中向上滚动,让 gridview 在水平 recyclerview 列表下方垂直滚动。

我希望在整个布局垂直滚动时水平滚动 recyclerview。

content_main.xml

<LinearLayout 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="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.test.MainActivity"
tools:showIn="@layout/app_bar_dash_board"
android:orientation="vertical"
android:scrollbars="vertical">
        <android.support.v7.widget.RecyclerView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:orientation="horizontal"
            android:scrollbars="none" />
        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:verticalSpacing="0dp"
            android:horizontalSpacing="0dp"
            android:stretchMode="columnWidth"
            android:scrollbars="none"
            android:numColumns="2"
            />
</LinearLayout>

grid_view.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>
<TextView
    android:id="@+id/grid_item_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:layout_gravity="bottom"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:background="#55000000"/>
</FrameLayout>

【问题讨论】:

    标签: android android-layout android-studio android-recyclerview


    【解决方案1】:

    为什么在回收站视图中使用 gridview。它不会正常运行。如果你想要一个网格视图,你可以使用 recyclerview 作为网格视图。 将您的回收站视图布局管理器设置为 StaggeredGridLayoutManager。像这样使用它。

    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
    

    请回复https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html

    【讨论】:

      【解决方案2】:

      您的主要布局应该是滚动视图,并且您的回收视图高度不应与父级匹配。

      您需要根据孩子的数量动态设置网格视图的高度。

      【讨论】:

        猜你喜欢
        • 2021-05-17
        • 1970-01-01
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-13
        • 2016-12-02
        • 2021-07-09
        相关资源
        最近更新 更多