2015-12-17 16 views
19

Mam NestedScrollView zawierający LinearLayout i RecyclerView (oba wewnątrz RelativeLayout).NestedScrollView przewija do góry w widoku Recyclerview resized

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/scroll"> 

    <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/lay_account"> 


     <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/white" 
       android:paddingTop="10dp" 
       android:id="@+id/lay_total" 
       android:paddingBottom="10dp"> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/total" 
        android:id="@+id/lblTotal" 
        android:textSize="@dimen/text_medium" 
        android:layout_marginLeft="20dp" 
        android:textColor="@color/dark_eurakostheme_color" 
        android:textStyle="bold"/> 


      <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical|center_horizontal"> 

       <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/txtTotalAmount" 
         android:textSize="@dimen/text_extra_extra_large" 
         android:gravity="center_horizontal" 
         android:textColor="@color/eurakostheme_color"/> 

       <ImageView 
         android:layout_width="@dimen/icon_extra_extra_large" 
         android:layout_height="match_parent" 
         android:id="@+id/imageView3" 
         android:src="@drawable/coin_eurakos"/> 
      </LinearLayout> 

     </LinearLayout> 

     <View 
       android:layout_width="fill_parent" 
       android:layout_height="1dp" 
       android:id="@+id/divisor" 
       android:background="@color/dividerColor" 
       android:layout_alignBottom="@+id/lay_total"/> 

     <android.support.v7.widget.RecyclerView 
       android:layout_below="@id/lay_total" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/rclBasketAmounts" 
       android:background="@android:color/white" 
       android:padding="10dp"> 
     </android.support.v7.widget.RecyclerView> 

    </RelativeLayout> 

</android.support.v4.widget.NestedScrollView> 
</android.support.design.widget.CoordinatorLayout> 

Po załadowaniu danych do RecyclerView, muszę zmienić jego wysokość programowo tak:

RelativeLayout.LayoutParams lay_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
    50 + ((int) (baskets.length * getResources().getDimension(R.dimen.list_height_small)))); 

lay_params.addRule(RelativeLayout.BELOW, lay_total.getId()); 
recyclerView.setLayoutParams(lay_params); 
recyclerView.setAdapter(new ListBasketAmountAdapter(baskets)); 

Problem jest, gdy dane zakończeniu ładowania zwoje NestedScrollView automatycznie przewija do góry RecyclerView, ukrywając LinearLayout powyżej. Jakieś pomysły?

Dzięki.

+0

Nigdy nie powinno się umieścić 'RecyclerView' w' ScrollView'. –

+1

Czy znalazłeś poprawkę? Mam ten sam problem –

+0

jeszcze nie, wypróbowałem prawie wszystko – kevings14

Odpowiedz

75

Po kilku dniach znalazłem rozwiązanie tego problemu. Trzeba tylko dodać descendantFocusability w pierwszym układzie pod ScrollView tak:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/lay_account" 
    android:descendantFocusability="blocksDescendants"> 
+0

Dziękuję, w moim przypadku po długim dniu wyszukiwania, twoja odpowiedź rozwiązała problem, ale mam pionowy LinearLayout, który zawiera stały rozmiar ImageView (co powoduje ten efekt). –

+1

To rozwiązanie działa, ale należy pamiętać, że w widokach podrzędnych jest 'EditText', wtedy nie będzie można ustawić ostrości. –

10

Rozwiązanie podane przez robót kevings14 ale to nie był pomocny w moim przypadku. Mam RecyclerView + kilka EditText pod NestedScrollView, kiedy dodałem

android:descendantFocusability="blocksDescendants" 

w układzie głównym pod widoku przewijania. Nie byłem w stanie skupić się na EditText.

Potem wpadłem na to rozwiązanie, które zadziałało dla mnie.

<RelativeLayout 
    android:layout_width="match_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:layout_height="wrap_content"> 
2

W moim przypadku, używam wielu RecyclerView wewnątrz NestedScrollView. Wtedy to nie działa dla mnie. Więc moje rozwiązanie jest Kiedy moja zmiana danych kompletny potem po prostu przewinąć do góry,

yourList.setAdapter(yourListAdapter); 

scrollView.post(new Runnable() { 
    @Override 
    public void run() { 
     scrollView.fullScroll(ScrollView.FOCUS_UP); 
     //scrollView.scrollTo(0,0); 
    } 
}); 
Powiązane problemy