[英]Hide and show multiple view holder from RecyclerView Adapter Android
I have two view holders in my custom adapter say我的自定义适配器中有两个视图持有者说
ViewHolder1 at position 0 ViewHolder2 at position 1 ViewHolder1 在 position 0 ViewHolder2 在 position 1
Currently with view is perfectly fine.目前视野很好。
Now i have button in MainActivity.现在我在 MainActivity 中有按钮。 I want to do something like this in layman's term
我想用外行的话做这样的事情
On 1st click, it should hide the ViewHolder1 at position 0 and it should shift ViewHolder2 upwards.在第一次单击时,它应该在 position 0 处隐藏 ViewHolder1,并且它应该向上移动 ViewHolder2。 On 2nd click, it should view the Viewholder1 at position 0 and shift ViewHolder2 downward.
在第二次单击时,它应该在 position 0 处查看 Viewholder1 并将 ViewHolder2 向下移动。
How do i achieve this?我如何实现这一目标?
Maybe change the visibility of the whole item view to GONE or VISIBLE might help.也许将整个项目视图的可见性更改为 GONE 或 VISIBLE 可能会有所帮助。
I don't know if it works, but in theory it might work (cause you don't seem to want to remove items from the datasource).我不知道它是否有效,但理论上它可能有效(因为您似乎不想从数据源中删除项目)。
This is just to give you an idea on what you might do.这只是为了让您了解您可能会做什么。
Use interface:使用界面:
public interface itemVisibilityListener{
void itemVisibility(int position,int visibility);
}
In activity:活动中:
class MyActivity extends Activity implements itemVisibilityListener{
private int visible = View.VISIBLE;
private int gone = View.GONE;
private boolean firstItemVisible=true;
private MyAdapter adapter;
........
//on create
adapter.setItemVisibilityListener(this);
//when button clicked
button.setOnClickListener(new....{
if(firstItemVisible){
adapter.hideItem1(true);
}else{
adapter.hideItem1(false);
}
});
//this callback gives you feedback on visibility of first item
@override
public void itemVisibility(int position , int visibility){
button.setEnabled(true);
if(position==0 && visibility==visible){
//item of position 0 is visible
firstItemVisible=true;
}else if(position==0 && visibility==gone){
//item of position 0 is not visible
firstItemVisible=false;
}
}
}
In your adapter:在您的适配器中:
class MyAdapter extends .......{
private int visible = View.VISIBLE;
private int gone = View.GONE;
private boolean hide = false;
private itemVisibilityListener listener;
public void hideItem1(boolean hide){
this.hide=hide;
this.notifyDataSetChanged();
}
public void setItemVisibilityListener(itemVisibilityListener listener){
this.listener=listener;
}
.........
//on bind view holder
//this is your logic for handling multiple viewholders
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int postion){
if(holder.getItemViewType == FIRST_ITEM_TYPE){
//item 1 binding
if(hide){
holder.itemView.setVisibility(gone);
listener.itemVisibility(0,gone);
}else{
holder.itemView.setVisibility(visible);
listener.itemVisibility(0,visible);
}
}else if(holder.getItemViewType == SECOND_ITEM_TYPE){
//item 2 binding......
}
}
}
If you data is represented in a list like this,如果您的数据以这样的列表表示,
data = mutableListOf<BaseClass>(item1, item2)
then you can remove the 1st viewholder by updating your data and notifying your adapter然后您可以通过更新数据并通知适配器来删除第一个视图
data.remove(0)
adapter.notifyItemRemoved(0)
then when you want to add it again, you do然后当你想再次添加它时,你做
data.add(0, item1)
adapter.notifyItemInserted(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.