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

时间:2018-01-04 13:18:03

标签: android gridview android-recyclerview onclick

Selected Grid Unwanted change in color items Selected color changed

我有Staggered Grid,有各种水平文本视图和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());

    }

}

1 个答案:

答案 0 :(得分:0)

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

   @Override
    public int getItemViewType(int position) {
        return position;
    }

我希望这可以解决您的问题,因为它解决了我的问题。