6

tôi muốn đặt một RecylerView bên NestedScrollView như sauRecylerview không nhìn thấy được bên trong scrollview hoặc nestedScrollview

activity_service_menu.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="HELLO" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/rv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" /> 
    </LinearLayout> 
</android.support.v4.widget.NestedScrollView> 

ServiceMenuActivity.java

public class ServiceMenuTActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_service_menu_t); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     RecyclerView rv = (RecyclerView) findViewById(R.id.rv); 
     rv.setLayoutManager(new LinearLayoutManager(getApplicationContext())); 
     rv.setHasFixedSize(true); 
     rv.setAdapter(new RvAdapter()); 
    } 

    private static class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHolder> { 

     @Override 
     public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View serviceMenuItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_service_menu, parent, false); 
      return new RvHolder(serviceMenuItemView); 
     } 

     @Override 
     public void onBindViewHolder(RvHolder holder, int position) { 

     } 

     @Override 
     public int getItemCount() { 
      return 100; 
     } 

     public static class RvHolder extends RecyclerView.ViewHolder { 

      public RvHolder(View itemView) { 
       super(itemView); 
      } 
     } 
    } 

} 

Tôi đã đặt LinearLayout bên trong scrollView và nestedScrollView. Nhưng RecyclerView không hiển thị. Nếu tôi thay thế ScrollView bằng FrameLayout hoặc bất kỳ bố cục nào khác, thì RecyclerView sẽ hiển thị.

Tôi muốn sử dụng nestedScrollView và cuộn tổng bố cục khi recyclerView được cuộn. Thật không may recyclerView thậm chí không nhìn thấy được.

+0

bạn có thực sự cần 'LinearLayout' như cha mẹ của 'RecyclerView' bên' NestedScrollView'? –

+0

Không. Vì ScrollView chỉ chấp nhận 1 con, tôi đã đặt LinearLayout làm trình bao bọc. Tôi cần RecyclerView bên dưới TextView và khi RecyclerView được cuộn, toàn bộ khung nhìn sẽ cuộn. – Narendra

+0

bạn có thể mô tả cơ bản loại hành vi nào bạn muốn không? như Thu gọn Thanh công cụ hay bất kỳ thứ gì khác. –

Trả lời

11

Làm theo mẫu này sẽ nhận được ý tưởng bạn đã làm sai. Dòng chính là: android: fillViewport = "true".

<android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:paddingTop="24dp"> 


     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="16dp"> 


     <com.app.view.CustomRecyclerView 
      android:id="@+id/recycler_movie_suggestion" 
      android:layout_width="match_parent" 
      android:layout_height="170dp" 
      android:fillViewport="true" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    </android.support.v7.widget.CardView> 

    </LinearLayout> 

    </android.support.v4.widget.NestedScrollView> 
+2

Bạn là người tuyệt vời . Có vẻ như thiết lập fillViewport = "true" trên NestedScrollView đã làm các trick. Cảm ơn bạn! – Pkmmte

+0

Đây là giải pháp duy nhất có hiệu quả đối với tôi, tôi đã thử một giải pháp từ những câu hỏi liên quan nhưng không ai làm việc. – UrielUVD

0

khi bạn sử dụng hai phần tử cuộn bên trong nhau bạn đang ở trong nước nóng! bạn phải tính toán chiều cao của vật phẩm Recycler và sau đó tìm toàn bộ chiều cao của bộ tái chế. nhìn vào liên kết dưới đây, tôi giải thích hoàn toàn vấn đề này.

Use RecyclerView insdie ScrollView with flexible recycler item height

Tôi hy vọng nó giúp bạn

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