编程语言
首页 > 编程语言> > java – RecyclerView项目的底部表格对话框单击

java – RecyclerView项目的底部表格对话框单击

作者:互联网

在我的recyclerview中,有一些用户可以添加为收藏的项目.
当用户单击该项目的图标时,我将打开带有2个按钮的底部对话框.是或否.一旦我点击是,最喜欢的将被保存.

我通常会针对正常点击执行此操作,但如何针对任何点击将其实施到RecyclerView Adapter?

适配器类

 @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String urlExtension = "http://192.168.1.110/myapp/public/images";
    Glide.with(context).load(urlExtension+itemList.get(position).getList_image()).into(holder.imageView_list_city);
    holder.textView_list_city_name.setText(itemList.get(position).getList_name());
    holder.textView_list_city_desc.setText(itemList.get(position).getList_description());

    holder.layout_item_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "add", Toast.LENGTH_SHORT).show();


        }
    });

    holder.layout_item_detail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "detail", Toast.LENGTH_SHORT).show();
        }
    });

}

底板类

public class BottomSheetList extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.bottom_sheet_add, container, false);
    return view;
}
}

解决方法:

点击后需要定位.

在ViewHolder中设置OnClickListener并使用getAdapterPosition()获取点击项目的位置,然后调用鞋底页面视图

 static class Holder extends RecyclerView.ViewHolder {

    Button btnAdd;
    OnItemClickListener onItemClickListener;


    Holder(final View itemView) {
        super(itemView);


        btnAdd.setOnClickListener(view -> {
            onItemClickListener.onItemClick(getAdapterPosition());
        });
    }

    void setItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

接口

interface OnItemClickListener {

void onItemClick(int position);

}

设置监听器

holder.setItemClickListener(this);

 @Override
public void onItemClick(int position) {
    View modelBottomSheet = LayoutInflater.from(mContext).inflate(R.layout.layout, null);
    BottomSheetDialog dialog = new BottomSheetDialog(mContext);
    dialog.setContentView(modelBottomSheet);
    dialog.show();

}

标签:android,java,android-recyclerview,android-studio,bottom-sheet
来源: https://codeday.me/bug/20190910/1800049.html