6

I m trying to display nested recyclerview but the child items does not display. I want to display different items in all child view. I don't get a error, but the view is not refreshed.

Here is my code can any one help.

Thanks

    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>
cetinyasar
  • 73
  • 1
  • 1
  • 5
  • 3
    possible duplicate of [How to have a ListView/RecyclerView inside a parent RecyclerView?. I have a parent list, which contains some child lists at every position](http://stackoverflow.com/questions/32011995/how-to-have-a-listview-recyclerview-inside-a-parent-recyclerview-i-have-a-pare) – Mogsdad Aug 26 '15 at 03:42
  • You should not create new Adapter in onBindView(), you should do that once. Please look at [this library](https://github.com/vivchar/RendererRecyclerViewAdapter) I think it will help you – Vitaly Jan 26 '18 at 15:01
  • please see more detailed message there https://stackoverflow.com/a/48464993/4894238 – Vitaly Jan 26 '18 at 16:17
  • I solved this problem recently. You can follow the accepted answer here: https://stackoverflow.com/a/32086918/4757307 You should implement your custom linearlayout – pptang Aug 26 '15 at 03:37

3 Answers3

2

Existing layout manager does not support wrap content yet. Test it by assigning a fixed height to your recyclerChild and the view would appear.

As a solution to this problem you can create a new LayoutManager that extends the existing one and overrides onMeasure method to measure for wrap content.

Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17
0

By Android Support Library 23.2 of a support library version 23.2.0. So all WRAP_CONTENT should work correctly.

Please update version of a library in gradle file.

compile 'com.android.support:recyclerview-v7:23.2.0'
Orcun
  • 650
  • 1
  • 7
  • 16
-1

RecyclerView does not support wrap_content.Set some value in nested recycler view like 200dp and your item will shows. More discussion available here

Community
  • 1
  • 1
bilal
  • 2,187
  • 3
  • 22
  • 23
  • This has changed with the latest release, you can now use wrap_content for your RecyclerView. Edit: Source -> http://android-developers.blogspot.se/2016/02/android-support-library-232.html – zoltish Feb 29 '16 at 09:58