【问题标题】:How to disable child(RecyclerView) scrolling before parent(ScrollView) has completely scrolled如何在父(ScrollView)完全滚动之前禁用子(RecyclerView)滚动
【发布时间】:2016-03-05 05:52:47
【问题描述】:

我在 ScrollView 中有一个 RecyclerView。当我尝试滚动到底部时,RecyclerView(child element) 在父级开始滚动之前滚动到底部。但我想要的是,父母应该在孩子开始滚动之前完全滚动。这是我的布局文件。有人可以指导我如何实现这一目标吗?

<RelativeLayout 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">

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/user_activity_linear_layout">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:id="@+id/blank_layout"
            android:orientation="vertical">
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recently_watched_recycler_view"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:id="@+id/blank_layout"
            android:orientation="vertical">
        </LinearLayout>

    </LinearLayout>

</ScrollView>

</RelativeLayout>

【问题讨论】:

  • 我觉得我应该警告你,在滚动视图中滚动视图几乎总是会给你带来 android 问题,应该不惜一切代价避免
  • 我已经阅读了很多针对滚动视图内的滚动视图的此类警告。为什么它被认为是一种不好的做法?我的用例要求我有这样的布局。
  • 如果上面是你的实际布局,为什么不把blank_layout 作为recycler 视图的第一项呢?这当然假设您希望回收器视图填充滚动视图,根据您提供的 xml,它看起来就像您所做的那样。如果不是这样,为什么它必须首先在滚动视图中?
  • 实际上我的实际布局有一个寻呼机,并且寻呼机的每一页都有 recyclerview 作为其子级。 (为了简单起见,我在这里删除了寻呼机并用 recyclerview 替换)现在,我没有将 recyclerview 内的空白空间作为第一个孩子,因为那样 PagerTabStrip 和 recyclerview 孩子之间会有空白空间。我希望这个空白空间位于 PagerTabStrip 上方。有什么建议吗?
  • 那么,如果你只想要标签条上方的空间,那为什么还需要滚动视图呢?

标签: android android-recyclerview android-scrollview


【解决方案1】:

只需使用NestedScrollView - 它支持在其中包含启用嵌套滚动的视图(例如RecyclerView)。确保您使用的是Support Library 23.2,它允许RecyclerView 根据其内容来衡量自己。

【讨论】:

  • 感谢您的回复。我试图查找是否为我启用了 Support Library 23.2。但我不相信如果是这样。我尝试了谷歌,但找不到如何确保它是否已启用。你能指点我如何检查吗?
  • 什么意思?如果您使用的是 Android Studio 和 Gradle,请检查您的 build.gradle 文件。
  • 编译 'com.android.support:design:23.2' - 我有这个。
  • 依赖是'com.android.support:design:23.2.0'
  • 是的,抱歉,打错了。谢谢。我现在就试试。
【解决方案2】:

禁用 RecyclerView。扩展滚动视图,以便您知道 ot 何时到达底部。

public class myScrollView extends ScrollView
{
    public myScrollView(Context context)
    {
        super(context);
    }
    public myScrollView(Context context, AttributeSet attributeSet)
    {
        super(context,attributeSet);
    }

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
    View view = (View)getChildAt(getChildCount()-1);
    int d = view.getBottom();
    d -= (getHeight()+getScrollY());
    if(d==0)
    {
        //you are at the end of the list in scrollview 
        //do what you wanna do here
    }
    else
        super.onScrollChanged(l,t,oldl,oldt);
}

}

一旦你的scrollViewReachs 底部启用recyclerView。搏一搏。如果您无法使用此方法达到您想要的效果,请告诉我。

【讨论】:

  • 好的,谢谢!我会尽力回复你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
相关资源
最近更新 更多