如何从RecyclerView中的所有项目中删除按钮/视图?

时间:2019-11-02 09:52:10

标签: android android-recyclerview

我有一个基本的卧式recyclerView,在所有项目中都有按钮。当用户单击任意一项中的按钮时,我希望从recyclerView中的所有项中删除该按钮或取消可见性。

这是我的代码:

 @Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

    holder.button.setOnClickListener(v -> {
        v.postDelayed(() -> holder.likes.setVisibility(View.GONE),500);
    });

}

这段代码仅在已单击的项目中删除按钮。该如何解决?

编辑

private ArrayList<SingleItemModel> itemsList;
private Context mContext;

SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
    this.itemsList = itemsList;
    this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.horizontal_single_row, viewGroup, false);
    SingleItemRowHolder mh = new SingleItemRowHolder(v);
    return mh;
}

boolean a;
@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

    holder.likes.setOnClickListener(v -> {
        v.postDelayed(() -> holder.button.setVisibility(View.GONE),500);

    });

}

@Override
public int getItemCount() {
    return (null != itemsList ? itemsList.size() : 0);
}

class SingleItemRowHolder extends RecyclerView.ViewHolder {

    ImageButton button;

    SingleItemRowHolder(View view) {
        super(view);
        this.button = view.findViewById(R.id.profilePic);

    }

}

3 个答案:

答案 0 :(得分:2)

尝试一下

private ArrayList<SingleItemModel> itemsList;
private Context mContext;
private boolean isClick = true;

SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
    this.itemsList = itemsList;
    this.mContext = context;
}

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.horizontal_single_row, viewGroup, false);
    SingleItemRowHolder mh = new SingleItemRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {

    holder.likes.setOnClickListener(v -> {
        v.postDelayed(() -> {
        holder.button.setVisibility(View.GONE);
        isClick = false;
        notifyDataSetChanged();
        },500);
    });

    if(isClick){
        holder.binding.llShare.setVisibility(View.VISIBLE);
    }else{
        holder.binding.llShare.setVisibility(View.GONE);
    }

}

@Override
public int getItemCount() {
    return (null != itemsList ? itemsList.size() : 0);
}

class SingleItemRowHolder extends RecyclerView.ViewHolder {

    ImageButton button;

    SingleItemRowHolder(View view) {
        super(view);
        this.button = view.findViewById(R.id.profilePic);

    }
}

这是对您问题的完美答案。

希望这对您有帮助!

谢谢。

答案 1 :(得分:1)

    private boolean isShow=false;
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position)
    {
        if(isShow){holder.button.setVisibility(View.GONE);}
    }

holder.likes.setOnClickListener(v -> {
        v.postDelayed(() -> isShow=true,notifyDataSetChanged();},500);

    });
    }

答案 2 :(得分:0)

我认为最好在模型中创建一个布尔字段(将其传递给recyclerview),然后检查按钮是否可见,如果按钮为false,按钮是不可见的,则当您单击某个项目时,可以使此布尔值对项目为false,然后使用notifyDataSetChanged()更新所有项目视图

我希望它能对您有所帮助(: