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

时间:2018-06-11 19:57:00

标签: java android android-studio android-recyclerview bottom-sheet

在我的recyclerview中,有些项目可供用户添加为收藏。 当用户点击该项目的+图标时,我将使用2个按钮打开Bottom Sheet DialogYesNo。一旦我点击是,最喜欢的将被保存。

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

适配器类

 @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;
}
}

1 个答案:

答案 0 :(得分:2)

点击后需要立场。

在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();

}