简体   繁体   English

从 RecyclerView 适配器 Android 隐藏和显示多视图支架

[英]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)

See this for reference.请参阅此内容以供参考。

暂无
暂无

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

相关问题 Android-持有人从recyclerView适配器项目位置返回null - Android - holder returning null from recyclerView adapter item position recyclerView Adapter中的特定View Holder无法转换为View Holder - Specific View holder in recyclerView Adapter cant cast to View Holder 通过Activity在RecyclerView适配器中显示/隐藏按钮 - Show / Hide buttons in RecyclerView adapter from Activity 如何从 RecyclerView Adapter 持有人访问数据库? - How to access database from RecyclerView Adapter holder? Android RecyclerView IndexOutOfBoundsException 检测到不一致。 无效的视图支架适配器 positionViewHolder - Android RecyclerView IndexOutOfBoundsException Inconsistency detected. Invalid view holder adapter positionViewHolder 在另一个视图持有者RecyclerView android中获取一个视图持有者引用 - Get one view holder reference in another view holder RecyclerView android 如何通过名单 <E> 从RecyclerView适配器到RecyclerView支架? - How to pass List<E> from RecyclerView Adapter to RecyclerView Holder? 如何在Adapter Android中访问View Holder外部的holder元素? - How to access the holder element outside view Holder in adapter android? 检测到Recyclerview不一致。 无效的视图支架适配器positionViewHolder - Recyclerview Inconsistency detected. Invalid view holder adapter positionViewHolder 尝试在recyclerview中调用notifydatasetchanged()时,无效的视图持有器适配器 - Invalid view holder adapter while trying to call notifydatasetchanged() in recyclerview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM