recyclerview变空后如何设置view的可见性
How to set the visibility of a view after recyclerview becomes empty
我正在尝试将与 recyclerview 位于同一片段中的视图的可见性动态设置为不可见,当 recyclerview 变为 empty.The 问题是我正在从中删除一个项目适配器内的 recyclerview 虽然可见性必须在 fragment.I 内设置也实现了 "swipe to delete" recyclerview 项目,但那是在带有 rv 的片段内以及需要变得不可见的视图并且它工作正常.
从适配器中删除:
holder.shoppingCartDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int holderPosition = holder.getAdapterPosition();
removeItem(holderPosition, v.getContext());
MainActivity.shoppingCartDatabase.shoppingCartDao().delete(currentItem);
}
});
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
// SET VISIBILITY
}
notifyItemRemoved(position);
}
从片段中删除-删除最后一项后可见性发生变化:
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
MainActivity.shoppingCartDatabase.shoppingCartDao().delete(shoppingCartList.get(position));
shoppingCartList.remove(position);
shopAdapter.notifyDataSetChanged();
verifyIfRecyclerViewIsEmpty(shopAdapter, recyclerView);
}
}).attachToRecyclerView(recyclerView);
其中 verifyIftheRecyclerViewIsEmpty(shopAdapter, recyclerView) 是可见性的方法:
private void verifyIfRecyclerViewIsEmpty(RecyclerView.Adapter adapter, RecyclerView recyclerView) {
if (adapter.getItemCount() == 0) {
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
} else {
constraintLayout.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.VISIBLE);
textView.setVisibility(View.GONE);
btnAdd.setVisibility(View.GONE);
}
}
尝试使用此解决方案中提到的 RecyclerViewEmptySupport:
- 创建接口
OnItemListener
确保在片段中实现它,并在方法 onEmpty
中调用 verifyIfRecyclerViewIsEmpty
public interface OnItemListener {
void onEmpty();
}
- 在您的适配器中创建侦听器
private OnItemListener mOnItemListener;
public void setItemListener(IOnItemClickedListener listener) {
mOnItemListener = listener;
}
- 适配器中的更新方法
removeItem
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
if (mOnItemListener != null) {
mOnItemListener.onEmpty()
}
}
notifyItemRemoved(position);
}
在新文件中创建一个interface
:
public interface onShoppingItemRemovedListener{
void onRecyclerViewEmpty();
}
在您的 adapter
中执行此操作:
class YourAdapter extends .........{
//add this
private onShoppingItemRemovedListener listener;
.....
.....
//in the constructor
public YourAdapter (onShoppingItemRemovedListener listener,..............){
this.listener = listener;
......
......
......
}
//when you delete item do this
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
//at this point the recyclerview is empty, so notify the fragment
listener.onRecyclerViewEmpty();
}
}
}
最后在 fragment
中实现 interface
并传递给 adapter
:
class YourFragement extends ........ implements onShoppingItemRemovedListener{
//now when you initialize the adapter pass the listener like this
adapter = new YourAdapter(this,...........);
.....
.....
//override this
@Override
public void onRecyclerViewEmpty(){
//this is triggered when the recycler view is completely empty
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
}
}
我正在尝试将与 recyclerview 位于同一片段中的视图的可见性动态设置为不可见,当 recyclerview 变为 empty.The 问题是我正在从中删除一个项目适配器内的 recyclerview 虽然可见性必须在 fragment.I 内设置也实现了 "swipe to delete" recyclerview 项目,但那是在带有 rv 的片段内以及需要变得不可见的视图并且它工作正常.
从适配器中删除:
holder.shoppingCartDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int holderPosition = holder.getAdapterPosition();
removeItem(holderPosition, v.getContext());
MainActivity.shoppingCartDatabase.shoppingCartDao().delete(currentItem);
}
});
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
// SET VISIBILITY
}
notifyItemRemoved(position);
}
从片段中删除-删除最后一项后可见性发生变化:
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
MainActivity.shoppingCartDatabase.shoppingCartDao().delete(shoppingCartList.get(position));
shoppingCartList.remove(position);
shopAdapter.notifyDataSetChanged();
verifyIfRecyclerViewIsEmpty(shopAdapter, recyclerView);
}
}).attachToRecyclerView(recyclerView);
其中 verifyIftheRecyclerViewIsEmpty(shopAdapter, recyclerView) 是可见性的方法:
private void verifyIfRecyclerViewIsEmpty(RecyclerView.Adapter adapter, RecyclerView recyclerView) {
if (adapter.getItemCount() == 0) {
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
} else {
constraintLayout.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.VISIBLE);
textView.setVisibility(View.GONE);
btnAdd.setVisibility(View.GONE);
}
}
尝试使用此解决方案中提到的 RecyclerViewEmptySupport:
- 创建接口
OnItemListener
确保在片段中实现它,并在方法onEmpty
中调用
verifyIfRecyclerViewIsEmpty
public interface OnItemListener {
void onEmpty();
}
- 在您的适配器中创建侦听器
private OnItemListener mOnItemListener;
public void setItemListener(IOnItemClickedListener listener) {
mOnItemListener = listener;
}
- 适配器中的更新方法
removeItem
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
if (mOnItemListener != null) {
mOnItemListener.onEmpty()
}
}
notifyItemRemoved(position);
}
在新文件中创建一个interface
:
public interface onShoppingItemRemovedListener{
void onRecyclerViewEmpty();
}
在您的 adapter
中执行此操作:
class YourAdapter extends .........{
//add this
private onShoppingItemRemovedListener listener;
.....
.....
//in the constructor
public YourAdapter (onShoppingItemRemovedListener listener,..............){
this.listener = listener;
......
......
......
}
//when you delete item do this
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
//at this point the recyclerview is empty, so notify the fragment
listener.onRecyclerViewEmpty();
}
}
}
最后在 fragment
中实现 interface
并传递给 adapter
:
class YourFragement extends ........ implements onShoppingItemRemovedListener{
//now when you initialize the adapter pass the listener like this
adapter = new YourAdapter(this,...........);
.....
.....
//override this
@Override
public void onRecyclerViewEmpty(){
//this is triggered when the recycler view is completely empty
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
}
}