美文网首页RecyclerView研究内容Android 学习
简单实现RecyclerView嵌套RecyclerView

简单实现RecyclerView嵌套RecyclerView

作者: 楷桐 | 来源:发表于2017-03-28 10:40 被阅读4555次

    Linearlayoutmanager RecyclerView嵌套GridLayoutManager RecyclerView
    布局方式:以一个GridLayoutManager的recyclerview作为Linearlayoutmanager RecyclerView的itemview

    MainRecyclerAdapter.java

    public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_main, parent, false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.mButton.setText("Item " + position);
            if(holder.mRecyclerView.getAdapter()==null) {
                holder.mRecyclerView.setAdapter(new GridAdapter());
            }/*else {
                holder.mRecyclerView.getAdapter().notifyDataSetChanged();
            }*/
    
        }
    
        @Override
        public int getItemCount() {
            return 10;
        }
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public final AppCompatButton mButton;
            public final RecyclerView mRecyclerView;
    
            public ViewHolder(View itemView) {
                super(itemView);
                mButton = (AppCompatButton) itemView.findViewById(R.id.button);
                mRecyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
                RecyclerView.LayoutManager manager = new GridLayoutManager(itemView.getContext(), 4);
                manager.setAutoMeasureEnabled(true);
                mRecyclerView.setLayoutManager(manager);
            }
        }
    }
    

    需要注意的是GridLayoutManager要设置setAutoMeasureEnabled(true)成自适应高度***

    onBindViewHolder 中需要判断item中recyclerview是否已经设置适配器,重复设置会导致滑动不流畅***

    GridAdapter.java

    public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_grid, parent, false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.mTextView.setText("Grid "+position);
          }
    
        @Override
        public int getItemCount() {
            return 8;
        }
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public final AppCompatTextView mTextView;
    
             public ViewHolder(View itemView) {
                super(itemView);
                 mTextView= (AppCompatTextView) itemView.findViewById(R.id.textView);
             }
        }
    }
    

    相关文章

      网友评论

      • cryeye123:在嵌套的adapter中该如何加item里的控件控制?我现在只有最外层的item内部控件好用,而内层却不好用,不知道该怎么处理?
      • 代码打志bin:当GridAdapter的item个数不一样的时候,会出现数据展示错乱,显示不完全
        楷桐:@代码打志bin 外层是StaggeredGridLayoutManager,里面的用Linearlayoutmanager,这样还是显示不完全?
        代码打志bin:@ProcessZ 一样的,我是两个Linearlayoutmanager 嵌套
        楷桐:最外层的RecyclerView,用 StaggeredGridLayoutManager 试一下,看看好使吗
      • ImTudou:https://github.com/tudouloveloli/DoubleRecyclerViewDemo
        楷桐:如果下拉刷新想要变更数据,参照以下:

        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
        swipe_refresh.postDelayed(new Runnable() {
        @Override
        public void run() {
        dataInfoList.clear();
        initData();
        if(adapter != null){
        adapter = null;
        }
        adapter = new ParentInfoAdapter(MainActivity.this, dataInfoList);
        recyclerView.setAdapter(adapter);
        swipe_refresh.setRefreshing(false);
        }
        }, 1000);
        }
        });
        ImTudou:@梧桐树下梧桐树 :smile:
        楷桐:@ImTudou else {
        holder.mRecyclerView.getAdapter().notifyDataSetChanged();
        }
        这句话注掉,否则界面很卡顿
      • ImTudou:有Demo吗
        ImTudou:@梧桐树下梧桐树 嗯啊
        楷桐:时间有限,你把这个代码拷过去就行了,自己试着弄一下吧
      • 言者无知_n4c:那个setAdapter写在OncreateVIewholder里面就好了

      本文标题:简单实现RecyclerView嵌套RecyclerView

      本文链接:https://www.haomeiwen.com/subject/syfjottx.html