Android - RecyclerView项目选择问题

时间:2017-05-27 13:56:28

标签: android android-recyclerview

我正在尝试实现功能,如果用户点击#define X static_cast<double>(2) #include <algorithm> // if you change the two lines, no error occurs int main() { return 0; } 中的一个或多个项目正在选择这些项目,并且用户第二次点击相同的项目/项目时会取消选择这些项目。要向用户显示已选择或取消选择用户点击的项目,将显示或隐藏图像。

但是,如果我点击RecyclerView中的一个项目,则会选择多个项目。

到目前为止,这是我的代码:

my_list_item.xml

RecyclerView

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/tvTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Test"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/ivItemIsSelected"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/done_256"
        android:visibility="invisible" />

</RelativeLayout>

MainActivity.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.www.recyclerviewadaptertest.MainActivity">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rvTest"/>

</RelativeLayout>

RecyclerViewTestAdapter .java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    private RecyclerView rvTest;
    private RecyclerViewTestAdapter recyclerViewTestAdapter;
    private ArrayList<TestModel> testModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testModels = new ArrayList<>();

        for(int i = 0; i < 30; i++){
           TestModel testModel = new TestModel("Test nr." + (i+1));
            testModels.add(testModel);
        }

        recyclerViewTestAdapter = new RecyclerViewTestAdapter(testModels);
        rvTest = (RecyclerView)findViewById(R.id.rvTest);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this);
        rvTest.setLayoutManager(mLayoutManager);
        rvTest.setItemAnimator(new DefaultItemAnimator());
        rvTest.setAdapter(recyclerViewTestAdapter);
    }
}

TestModel.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class RecyclerViewTestAdapter extends RecyclerView.Adapter<RecyclerViewTestAdapter.MyViewHolder> {

    private List<TestModel> testModels;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivItemIsSelected;
        public TextView tvTest;

        public MyViewHolder(View view) {
            super(view);
            ivItemIsSelected = (ImageView) view.findViewById(R.id.ivItemIsSelected);
            tvTest = (TextView) view.findViewById(R.id.tvTest);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(ivItemIsSelected.getVisibility() == View.INVISIBLE){
                        ivItemIsSelected.setVisibility(View.VISIBLE);
                    }else if(ivItemIsSelected.getVisibility() == View.VISIBLE){
                        ivItemIsSelected.setVisibility(View.INVISIBLE);
                    }
                }
            });
        }
    }

    public RecyclerViewTestAdapter(List<TestModel> testModels) {
        this.testModels = testModels;
    }

    @Override
    public RecyclerViewTestAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_list_item, parent, false);

        return new RecyclerViewTestAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewTestAdapter.MyViewHolder holder, int position) {
        TestModel testModel = testModels.get(position);
        holder.tvTest.setText(testModel.getTestText());
    }

    @Override
    public int getItemCount() {
        return testModels.size();
    }
}

2 个答案:

答案 0 :(得分:2)

您不应该在ViewHolder中保持“已选中”状态,因为它会被回收。在模型中保持“选定”状态。修改TestModel.java: 公共类TestModel {

onLoadMore

}

在onBind中设置对模型项的引用,并根据item:

的选定标志设置Visibility
private String testText;
public boolean selected = false;

public TestModel() {
}

public TestModel(String testText) {
    this.testText = testText;
}

public String getTestText() {
    return testText;
}

public void setTestText(String testText) {
    this.testText = testText;
}

还有其他方法可以实现这一点,但重要的是要记住每次调用onBindViewHolder时都必须更新其中的所有内容以反映指定位置的项目。希望这会有所帮助。

答案 1 :(得分:0)

  holder.parentLayout.setOnClickListener {

        if (holder.categoryIcon.isSelected) {
            selectedPos = position
        }

        if (selectedPos == position) {
            notifyItemChanged(selectedPos)
            selectedPos = RecyclerView.NO_POSITION
        } else {
            selectedPos = position
            notifyItemChanged(selectedPos)
        }
    }

    if (selectedPos == position) {
        holder.categoryIcon.setBackgroundColor(ContextCompat.getColor(context, R.color.orange))
        holder.categoryIcon.isSelected = true
    } else {
        holder.categoryIcon.setBackgroundColor(ContextCompat.getColor(context, R.color.white))
        holder.categoryIcon.isSelected = false
    }