【问题标题】:android: RecyclerView inside a ScrollView (or parallax)android:ScrollView(或视差)内的 RecyclerView
【发布时间】:2015-01-20 15:03:45
【问题描述】:

我有一个包含 2 个卡片视图的片段,其中包含多个控件。

下面我有一个recyclerview的第二个cardview,这个工作完美

问题在于 recyclerview 从屏幕的最底部开始,并且滚动 recyclerview 非常

以前使用过列表视图,这让我适合您的内容,因此可以立即在屏幕上滚动,但使用 recycllerview 不能。

如何让我在recyclerview中滚动时,控件向上像视差效果?

编辑:更清楚,想象一个片段中有 2 个卡片视图,它们占据了 80% 的屏幕,在这些之上,一个包含 100 个项目的列表的 recyclerview,但是滚动是如此之小......列表视图让我适应到“内容”并一次滚动整个屏幕。

这是我的 XML 布局:

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/cvCampos1"
            layout="@layout/ctlcabeceralibreta" />

        <include
            android:id="@+id/cvCampos2"
            layout="@layout/ctlcabeceralibreta2" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRegistros"
            android:layout_width="fill_parent"
            android:layout_height="1000dp"
            android:layout_below="@id/cvCampos2"
            android:scrollbars="vertical" />
    </RelativeLayout>

</ScrollView>

这是截图:

【问题讨论】:

  • 请清楚一点,因为不清楚实际问题是什么
  • 想象一个片段中有2个cardview,它们占据了80%的屏幕,下面是一个recyclerview,有100个项目的列表,但是滚动太小了……listview让我适应“内容”并立即滚动整个屏幕。
  • 为recylerview设置一个最小高度?
  • 或者在所有设置的 recyclerView 高度周围放置一个滚动视图来包装内容。并使用以下方法禁用回收器视图的滚动: .setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } });
  • 是的,现在我已经使用并在所有尝试过的设备上工作的解决方案是将属性放在 recyclerview android:layout_height = "wrap_content"(在 ScrollView 内)然后使用它用于recyclerviewstackoverflow.com/a/27616854/518657的LinearLayoutManager,它计算item的高度,唯一的问题是所有item的高度必须相同。如果有人对此问题有更优雅的解决方案,请发布,不可能没有解决这个非常基本的问题。

标签: android android-layout android-appcompat android-recyclerview android-cardview


【解决方案1】:

在上述情况下,您需要做的是在同一个recyclerview 中使用多个ViewHolders

我看到你在那里有三种类型的布局。 recyclerview 中的前两张卡片和其他布局。

RecyclerView with multiple View Types 为您提供了如何在一个简单的回收器视图中使用多个视图类型的示例。

【讨论】:

    【解决方案2】:

    放置一个固定高度的作品!但我需要计算高度 列表中的项目数

    这适用于列表视图(我没有使用 recyclerview 对其进行测试)

    public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }
    
            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                }
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }
    

    来源:

    Full example

    【讨论】:

    • 是的,这是我在使用listview时在我的应用程序中使用的功能,但现在我使用了recyclerview,并没有找到与此功能等效的功能。
    • 你能把你的布局xml和截图添加到你的问题中吗..请
    • 你应该说这个答案was not written by you
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2016-04-18
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多