2015-03-05 23 views
6

Tôi cố gắng hiển thị lồng nhau recyclerview nhưng các mục con không hiển thị. Tôi muốn hiển thị các mục khác nhau trong tất cả chế độ xem con. Tôi không gặp lỗi, nhưng chế độ xem không được làm mới.android recested recyclerview

Đây là mã của tôi có thể giúp bạn.

Cảm ơn

public class MainActivity extends ActionBarActivity { 

    RecyclerView recyclerView; 
    RootAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     adapter = new RootAdapter(this); 
     recyclerView = (RecyclerView) findViewById(R.id.recyclerRoot); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     recyclerView.setAdapter(adapter); 

    } 

private class RootAdapter extends RecyclerView.Adapter<RootAdapter.RootViewHolder> { 

     private final LayoutInflater inflater; 
     String[] _items = new String[]{"ITEM 1", "ITEM 2", "ITEM 3", "ITEM 4"}; 
     public RootAdapter(Context context) 
     { 
      inflater = LayoutInflater.from(context); 
     } 

     @Override 
     public RootViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
      View view = inflater.inflate(R.layout.root_row, viewGroup, false); 
      RootViewHolder rvi = new RootViewHolder(view); 
      return rvi; 
     } 

     @Override 
     public void onBindViewHolder(RootViewHolder rootViewHolder, int i) { 
      rootViewHolder.txtRootLine.setText(_items[i]); 
      rootViewHolder.recyclerViewChild.setLayoutManager(new LinearLayoutManager(inflater.getContext())); 
      rootViewHolder.recyclerViewChild.setAdapter(new ChildAdapter(inflater)); 
     } 

     @Override 
     public int getItemCount() { 
      return _items.length; 
     } 

     class RootViewHolder extends RecyclerView.ViewHolder { 
      TextView txtRootLine; 
      RecyclerView recyclerViewChild; 
      public RootViewHolder(View itemView) { 
       super(itemView); 
       txtRootLine = (TextView) itemView.findViewById(R.id.txtRootLine); 
       recyclerViewChild = (RecyclerView) itemView.findViewById(R.id.recyclerChild); 
      } 
     } 
    } 


private class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ChildViewHolder> { 
     private LayoutInflater _inflater; 
     String[] _childItems = new String[]{"child 1", "child 2", "child 2"}; 
     public ChildAdapter(LayoutInflater inflater) { 
      _inflater = inflater; 
     } 

     @Override 
     public ChildViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
      View view = _inflater.inflate(R.layout.child_row, viewGroup, false); 
      ChildViewHolder rvi = new ChildViewHolder(view); 
      return rvi; 
     } 

     @Override 
     public void onBindViewHolder(ChildViewHolder childViewHolder, int i) { 
      childViewHolder.txtChildLine.setText(_childItems[i]); 
     } 

     @Override 
     public int getItemCount() { 
      return _childItems.length; 
     } 

     public class ChildViewHolder extends RecyclerView.ViewHolder { 
      TextView txtChildLine; 
      public ChildViewHolder(View itemView) { 
       super(itemView); 
       txtChildLine = (TextView) itemView.findViewById(R.id.txtChildLine); 
      } 
     } 
    } 


activity_main.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/recyclerRoot" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </android.support.v7.widget.RecyclerView> 

</LinearLayout> 

root_row.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/txtRootLine" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/recyclerChild" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </android.support.v7.widget.RecyclerView> 

</LinearLayout> 


child_row.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/txtChildLine" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

hey, bạn có tìm thấy giải pháp nào cho điều đó không ?? –

+3

có thể trùng lặp của [Làm thế nào để có một ListView/RecyclerView bên trong một RecyclerView cha mẹ ?. Tôi có một danh sách cha mẹ, trong đó có một số danh sách con ở mọi vị trí] (http://stackoverflow.com/questions/32011995/how-to-have-a-listview-recyclerview-inside-a-parent-recyclerview-i- have-a-pare) – Mogsdad

+0

Bạn không nên tạo Bộ điều hợp mới trong onBindView(), bạn nên làm điều đó một lần. Vui lòng xem [thư viện này] (https://github.com/vivchar/RendererRecyclerViewAdapter) Tôi nghĩ rằng nó sẽ giúp bạn – Vitaly

Trả lời

-1

RecyclerView không hỗ trợ wrap_content.Set một số giá trị theo quan điểm tái chế lồng nhau như 200dp và item của bạn sẽ hiển thị. Nhiều cuộc thảo luận khác có sẵn here

+0

Điều đó không giải quyết vấn đề. –

+0

Điều này đã thay đổi với bản phát hành mới nhất, giờ đây bạn có thể sử dụng wrap_content cho RecyclerView của mình. Chỉnh sửa: Nguồn -> http://android-developers.blogspot.se/2016/02/android-support-library-232.html – zoltish

2

Trình quản lý bố cục hiện tại chưa hỗ trợ nội dung bọc. Kiểm tra bằng cách gán chiều cao cố định cho recyclerChild của bạn và chế độ xem sẽ xuất hiện.

Như một giải pháp cho vấn đề này, bạn có thể tạo Trình quản lý bố cục mới mở rộng Trình quản lý bố cục hiện tại và ghi đè phương pháp trênMeasure để đo nội dung gói.

0

Bằng Thư viện hỗ trợ Android 23,2 của một thư viện hỗ trợ phiên bản 23.2.0. Vì vậy, tất cả WRAP_CONTENT sẽ hoạt động chính xác.

Vui lòng cập nhật phiên bản thư viện trong tệp gradle.

compile 'com.android.support:recyclerview-v7:23.2.0' 
+0

Vẫn tất cả các hàng của nó không hiển thị –

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