RecyclerView中嵌套RecyclerView的时候出现子RecyclerView显示不全的问题解决

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42618969/article/details/81119860

如图首页整个界面是一个RecyclerView,根据不同的type创建不同的view来显示:

RecyclerView常见问题解决方案:

1:首先查看RecyclerView在设置Adapter前有没有被LayoutManager管理(如果没有肯定连显示都不显示)

2:查看子RecyclerView的子项布局最外层有没有把高度设置成match_parent,如果设置了(只显示一行)

3:当前两种情况都没有错的时候我们就检查一下传入的数据有没有问题等

4:RecyclerView嵌套自动滚动,RecyclerView 高度设置wrap_content 无作用等问题 

项目中遇到多个recyclerview嵌套时,要么只能显示一个Item,要么Item宽度不能铺满填充父布局,按照网上搜的“简单解决RecyclerView嵌套的RecyclerView条目显示不全和宽度不能铺满"还是没能解决,最后,看到本文开始的链接里的文章,终于解决了。

总结一下:最子recyclerview(最里面的)onCreateViewHolder方法中应这样写inflate(R.layout.item_fragment_study_hot_item, parent, false);

然后其父布局inflate(R.layout.item_fragment_study_hot_body, null, false);

这样的写法,解决了recyclerview只能显示一个Item,还有问题是宽度不能铺满,然后在写父recyclerview的setLayoutManager方法时,这样写:

//RecyclerView 条目布局宽度设置match_parent无效果的问题解决 
 //这里为了解决recycleview不能撑满全屏的问题,这里layoutManager不管你布局里是否设置,都不准确,所以需要在代码里
 //重新设置MATCH_PARENT

LinearLayoutManager layoutManager = new LinearLayoutManager(context) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

 

如下:是模仿朋友圈动态和评论的例子:

private class UserDiaryAdapter extends RecyclerView.Adapter<UserDiaryAdapter.ViewHolder> {
    private List<Diary> mDiaryList;
    class ViewHolder extends RecyclerView.ViewHolder {
        View myView;
        ImageView userImage;
        TextView userName;
        TextView dateTime;
        TextView circleText;
        ImageView circleBabyImage;
        RecyclerView commentRecycler;

        public ViewHolder(View itemView) {
            super(itemView);
            myView=itemView;
            userImage = (ImageView) itemView.findViewById(R.id.user_image);
            userName = (TextView) itemView.findViewById(R.id.user_name);
            dateTime = (TextView) itemView.findViewById(R.id.data_time);
            circleText = (TextView) itemView.findViewById(R.id.circle_text);
            circleBabyImage = (ImageView) itemView.findViewById(R.id.circle_baby_image);
            commentRecycler=(RecyclerView) itemView.findViewById(R.id.comment_recycler);
            //如果写成RecyclerView就得使用如下的layoutManager来管理
            LinearLayoutManager layoutManager=new LinearLayoutManager(myView.getContext());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            commentRecycler.setLayoutManager(layoutManager);
        }
    }

    public UserDiaryAdapter(List<Diary> diaryList) {
        mDiaryList = diaryList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.diary_circle_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Diary diary = mDiaryList.get(position);
        GlideUtil.GlideWithPlaceHolder(CheckUserActivity.this, diary.getAuthorFace()).into(holder.userImage);
        holder.userName.setText(diary.getAuthorName());
        holder.dateTime.setText(diary.getDiaryTime());
        holder.circleText.setText(diary.getContent());
        GlideUtil.GlideWithPlaceHolder(CheckUserActivity.this, diary.getDiaryImg().get(position)).into(holder.circleBabyImage);
       // holder.commentRecycler.setAdapter(new CommentAdapter(diary.getComment().getCommentMemberList()));
        //只显示一条?
        CommentAdapter adapter=new CommentAdapter(diary.getComment().getCommentMemberList());
        holder.commentRecycler.setAdapter(adapter);
    }

    @Override
    public int getItemCount() {
        return mDiaryList.size();
    }
    class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolders>{
        private List<CommentMember> mCommentMemberList;
        class ViewHolders extends RecyclerView.ViewHolder{
            TextView commentName;
            TextView commentContent;
            public ViewHolders(View itemView){
                super(itemView);
                commentName=(TextView)itemView.findViewById(R.id.comment_name);
                commentContent=(TextView)itemView.findViewById(R.id.comment_content);
            }
        }
        public CommentAdapter(List<CommentMember> commentMembers){
            mCommentMemberList=commentMembers;
        }
        @Override
        public ViewHolders onCreateViewHolder(ViewGroup parent,int viewType){
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment_layout,parent,false);
            ViewHolders holders=new ViewHolders(view);
            return holders;
        }
        @Override
        public void onBindViewHolder(ViewHolders holder,int position){
            CommentMember commentMember=mCommentMemberList.get(position);
            holder.commentName.setText(commentMember.getMemberName()+": ");
            holder.commentContent.setText(commentMember.getContent());
        }
        @Override
        public int getItemCount(){
            return mCommentMemberList.size();
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页