简体   繁体   English

回收站视图项目消失

[英]Recycler view item disappearing

I have an issue with recycler view.我对回收站视图有疑问。 I implemented a collapse logic as you can see on the code below.正如您在下面的代码中看到的那样,我实现了一个折叠逻辑。 But when I close the second item the view disappear as you can see on the video .但是当我关闭第二个项目时,视图会消失,正如您在视频中看到的那样。 What am I doing wrong.我究竟做错了什么。 Please assist.请协助。 Thanks谢谢

 public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.report_layout, viewGroup, false);

    final ReportHolder holder = new ReportHolder(v);

    //hide half of the view
    holder.linearLayout.setVisibility(View.GONE);

    holder.tvPrintReceipt.setVisibility(View.INVISIBLE);

    holder.tvClose.setOnClickListener(v1 -> {
        TransitionManager.beginDelayedTransition(viewGroup, new AutoTransition());
        holder.linearLayout.setVisibility(View.GONE);
        holder.tvViewRecords.setVisibility(View.VISIBLE);
    });

    holder.tvViewRecords.setOnClickListener(v2 ->{
        TransitionManager.beginDelayedTransition(viewGroup, new AutoTransition());
        holder.linearLayout.setVisibility(View.VISIBLE);
        holder.tvViewRecords.setVisibility(View.GONE);
    });

    return holder;
}

In order to not have problems with this you should set the visibility of your entire item to GONE.为了不出现问题,您应该将整个项目的可见性设置为 GONE。 itemView.setVisibility(View.GONE); or view.setVisibility(View.GONE);view.setVisibility(View.GONE);

Also check this thread for more information How to hide an item from Recycler View on a particular condition?另请查看此线程以获取更多信息如何在特定条件下从 Recycler View 隐藏项目?

The reason of this problem is that you wrote your logic in onCreatViewHolder which will be called once and you have to move it to onBindViewHolder which will be called per each item in RecyclerView.这个问题的原因是你在onCreatViewHolder中编写了你的逻辑,它将被调用一次,你必须将它移动到onBindViewHolder ,它将在 RecyclerView 中的每个项目中调用。 This will solve your issue.这将解决您的问题。 Now, clicking on each item, makes change on all items in the list.现在,单击每个项目,对列表中的所有项目进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM