2015-12-17 20 views
19

Tôi có một NestedScrollView chứa LinearLayout và RecyclerView (cả bên trong RelativeLayout).NestedScrollView cuộn lên đầu trên Recyclerview được đổi kích thước

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

Sau khi tải dữ liệu vào RecyclerView, tôi cần phải thay đổi nó chiều cao theo chương trình như thế này:

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

Vấn đề là khi dữ liệu đã tải xong cuộn NestedScrollView tự động cuộn lên đầu RecyclerView, ẩn LinearLayout phía trên nó. Bất kỳ ý tưởng?

Cảm ơn.

+0

Bạn không bao giờ nên đặt một 'RecyclerView' trong một' ScrollView'. –

+1

Bạn có tìm thấy bản sửa lỗi cho điều này không? Tôi gặp vấn đề tương tự –

+0

chưa, đã thử hầu như mọi thứ – kevings14

Trả lời

75

Sau vài ngày tôi đã tìm thấy giải pháp cho sự cố. Bạn chỉ cần thêm descendantFocusability trong bố trí đầu tiên thuộc ScrollView như thế này:

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

Cảm ơn bạn, trong trường hợp của tôi sau một ngày dài tìm kiếm, câu trả lời của bạn đã giải quyết được vấn đề, nhưng tôi có LinearLayout thẳng đứng chứa ImageView có kích thước cố định (Điều này gây ra hiệu ứng này). –

+1

Giải pháp này hoạt động nhưng hãy nhớ nếu có một 'EditText' trong chế độ xem con thì nó sẽ không thể tập trung được. –

10

Các giải pháp được đưa ra bởi công trình kevings14 nhưng nó không phải là hữu ích trong trường hợp của tôi. Tôi đã một RecyclerView + một vài EditText dưới NestedScrollView, khi tôi thêm

android:descendantFocusability="blocksDescendants" 

trong cách bố trí chính dưới xem di chuyển. Tôi không thể tập trung EditText.

Sau đó, tôi đã đưa ra giải pháp này, đã làm việc cho tôi.

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

Trong trường hợp của tôi, tôi sử dụng nhiều RecyclerView bên NestedScrollView. Sau đó, nó không hoạt động đối với tôi. Vì vậy, giải pháp của tôi là, Khi cập nhật dữ liệu hoàn chỉnh của tôi sau đó tôi chỉ cần di chuyển lên trên,

yourList.setAdapter(yourListAdapter); 

scrollView.post(new Runnable() { 
    @Override 
    public void run() { 
     scrollView.fullScroll(ScrollView.FOCUS_UP); 
     //scrollView.scrollTo(0,0); 
    } 
}); 
Các vấn đề liên quan