实际上,我正在尝试开发一款可以在餐厅预订的应用程序。
您可能知道,当服务员点菜时,他可以从菜单中添加任何项目来记录(例如笔记本),甚至可以为添加的项目添加变体。
例如,我在应用程序中有一个披萨菜单,当我按菜单中的某些项目时,它会在添加披萨类型后添加到笔记本中(“菜单”和“笔记本为2 RecyclerView”),我可以通过“修改”它们在主要项目中添加一个子项目或更好的孩子。
或者更好的是,我的笔记本上有3个不同的披萨,通过单击其中的一个,我可以添加从另一个回收站视图中选择的变体“ PIZZA”和变体“ WITH PEPERONI”或“ WITHOUT PEPERONI”或“ WITH更多MOZZARELLA”。
就目前而言,我认为我的应用程序运行良好,就像我已经拥有了我所需要的,正如您在此GIF上所看到的那样,我正在将recyclerView“食品”添加到“ NOTEBOOK” recyclerView中按VARIANTI,我可以将Child项添加到最后添加的FOOD中。
但是问题来了,当我必须删除添加到食品中的单个变量时,我会对如何单击单个变量并能够删除单个变量提出一些建议。
这是我的适配器代码:
public class AdapterPTERM extends RecyclerView.Adapter<AdapterPTERM.ExampleViewHolder> {
private List<ItemPTERM> mExampleList;
private final LayoutInflater mInflater;
private OnItemClickListener mListener;
private List<Variant> variants;
private ManageAddRemoveCallbacks mManageAddRemoveCallbacks ;
private View.OnClickListener varientClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getTag()!= null){
int position = (int) v.getTag();
}
}
};
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_pterm,parent,false);
return new ExampleViewHolder(v,mListener);
}
AdapterPTERM(Context context, List<ItemPTERM> exampleList){
mExampleList = exampleList;
mInflater = LayoutInflater.from(context);
}
@Override
public void onBindViewHolder(@NonNull final ExampleViewHolder holder, final int position) {
final ItemPTERM item = mExampleList.get(position);
holder.desc.setText(item.getBtnName());
holder.qta.setText(String.valueOf(item.getQuant()));
holder.variantsContainer.removeAllViews();
// ADDING CHILD HERE
variants = item.getVariants();
if (variants != null && variants.size() > 0){
for(Variant v : variants){
View vView = mInflater.inflate(R.layout.variant_layout,holder.variantsContainer,false);
TextView nameTV = vView.findViewById(R.id.variant_name);
nameTV.setText(v.getName());
vView.setTag(position);
vView.setOnClickListener(varientClickListener);
holder.variantsContainer.addView(vView);
}
}
tvAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
void onAddItem(true);
}
});
tvRemoveItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
void onBtnClickRemoveItem(true);
}
});
// NOT IMPORTANT (CHANGING FIELDS COLOR)
if(position % 2 == 0 ){
holder.itemView.setBackgroundColor(Color.parseColor("#C0C0C0"));
}else if(position % 2 == 1){
holder.itemView.setBackgroundColor(Color.parseColor("#D3D3D3"));
}
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView desc;
public TextView qta;
private LinearLayout variantsContainer;
ExampleViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
desc = itemView.findViewById(R.id.Desc);
qta = itemView.findViewById(R.id.Qta);
variantsContainer = itemView.findViewById(R.id.ll_child_items);
try {
mManageAddRemoveCallbacks = ((ManageAddRemoveCallbacks) this);
} catch (ClassCastException e) {
throw new ClassCastException("ManageAddRemoveCallbacks must implement ManageAddRemoveCallbacksCallback.");
}
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});
}
}
public void removeItem(int position) {
mExampleList.remove(position);
notifyItemRemoved(position);
}
public interface ManageAddRemoveCallbacks {
void onAddItem(boolean isAdded);
void onBtnClickRemoveItem(boolean isRemoved);
}
}
答案 0 :(得分:1)
您可以在各自的click listeners();
上实现接口,首先转到您的适配器并声明一个这样的接口:
private ManageAddRemoveCallbacks mManageAddRemoveCallbacks ;
public interface ManageAddRemoveCallbacks {
void onAddItem(boolean isAdded);
void onBtnClickRemoveItem(boolean isRemoved);
}
现在在您的viewHolder中初始化您的界面:
try {
this.mManageAddRemoveCallbacks = ((ManageAddRemoveCallbacks) currentFragment);
} catch (ClassCastException e) {
throw new ClassCastException("ManageAddRemoveCallbacks must implement ManageAddRemoveCallbacksCallback.");
}
现在在您的 bind
方法中,您将在视图中添加点击监听器 ,将值分配给{{1} }
interfaces
现在,您需要在活动/片段中实现这些接口,如下所示:
tvAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
void onAddItem(ture or false depending on your requirements);
}
});
tvRemoveItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
void onBtnClickRemoveItem(ture or false depending on your requirements);
}
});
,以便您可以使用它们。这样,您将在活动中获取值。