Android 交错网格显示错误的选定项目

Android 交错网格显示错误的选定项目,android,gridview,android-recyclerview,onclick,Android,Gridview,Android Recyclerview,Onclick,我用各种水平文本视图和onclick项交错排列网格,我正在更改颜色并将这些项添加到arraylist中。在后台,它可以很好地将每个选定的项添加到数组列表中 和第一幅图像一样,它显示选定的项目,但当我向右移动时,一些文本视图的颜色也发生了变化,而没有选择;当我向后滚动以启动选定项目时,颜色变为灰色,随机文本视图显示整个网格视图中的选定项目 这是我的适配器,我在其中更改颜色并在数组列表中添加项 public class Interest_RecyclerView_Adapter extends Re

我用各种水平文本视图和onclick项交错排列网格,我正在更改颜色并将这些项添加到arraylist中。在后台,它可以很好地将每个选定的项添加到数组列表中

和第一幅图像一样,它显示选定的项目,但当我向右移动时,一些文本视图的颜色也发生了变化,而没有选择;当我向后滚动以启动选定项目时,颜色变为灰色,随机文本视图显示整个网格视图中的选定项目

这是我的适配器,我在其中更改颜色并在数组列表中添加项

public class Interest_RecyclerView_Adapter extends RecyclerView.Adapter<Interest_RecyclerView_Adapter.ViewHolder> {
    public static final String TAG="###Interest Adapter###";
    Context context;
    List<String> jsonList;
    List<String> items=new ArrayList<>();



    public Interest_RecyclerView_Adapter(Context context, List<String> jsonList) {
        this.context = context;
        this.jsonList = jsonList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater= (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.interest_grid_single_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        Log.d(TAG,"On Bind View Holder ");
        holder.textView.setText(jsonList.get(position));

        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG,"On Click of Interest Calling "+position);

                String getText= (String) holder.textView.getText();
                if (items.contains(getText)){
                    Log.d(TAG,"Items Already Exist in List");
                    Log.d(TAG,"Get Text Value and Item Size "+getText+" "+items.size());
                    items.remove(getText);
                    Log.d(TAG,"Get Item Size "+items.size());
                    GradientDrawable shape=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[] {Color.parseColor("#FFc0c0c0"),Color.parseColor("#FF5E5E5E"),Color.parseColor("#FF000000")});
                    shape.setShape(GradientDrawable.RECTANGLE);
                    shape.setCornerRadius(45);
                    holder.textView.setBackgroundDrawable(shape);
                }
                else if (!items.contains(getText)){
                    holder.textView.setBackgroundDrawable(holder.textView.getContext().getResources().getDrawable(R.drawable.round_button_interest));
                    items.add(getText);
                    Log.d(TAG,"Items Size "+items.size());

                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (jsonList!=null && jsonList.size()>0){
            Log.d(TAG,"JSON list is not Null and has size of = "+jsonList.size());
            return jsonList.size();
        }
        else {
            return 0;
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        Interest_Grid interest_grid=new Interest_Grid();
        public ViewHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.interestSingleItemTextView);
            //Change the color of interest box at first launch of app (Currently its black grey)
            GradientDrawable shape=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[] {Color.parseColor("#FFc0c0c0"),Color.parseColor("#FF5E5E5E"),Color.parseColor("#FF000000")});
            shape.setShape(GradientDrawable.RECTANGLE);
            shape.setCornerRadius(45);
            textView.setBackgroundDrawable(shape);

        }
    }

    public void interestList(){
        Log.d(TAG,"Interest List methond in adapter");
        Log.d(TAG,"Size Of The List "+items.size());

    }

}
尝试像这样覆盖适配器中的getItemViewType

   @Override
    public int getItemViewType(int position) {
        return position;
    }
我希望这能解决你的问题,因为它解决了我的问题