Android嵌套的recyclerview [英] android nested recyclerview

查看:76
本文介绍了Android嵌套的recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示嵌套的recyclerview,但是子项未显示. 我想在所有子视图中显示不同的项目. 我没有收到错误,但是视图没有刷新.

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.

谢谢

    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>

推荐答案

现有的布局管理器尚不支持换行内容. 通过为您的recyclerChild分配一个固定的高度来进行测试,然后该视图就会出现.

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

作为解决此问题的方法,您可以创建一个新的LayoutManager,以扩展现有的LayoutManager并覆盖onMeasure方法以测量包装内容.

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.

这篇关于Android嵌套的recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆