2015-11-24 24 views
5

Tôi đang triển khai RecyclerView bên trong một ScrollView. Để chỉ có một hành vi cuộn trên toàn bộ trang, tôi triển khai phiên bản NonScrollRecyclerView. Việc thực hiện như sau:Non Scroll RecyclerView vấn đề cuộn trong SDK Android 23

public class NonScrollRecyclerView extends RecyclerView { 
    public NonScrollRecyclerView(Context context) { super(context); } 

    public NonScrollRecyclerView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public NonScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev){ 

     if(ev.getAction() == MotionEvent.ACTION_MOVE) 
      return true; 

     return super.dispatchTouchEvent(ev); 
    } 
} 

Khi tôi cập nhật xây dựng và mục tiêu của tôi thiết lập để SDK 23, tôi gặp khó khăn khi di chuyển các trang chứa NonScrollRecyclerView. Vấn đề cụ thể là trang cuộn OK cho đến khi tôi đạt đến phần xem trình tái chế và khi tôi cuộn lên chế độ xem này, tôi không thể cuộn nữa, lên hoặc xuống.

tôi donot phải đối mặt với vấn đề này với SDK 22 và dưới

xml của tôi là như sau:

XML@layout/rv chứa quan điểm tái chế

<ScrollView 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:background="@color/background_gray"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/background_gray" 
    android:orientation="vertical"> 

    <include 
     layout="@layout/row_mall_header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_header" 
     android:layout_marginTop="8dp"/> 

    <include 
     layout="@layout/row_mall_shops" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_shops" 
     android:layout_marginTop="8dp"/> 

    <include 
     layout="@layout/row_mall_coupons" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_coupons" 
     android:layout_marginTop="8dp"/> 

    <include 
     layout="@layout/rv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/cv_mall_feeds" 
     android:layout_marginTop="8dp"/> 

    </LinearLayout> 
</ScrollView> 

XML - @ layout/rv

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/background_gray" 
    android:id="@+id/ll_mall_feeds"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" 
    android:paddingTop="6dp" 
    android:paddingBottom="6dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tv_feedcount" 
     android:textColor="@color/semi_theme_blue" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="12dp" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

<com.project.ui.NonScrollRecyclerView 
    android:id="@+id/nrv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:divider="@android:color/transparent" 
    android:listSelector="@android:color/transparent" /> 

</LinearLayout> 
+2

Tại sao bạn không chỉ sử dụng LinearLayout thay vì RecyclerView? Về cơ bản, bạn đang chuyển RecyclerView thành LinearLayout, nhưng với nhiều chi phí hơn. –

+0

Đồng ý với nhận xét ở trên, chỉ cần loại bỏ recyclerView và nếu bạn xem xét điều này, bạn sẽ không hỏi về các vấn đề bố cục xml nữa http: // stackoverflow.com/questions/29956014/why-should-we-use-xml-layouts – Nanoc

+0

bạn có thể tạo quản lý bố cục tuyến tính riêng theo yêu cầu của bạn ... kiểm tra liên kết này https://github.com/serso/android-linear- layout-manager/blob/master/lib/src/main/java/org/solovyev/android/views/llm/LinearLayoutManager.java –

Trả lời

2

RecyclerView và ListView không được đề xuất bên trong một ScrollView vì các giá trị phần tử không được tính khi hiển thị ScrollView. Điều này có nghĩa, bộ điều hợp của bạn có thể không được điền khi ScrollView được hiển thị và sau đó khi RecyclerView được thông báo về các thay đổi dữ liệu (ví dụ khi bạn khởi tạo bộ điều hợp), không có cách nào tính toán lại chiều cao của các phần tử. Nó thực sự là một nỗi đau ở cổ bởi vì bạn phải cố gắng để tính toán các yếu tố chiều cao và nó không bao giờ chính xác, vì vậy bạn sẽ có sự khác biệt khi hiển thị một ListView hoặc RecyclerView bên trong một ScrollView. Một số ví dụ về cách tính chiều cao của các phần tử có thể được kiểm tra herehere.

Lời khuyên của tôi là để biến RecyclerView của bạn thành một LinearLayout và thêm yếu tố lập trình, vì vậy bạn giả lập các ListView hoặc RecyclerView hành vi:

LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.files); 
layout.removeAllViews(); 

for (int i = 0; i < fileAdapter.getCount(); i++) { 
    final View item = fileAdapter.getView(i, null, null); 
item.setClickable(true); 

    item.setId(i); 

    item.setOnClickListener(new View.OnClickListener() { 
     @Override 
      public void onClick(View v) { 

       fileContentRowPosition = v.getId(); 

     # Your click action here 


    } 
}); 


layout.addView(item); 

} 

Đây XML của nó với các định nghĩa tập tin:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

... 

    <LinearLayout 
     android:id="@+id/files" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:orientation="vertical"> 
    </LinearLayout> 


</ScrollView> 

Toàn bộ mã java có thể được kiểm tra here và toàn bộ bố cục here.

Mặt khác, nếu bạn vẫn muốn tiếp tục với thực hiện của bạn, và liên quan đến vấn đề của bạn, bạn có thể kiểm tra bài viết này về Handling Scrollable Controls in Scrollview

Trân trọng,

1

Đối với chỉ có một di chuyển trong toàn bộ trang bạn có thể sử dụng NestedScrollView thay vì ScrollView và đặt recyclerView.setNestedScrollingEnabled (false);

Các vấn đề liên quan