我有一个RecyclerView
,用于显示Firestore
中的项目。
我所做的并且正在起作用的是,当单击一个项目时,该项目将被添加到firestore中,并且其背景将颜色变为红色。
但是我想要的是,当单击另一个文本时,应取消选择该项目并将其背景恢复为默认值。发生的情况是该项目仅从Firestore中删除。只需选择一个项目并将其添加到firestore。
我搜索了在stackoverflow上找到的所有内容,但没有找到想要的内容。
这是我的代码:
holder.sizeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseFirestore.collection("Shopping Cart").document(userID).collection("Products").document(productID).collection("Size").document("Product Size").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
firebaseFirestore.collection("Shopping Cart").document(userID).collection("Products").document(productID).collection("Size").document("Product Size").delete();
holder.sizeView.setBackgroundResource(R.drawable.size_text_background);
Toast.makeText(context, "Size Removed", Toast.LENGTH_SHORT).show();
} else {
Map<String, String> productSizeMap = new HashMap<>();
productSizeMap.put("size", sizeID);
firebaseFirestore.collection("Shopping Cart").document(userID).collection("Products").document(productID).collection("Size").document("Product Size").set(productSizeMap);
holder.sizeView.setBackgroundResource(R.drawable.size_text_background_clicked);
Toast.makeText(context, "Size Added", Toast.LENGTH_SHORT).show();
}
}
}
});
}
});
请帮忙吗?如果您要查看其他代码,我会发布它。并预先感谢。
小编辑:
如何将其设置为必填字段,以便可以单击“添加到购物车”按钮?
答案 0 :(得分:0)
我将通过一个简单的示例来显示字符串列表,向您展示如何实现您的期望。
我在RecyclerView视图中代表一个项目的视图将是test_cell_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/holder_background"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"/>
</LinearLayout>
然后我创建了一个扩展RecyclerView.Adapter
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private int selectedPos = 0;
private OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(View viewItem, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout background;
TextView itemText;
ViewHolder(final View viewItem) {
super(viewItem);
background = (LinearLayout) viewItem.findViewById(R.id.holder_background);
itemText = (TextView) viewItem.findViewById(R.id.item_text);
viewItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(viewItem, position);
notifyItemChanged(selectedPos);
selectedPos =getAdapterPosition();
notifyItemChanged(selectedPos);
}
}
}
});
}
public void changeToSelect(int colorBackground) {
background.setBackgroundColor(colorBackground);
}
}
private List<String> list;
private Context context;
public TestAdapter(Context context, List<String> list) {
this.list = list;
this.context= context;
}
@Override
public TestAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.test_cell_item, parent, false);
ViewHolder itemViewHolder = new ViewHolder(itemView);
return itemViewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.changeToSelect(selectedPos == position ? Color.parseColor("#ca3854") : Color.BLACK);
TextView filterText = holder.itemText;
filterText.setTextSize(30);
filterText.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
}
在这里您可以看到我已经创建了一个用作OnItemClickListener的接口。我定义了一个全局变量来保存当前选定的项目位置。每次单击视图时,我都会更改所选项目的位置,并将更改通知给适配器,以便它可以使用更改来更新其视图。 OnBindViewHolder方法可以将背景的颜色更改为当前位置(如果当前处于选定位置)以选择颜色,否则更改为黑色。
在活动中,我使用了这样的适配器
RecyclerView view = findViewById(R.id.view);
ArrayList<String> list = new ArrayList<>();
for(int i = 100; i < 150; i++) {
list.add(Integer.toString(i));
}
TestAdapter adaptor = new TestAdapter(this, list);
adaptor.setOnItemClickListener(new TestAdapter.OnItemClickListener() {
@Override
public void onItemClick(View viewItem, int position) {
//Your item click code
}
});
view.setAdapter(adaptor);
LinearLayoutManager liveImageLayoutManager = new LinearLayoutManager(getApplicationContext());
liveImageLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
view.setLayoutManager(liveImageLayoutManager);
我希望您能对此有所了解,并根据您的代码调整它的处理方式。