Android中两个RecyclerView嵌套(一个水平,一个垂直)

首先,在开始写程序之前先添加design包,导包步骤如下:
1.第一步

Paste_Image.png

2.第二步

2.png

3.第三步

3.png

经过以上的三步,你就添加了design的包了,(添加其他的包也可以用这个方法来添加,简单快捷)

下面开始两个RecyclerView嵌套的使用,直接上代码
父布局的代码

<?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="wrap_content"
    >

    <TextView
        android:id="@+id/tv_name_parent"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>

子布局的代码

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

    <TextView
        android:id="@+id/tv_name_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:padding="10dp" />

</LinearLayout>

父布局Adapter

public class ParentRecyclerAdapter extends RecyclerView.Adapter<ParentViewHolder> {

    private Context context;
    private OnLayoutListenter listenter;
    private List<String> list_name_parent;

    public ParentRecyclerAdapter(Context context,
                                 List<String> list_name_parent) {
        this.context = context;
        this.list_name_parent = list_name_parent;
    }

    @Override
    public ParentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ParentViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.layout_recycelr_parent, parent, false));
    }

    @Override
    public void onBindViewHolder(ParentViewHolder holder, int position) {
        holder.tv_name_parent.setText(list_name_parent.get(position));
        getListenter().layoutChild(holder, position);


    }

    @Override
    public int getItemCount() {
        return list_name_parent.size();
    }

    public OnLayoutListenter getListenter() {
        return listenter;
    }

    public void setListenter(OnLayoutListenter listenter) {
        this.listenter = listenter;
    }
}

子布局Adapter

public class ChildRecyclerAdapter extends RecyclerView.Adapter<ChildViewHolder> {

    private Context context;
    private List<String> list_nam_child;

    public ChildRecyclerAdapter(Context context,
                                 List<String> list_nam_child) {
        this.context = context;
        this.list_nam_child = list_nam_child;
    }

    @Override
    public ChildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChildViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.layout_recycelr_child, parent, false));
    }

    @Override
    public void onBindViewHolder(ChildViewHolder holder, int position) {
        holder.tv_name_child.setText(list_nam_child.get(position));
    }

    @Override
    public int getItemCount() {
        return list_nam_child.size();
    }

}

MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv_parent;
    private ParentRecyclerAdapter adapterParent;
    private ChildRecyclerAdapter adapterChild;
    private List<String> list_name_parent;
    private List<String> list_name_child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        rv_parent = (RecyclerView) findViewById(R.id.rv_parent);
        rv_parent.setLayoutManager(new LinearLayoutManager(this));
        rv_parent.addItemDecoration(
                new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        adapterParent = new ParentRecyclerAdapter(MainActivity.this, list_name_parent);
        adapterParent.setListenter(new OnLayoutListenter() {
            @Override
            public void layoutChild(ParentViewHolder holder, int position) {
                holder.rv_child.setLayoutManager(new GridLayoutManager(MainActivity.this, 4,
                        LinearLayoutManager.VERTICAL, false));
                holder.rv_child.addItemDecoration(
                        new DividerItemDecoration(MainActivity.this,
                                LinearLayoutManager.HORIZONTAL));
                adapterChild = new ChildRecyclerAdapter(MainActivity.this, list_name_child);
                holder.rv_child.setAdapter(adapterChild);
            }
        });
        rv_parent.setAdapter(adapterParent);
    }

    private void initData() {
        list_name_parent = new ArrayList<>();
        list_name_child = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list_name_parent.add("我是父布局:" + i);
        }
        for (int j = 0; j < 3; j++) {
            list_name_child.add("我是子布局:" + j);
        }
    }
}

下载地址:
http://download.csdn.net/download/qq_32376365/9943292

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,049评论 25 709
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 47,073评论 22 665
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,790评论 2 45
  • 不知何时起,学校路两旁的银杏树上的叶子都黄了,一个个挂在树上。风一吹来,沙沙的。过了一两天吧,突然有一天走...
    若溪风阅读 4,844评论 0 0
  • 这两天小编想赶着植树节多在蚂蚁森林里种棵树, 这一入圈,小编深感种树圈也深似海…… 有个汉纸每天都给蚂蚁森林重度迷...
    Shirleyachi阅读 2,622评论 0 0

友情链接更多精彩内容