2016-03-05 62 views
0

我在ScrollView中有一个RecyclerView。当我尝试滚动到底部时,RecyclerView(子元素)会在父级开始滚动之前滚动到底部。但是,我想要它,父母应该在孩子开始滚动之前完全滚动。这是我的布局文件。有人能指导我怎么能实现这一目标?如何在父窗口(ScrollView)完全滚动之前禁用子窗口(RecyclerView)滚动

<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> 
+0

我觉得我应该警告你滚动视图内的滚动视图几乎总是会给你Android的问题,应该不惜一切代价避免 –

+0

我已经阅读了很多这样的警告对scrollview内的滚动视图。为什么这被认为是一种不好的做法?我的用例需要我有这样的布局。 – Bhaskar

+0

那么如果上面是你的实际布局,为什么不把'blank_layout'作为回收视图的第一项呢?这当然假设你想让回收者视图填充scrollview,通过你提供的xml,它看起来就像你一样。 如果不是这种情况,为什么它首先必须在scrollview中? –

回答

0

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

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

}

一旦scrollViewReaches底启用recyclerView。搏一搏。如果使用这种方法无法实现您的愿望,请告诉我。

+0

当然,谢谢!我会尽力回复你。 – Bhaskar

0

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

+0

感谢您的回复。我试图找到支持库23.2是否为我启用。但我不相信,如果是的话。我试过谷歌,但无法找到如何确保它是否启用。你能给我一些指点,我该如何检查? – Bhaskar

+0

你是什么意思?如果你使用Android Studio和Gradle,你可以检查你的'build.gradle'文件。 – ianhanniballake

+0

compile'c​​om.android.support:design:23.2' - 我有这个。 – Bhaskar