notifyOnItemChanged不会更改边框颜色和文本

时间:2019-04-05 09:04:46

标签: android gridview android-recyclerview

我在recyclerview中有一个网格布局单个项目的图像视图带有边框和文本视图 imageview具有矢量图标

选择(单击项目)项目时,图像,边框和文本的颜色应更改,并且旧项目应恢复正常。 因此,只有一个选择是可能的

当前,仅新旧图像改变了颜色。 我不能用边框和文字来实现。

出于优化目的,我应该使用notifyItemChanged()而不是NOT notifyDataSetChanged()

Please check the image. 单击添加8时,它会突出显示,但旧项添加6应该恢复正常。像星星一样正常,但边框和文字不一样。

如何在边框和文字上实现相同的功能?

这是我尝试过的适配器类的代码。

      class imgAdapter extends RecyclerView.Adapter<imgAdapter.viewHolder> {
    Context context;
    List<String> mData;
    int selectedPos = RecyclerView.NO_POSITION;

    public imgAdapter(Context context, List<String> data) {
        this.context = context;
        this.mData = data;
    }

    //create holder
    @Override
    public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
        return new viewHolder(view,mData);
    }

    //bind the view to holder
    @Override
    public void onBindViewHolder(viewHolder holder, int position) {
        holder.category.setText(mData.get(position));
        holder.imgView.setImageResource(R.drawable.star_black);
        holder.imgView.setSelected(selectedPos == position);
    }

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


    //view Holder class
    public class viewHolder extends RecyclerView.ViewHolder {
        TextView category;
        ImageView imgView;
        LinearLayout linearLayout;
        int oldPos;

        public viewHolder(final View itemView, final List<String> mData) {
            super(itemView);
            category = (TextView) itemView.findViewById(R.id.country);
            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            linearLayout = (LinearLayout)itemView.findViewById(R.id.linearLayout);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                        oldPos = selectedPos;
                        notifyItemChanged(selectedPos); //reset color of old position

                    DrawableCompat.setTint(imgView.getDrawable(), ContextCompat.getColor(context, R.color.colorOrange));  //change color of selected position
                    DrawableCompat.setTint(imgView.getBackground(), ContextCompat.getColor(context, R.color.colorOrange));  //change border color of selected position
                    category.setTextColor(Color.parseColor("#F45642"));

                    selectedPos = getPosition();     // add new position as old position for next click

                    Toast.makeText(context, "old: " + oldPos, Toast.LENGTH_SHORT).show();


                }


            });

        }
    }
}

gridview单项的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="10dp"
    android:id="@+id/linearLayout">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_gravity="center"
        android:src="@drawable/star_black"
        android:tint="@color/colorGrey"
        android:background="@drawable/border"
        android:backgroundTint="@color/colorPrimary"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/category"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="Categories"/>

</LinearLayout>

谢谢。


更新

  1. 我担心通知仅更改图像颜色,而不更改边框颜色(设置为背景色)。 我们是否支持“通知”?

  2. 有什么方法可以手动设置setbackground,例如position == oldpos。
    即类似的东西: img [oldpos] .setBackground()

1 个答案:

答案 0 :(得分:0)

首先在可绘制文件夹中创建text_selector.xml图像,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/red" android:state_selected="true" />
    <item android:color="@android:color/red" android:state_focused="true" />
    <item android:color="@android:color/gray" />
</selector>

然后将“ text_selector.xml”作为textcolor传递到textview的recyclerview项目中。另外,您仅针对旧职位调用notifyItemChanged,也没有通知新项目更改。因此请按以下方式更改“ itemView.setOnClickListener”:

itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    if(oldPos != RecyclerView.NO_POSITION){
                        notifyItemChanged(oldPos); //reset color of old position
                    }

                    oldPos = selectedPos;

                    DrawableCompat.setTint(imgView.getDrawable(), ContextCompat.getColor(context, R.color.colorOrange));  //change color of selected position
                    DrawableCompat.setTint(imgView.getBackground(), ContextCompat.getColor(context, R.color.colorOrange));  //change border color of selected position
                    // category.setTextColor(Color.parseColor("#F45642"));
                    // comment this line and put text_selector drawable as textColor in xml

                    notifyItemChanged(selectedPos); //reset color of new position

                    selectedPos = getPosition();     // add new position as old position for next click

                    Toast.makeText(context, "old: " + oldPos, Toast.LENGTH_SHORT).show();
                }
            });

然后如下更改您的onBindViewHolder:

//bind the view to holder
    @Override
    public void onBindViewHolder(viewHolder holder, int position) {
        holder.category.setText(mData.get(position));
        holder.imgView.setImageResource(R.drawable.star_black);
        if(selectedPos == position){
            holder.category.setSelected(true);
            holder.imgView.setSelected(true);   
        }else{
            holder.category.setSelected(false);
            holder.imgView.setSelected(false);
        }

    }