Android 单选RecyclerView文本视图

Android 单选RecyclerView文本视图,android,google-cloud-firestore,textview,android-recyclerview,Android,Google Cloud Firestore,Textview,Android Recyclerview,我有一个RecyclerView,显示来自Firestore的物品 我所做的和它的工作是,当一个项目被点击时,它将被添加到firestore,其背景颜色将变为红色 但我想要的是,当单击另一个文本时,应该取消选择该项,并将其背景恢复为默认值。发生的情况是,该物品仅从firestore中移除。只能选择一个项目并将其添加到firestore 我搜索了stackoverflow上的所有内容,但没有找到我想要的 这是我的代码: holder.sizeView.setOnClickListener(new

我有一个
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();

                            }

                        }

                    }
                });

            }
        });
holder.sizeView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
firebaseFirestore.collection(“购物车”).document(userID).collection(“Products”).document(productID).collection(“Size”).document(“Product Size”).get().addOnCompleteListener(新OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
DocumentSnapshot DocumentSnapshot=task.getResult();
if(documentSnapshot.exists()){
firebaseFirestore.collection(“购物车”).document(用户ID).collection(“产品”).document(产品ID).collection(“大小”).document(“产品大小”).delete();
holder.sizeView.setBackgroundResource(R.drawable.size\u text\u background);
Toast.makeText(上下文,“已删除大小”,Toast.LENGTH_SHORT).show();
}否则{
Map productSizeMap=newhashmap();
productSizeMap.put(“大小”,sizeID);
firebaseFirestore.collection(“购物车”).document(用户ID).collection(“产品”).document(产品ID).collection(“大小”).document(“产品大小”).set(产品大小映射);
holder.sizeView.setBackgroundResource(R.drawable.size\u text\u background\u clicked);
Toast.makeText(上下文,“添加大小”,Toast.LENGTH_SHORT).show();
}
}
}
});
}
});
需要帮忙吗?如果有一些其他代码,你想看到我会张贴它。先谢谢你

小编辑:


如何将其设置为必填字段,以便可以单击“添加到购物车”按钮?

我将通过一个简单的示例显示字符串列表,向您展示如何实现所需的功能

在RecyclerView视图中表示项目的视图将是
test\u cell\u 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>
在这里,您可以看到我创建了一个用作侦听器的接口。我已经定义了一个全局变量来保存当前选定的项目位置。每次单击视图时,我都会更改所选项目的位置并通知适配器的更改,以便它可以使用更改更新其视图。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);
RecyclerView视图=findviewbyd(R.id.view);
ArrayList=新建ArrayList();
对于(int i=100;i<150;i++){
list.add(Integer.toString(i));
}
TestAdapter适配器=新的TestAdapter(此,列表);
Adapter.setOnItemClickListener(newtestAdapter.OnItemClickListener()){
@凌驾
公共虚线单击(视图视图项,int位置){
//您的项目点击代码
}
});
视图.设置适配器(适配器);
LinearLayoutManager liveImageLayoutManager=新的LinearLayoutManager(getApplicationContext());
liveImageLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
view.setLayoutManager(liveImageLayoutManager);

我希望您能了解一下这一点,并将其应用到您的代码中。

我将通过一个简单的示例来显示字符串列表,向您展示如何实现您想要的功能

在RecyclerView视图中表示项目的视图将是
test\u cell\u 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>
在这里,您可以看到我创建了一个用作侦听器的接口。我已经定义了一个全局变量来保存当前选定的项目位置。每次单击视图时,我都会更改所选项目的位置并通知适配器的更改,以便它可以使用更改更新其视图。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);
RecyclerView视图=findviewbyd(R.id.view);
ArrayList=新建ArrayList();
对于(int i=100;i<150;i++){
list.add(Integer.toString(i));
}
TestAdapter适配器=新的TestAdapter(此,列表);
Adapter.setOnItemClickListener(newtestAdapter.OnItemClickListener()){
@凌驾
公共虚线单击(视图视图项,int位置){
//您的项目点击代码
}
});
视图.设置适配器(适配器);
LinearLayoutManager liveImageLayoutManager=新的LinearLayoutManager(getApplicationContext());
liveImageLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
view.setLayoutManager(liveImageLayoutManager);

我希望您能看看这一点,并将其应用到您的代码中。

好的,首先感谢您的回答。其次,您的代码运行得很好。但是当我把它改编成我的代码时,它变得很奇怪。例如,当我单击某个项目时,其颜色将成为代码中的颜色。当我单击另一项时,col