【问题标题】:How to set the visibility of a view after recyclerview becomes emptyrecyclerview变空后如何设置视图的可见性
【发布时间】:2020-09-12 15:32:31
【问题描述】:

我正在尝试将与 recyclerview 位于同一片段中的视图的可见性动态设置为当 recyclerview 变空时不可见。问题是我正在从 recyclerview 中删除一个项目适配器,而可见性必须在片段内设置。我还实现了“滑动删除”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);
    }
}

【问题讨论】:

  • 你的代码到底有什么问题?
  • @Zain 我正在尝试设置适配器内部视图的可见性,因为我在其中进行删除,但问题是视图位于片段中而不是适配器中.

标签: android android-studio android-recyclerview visibility


【解决方案1】:

尝试使用此解决方案中提到的 RecyclerViewEmptySupport:

https://stackoverflow.com/a/30415582/5434762

【讨论】:

    【解决方案2】:
    1. 创建接口OnItemListener确保你在你的片段中实现它,并在方法onEmpty中调用verifyIfRecyclerViewIsEmpty
    public interface OnItemListener {
    
        void onEmpty();
    }
    
    1. 在您的适配器中创建监听器
        private OnItemListener mOnItemListener;
    
        public void setItemListener(IOnItemClickedListener listener) {
            mOnItemListener = listener;
        }
    
    1. 在您的适配器中更新方法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);
    }
    

    【讨论】:

    • 这似乎不起作用。我按照您的步骤操作:我在适配器中创建了接口,片段实现了接口并在方法 onEmpty() 中添加了验证。我声明了私有 OnItemListener mOnItemListener 并创建了 setItemListener() 方法,如下所示: public void setItemListener(AdapterView.OnItemClickListener listener) { mOnItemListener = (OnItemListener) listener; } 但该方法并未在任何地方使用。
    • 您应该在片段内初始化适配器时设置它,调用适配器。 setItemListener(this)
    【解决方案3】:

    在新文件中创建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);
    
    
    }
    
    
    }
    

    【讨论】:

    • 这就像一个魅力!易于理解和理解。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 2019-06-22
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2021-10-16
    • 2016-09-18
    • 2015-02-04
    相关资源
    最近更新 更多