Android 选择了多项 RecyclerView

Android many item selected RecyclerView

如果有人可以帮助我。我有一个带有项目的 RecyclerView 但是当我尝试 select ONE 项目时,我可以 select 很多项目。我认为问题出在 onClick 回调中,但我不确切知道。这是适配器代码:

public class HorizontalRecyclerViewAdapter extends RecyclerView
    .Adapter<HorizontalRecyclerViewAdapter
    .DataObjectHolder> {
private ArrayList<HorizontalData> mDataset;
private static MyClickListener myClickListener;

private SparseBooleanArray selectedItem = new SparseBooleanArray();

public class DataObjectHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    TextView mLabel;
    TextView mDateTime;
    LinearLayout linearLayout;

    public DataObjectHolder(View itemView) {
        super(itemView);
        mLabel = (TextView) itemView.findViewById(R.id.item_list_view_text_view);
        mDateTime = (TextView) itemView.findViewById(R.id.item_list_view_text_view_two);
        linearLayout = (LinearLayout) itemView.findViewById(R.id.myBackground);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (selectedItem.get(getAdapterPosition(), false)) {
            selectedItem.delete(getAdapterPosition());
            v.setSelected(false);
        } else {
            selectedItem.put(getAdapterPosition(), true);
            v.setSelected(true);
        }

        myClickListener.onItemClick(getAdapterPosition(), v);
    }
}

public void setOnItemClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}

public HorizontalRecyclerViewAdapter(ArrayList<HorizontalData> myDataset) {
    mDataset = myDataset;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.date_item, parent, false);

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
    holder.linearLayout.setSelected(selectedItem.get(position, false));

    holder.mLabel.setText(mDataset.get(position).getmTitle());
    holder.mDateTime.setText(mDataset.get(position).getmSubTitle());
}

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

public interface MyClickListener {
    void onItemClick(int position, View v);
}

}

谢谢!

首先你不需要SparseBooleanArray,因为你只想选择一个项目。 int selectedItemPos 表示当前选定的项目位置就足够了。

这是我刚刚创建的一个简单的适配器,它正在运行,并附有解释所发生情况的注释:

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {

    private ArrayList<String> items;
    //field mentioned before
    private int selectedItemPos = -1;

    //I create some items to see if my adapter is working.
    public TestAdapter() {
        this.items = new ArrayList<>();
        for (int i=0; i<20; i++) {
            items.add("Test" + i);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        String item = items.get(position);
        holder.mName.setText(item);
        //This line sets selection state to true if current position is the same as selected one, and false otherwise.
        holder.itemView.setSelected(selectedItemPos == position);
    }

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

    //helper method
    protected void setSelectedItem(int position) {
        int oldSelected = selectedItemPos;
        selectedItemPos = position;

        // update view of unselected item
        notifyItemChanged(oldSelected);

        //update view of just selected item
        notifyItemChanged(selectedItemPos);
    }

    protected class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView mName;

        public ViewHolder(View view) {
            super(view);
            mName = (TextView) view.findViewById(R.id.taskListItem_name);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            //if the clicked item is already selected,
            // we will just unselect it and no new item will be selected 
            //(new one is -1, which is none)
            int newSelectedItem = getAdapterPosition()==selectedItemPos?-1:getAdapterPosition();

            //apply changes on adapter
            setSelectedItem(newSelectedItem);
        }
    }
}

您应该能够轻松地将此代码与您的合并。