2016-03-05 64 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