在布局中使用多个 RecyclerViews 进行 Scolling [英] Scolling with multiple RecyclerViews in Layout

查看:24
本文介绍了在布局中使用多个 RecyclerViews 进行 Scolling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含两个 RecyclerViews 的布局.一个水平滚动,而另一个垂直滚动.我可以在每个 RecyclerView 内正确滚动,但整个页面不会滚动,即顶部 RecyclerView 始终保持在顶部,底部保持在底部,就像两者都固定在适当的位置.

I have created a layout which has two RecyclerViews. One scrolls horizontally while other scrolls vertically. I can scroll correctly inside each RecyclerView but the page as a whole won't scroll i.e. top RecyclerView stays at top always and bottom one stays at bottom like both are fixed in position.

以下是我的xml布局:

Following is my xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <EditText
            android:id="@+id/search"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:hint="Search Dramas"
            android:textSize="16sp"
            android:paddingLeft="10dp"
            android:gravity="center"
            android:textColor="@color/dark_grey"
            android:textColorHint="@color/dark_grey"
            android:background="@drawable/border_bottom"/>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/border_bottom_background_black"
            android:textColor="@color/white"
            android:padding="10dp"
            android:text="Most Watched"/>

        <!-- A RecyclerView to display horizontal list -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/featured"
            android:scrollbars="none"
            android:layout_width="fill_parent"
            android:layout_height="240dp"
            android:paddingLeft="0dp"
            android:paddingRight="15dp"
            android:paddingTop="15dp"
            android:paddingBottom="25dp"
            android:background="@color/black"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/border_bottom_backgroundless"
            android:textColor="@color/dark_grey"
            android:padding="10dp"
            android:text="All Dramas"/>

        <!-- A RecyclerView to display vertical list -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/pick_item"
            android:scrollbars="vertical"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"/>


</LinearLayout>

推荐答案

我能够通过将所有内容放在 ScrollView 中,然后手动/以编程方式设置垂直 RecyclerView 的高度来解决问题.

I was able to solve the problem by putting everything inside ScrollView and then setting height of vertical RecyclerView manually/programmatically.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

            <EditText
                android:id="@+id/search"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:hint="Search Dramas"
                android:textSize="16sp"
                android:paddingLeft="10dp"
                android:gravity="center"
                android:textColor="@color/dark_grey"
                android:textColorHint="@color/dark_grey"
                android:background="@drawable/border_bottom"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/border_bottom_background_black"
                android:textColor="@color/white"
                android:padding="10dp"
                android:text="Most Watched"/>

            <!-- A RecyclerView to display horizontal list -->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/featured"
                android:scrollbars="none"
                android:layout_width="fill_parent"
                android:layout_height="240dp"
                android:paddingLeft="0dp"
                android:paddingRight="15dp"
                android:paddingTop="15dp"
                android:paddingBottom="25dp"
                android:background="@color/black"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/border_bottom_backgroundless"
                android:textColor="@color/dark_grey"
                android:padding="10dp"
                android:text="All Dramas"/>

            <!-- A RecyclerView to display list -->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/pick_item"
                android:paddingBottom="20dp"
                android:scrollbars="vertical"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:minHeight="840dp"/>


    </LinearLayout>
</ScrollView>

以编程方式设置 RecyclerView 的高度:

Setting RecyclerView's height programmatically:

LinearLayout.LayoutParams params = new     
     LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
     ViewGroup.LayoutParams.WRAP_CONTENT);
// calculate height of RecyclerView based on child count
params.height=1150;
// set height of RecyclerView
recyclerView.setLayoutParams(params);

这篇关于在布局中使用多个 RecyclerViews 进行 Scolling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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