改变recyclerview的ImageView的颜色? [英] changing imageview's color in recyclerview?

查看:616
本文介绍了改变recyclerview的ImageView的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用recyclerview为listify我的数据,我也要求任何项目添加到用户的喜爱列表,所以我使用适配器的观点,一个PNG的ImageView,但每当我点击的ImageView在我最喜欢的列表中添加项的所有图像的颜色改变。但我想改变只是被点击的ImageView颜色。
这里是我的code。

 公共类TopTwentyAdapter扩展RecyclerView.Adapter< TopTwentyAdapter.mViewHolder> {私人最终LayoutInflater mInflater;
私人最终名单< TopTwentyModel> mModels;
私人ImageLoader的ImageLoader的;
私人字符串URL =HTTP:公共TopTwentyAdapter(上下文的背景下,列表与LT; TopTwentyModel>型号){
    mInflater = LayoutInflater.from(上下文);
    mModels =模型;
}@覆盖
公共mViewHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){
    最后查看ItemView控件= mInflater.inflate(R.layout.twenty_list,父母,假);
    返回新mViewHolder(ItemView控件);
}
@覆盖
公共无效onBindViewHolder(最终mViewHolder持有人,最终诠释位置){
    最后TopTwentyModel模型= mModels.get(位置);
    holder.bind(模型);
    holder.frame.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){            TextView中的TextView =(TextView中)view.findViewById(R.id.textView24);
            字符串s = textView.getText()的toString()。
            INT POS =的Integer.parseInt(S);
            最后TopTwentyModel模型= mModels.get(POS)
            意向意图=新意图(AppController.getInstance()getApplicationContext(),OfferDetails.class。);
            intent.putExtra(ID,model.getId());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            。AppController.getInstance()getApplicationContext()startActivity(意向)。
        }
    });
}@覆盖
公众诠释getItemCount(){
    返回mModels.size();
}公共类mViewHolder扩展RecyclerView.ViewHolder {
    私人最终的TextView txtViewTitle,字幕,brandid;
    私人最终NetworkImageView缩略图;
    私人最终RelativeLayout的框架;
    私人最终的TextView的数据;
    私人最终ImageView的夹子;
    私人ImageLoader的ImageLoader的;
    私人CardView cardview;
    //私人MaterialRippleLayout纹波;    公共mViewHolder(查看ItemView控件){
        超(ItemView控件);
        txtViewTitle =(TextView中)itemView.findViewById(R.id.txttitle_toptwenty);
        字幕=(TextView中)itemView.findViewById(R.id.sub_title_toptwenty);
        缩略图=(NetworkImageView)itemView.findViewById(R.id.thumbnail_topwenty);
        帧=(RelativeLayout的)itemView.findViewById(R.id.layer);
        brandid =(TextView中)itemView.findViewById(R.id.offerid);
        cardview =(CardView)itemView.findViewById(R.id.card_view);
        数据=(TextView中)itemView.findViewById(R.id.textView24);
        夹=(ImageView的)itemView.findViewById(R.id.add_fav);
        字体脸= Typeface.createFromAsset(AppController.getInstance()getAssets(),字体/ trebuc.ttf);
        subtitle.setTypeface(面);
        txtViewTitle.setTypeface(面);
        //ripple=(MaterialRippleLayout)itemView.findViewById(R.id.ripple);        clip.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                资源解析度= AppController.getInstance()getResources()。
                最终可绘制可绘制= res.getDrawable(R.drawable.ic_clipped_32);
                drawable.setColorFilter(Color.BLACK,PorterDuff.Mode.SRC_ATOP);
                clip.setBackgroundDrawable(绘制);
                notifyItemChanged(getAdapterPosition());            }
        });
    }    公共无效绑定(TopTwentyModel模型){        data.setText(model.getFakeId());        串hexColor =(的String.format(#%06X,(0XFFFFFF&放大器; model.getColor())));        frame.setBackgroundColor(model.getColor());
        subtitle.setText(model.getTitle());
        txtViewTitle.setText(model.getSubtitle());        如果(ImageLoader的== NULL)
            ImageLoader的= AppController.getInstance()getImageLoader()。
        字符串full_Url =HTTP://+ model.getBrandimage();
        thumbnail.setImageUrl(full_Url,ImageLoader的);
    }
}


解决方案

这是行不通的原因很简单:要设置直接背景,以查看。 绝不会做到这一点。修改您的模型,然后使用一个通知...()的方法来告诉适配器应该更新查看在该位置。

在code您发布不包括整个执行适配器,但我认为你设置 onBindViewHolder颜色( )呢?既然你叫 notifyItemChanged()在该位置该模型将反弹,你设置的颜色将被覆盖到旧的。


你应该做的是这样的:

 公共类ExampleViewHolder扩展RecyclerView.ViewHolder {    私人最终查看mSomeView;    私人ExampleModel mCurrentModel;    公共ExampleViewHolder(查看ItemView控件){
        超(ItemView控件);        mSomeView = itemView.findViewById(R.id.someView);        mSomeView.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //这里我们更改保存在当前模型中的背景,并通知了`Adapter`该模型已经改变。
                mCurrentModel.setBackgroundResourceId(R.drawable.someOtherBackground);
                notifyItemChanged(getAdapterPosition());
            }
        });
    }    公共无效绑定(ExampleModel模型){
        mCurrentModel =模型;
        //在绑定方法中,我们设置背景相应的`View`。
        mSomeView.setBackgroundResource(model.getBackgroundResourceId());
    }
}

I am using recyclerview for listify my data, and i have requirement to add any item to user's favourite list, so i am using a png imageview in adapter's view, but whenever i click the imageview to add the item in my favourite list, the color of all images changes. but i want to change just the clicked imageview's color. here is my code.

public class TopTwentyAdapter extends RecyclerView.Adapter<TopTwentyAdapter.mViewHolder> {

private final LayoutInflater mInflater;
private final List<TopTwentyModel> mModels;
private ImageLoader imageLoader;
private String url = "http:";

public TopTwentyAdapter(Context context, List<TopTwentyModel> models) {
    mInflater = LayoutInflater.from(context);
    mModels = models;
}

@Override
public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View itemView = mInflater.inflate(R.layout.twenty_list, parent, false);


    return new mViewHolder(itemView);
}


@Override
public void onBindViewHolder(final mViewHolder holder, final int position) {
    final TopTwentyModel model = mModels.get(position);
    holder.bind(model);
    holder.frame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            TextView textView = (TextView) view.findViewById(R.id.textView24);
            String s = textView.getText().toString();
            int pos = Integer.parseInt(s);
            final TopTwentyModel model = mModels.get(pos);
            Intent intent = new Intent(AppController.getInstance().getApplicationContext(), OfferDetails.class);
            intent.putExtra("id", model.getId());
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            AppController.getInstance().getApplicationContext().startActivity(intent);


        }
    });


}

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

public class mViewHolder extends RecyclerView.ViewHolder {


    private final TextView txtViewTitle, subtitle, brandid;
    private final NetworkImageView thumbnail;
    private final RelativeLayout frame;
    private final TextView data;
    private final ImageView clip;
    private ImageLoader imageLoader;
    private CardView cardview;
    //private MaterialRippleLayout  ripple;

    public mViewHolder(View itemView) {
        super(itemView);
        txtViewTitle = (TextView) itemView.findViewById(R.id.txttitle_toptwenty);
        subtitle = (TextView) itemView.findViewById(R.id.sub_title_toptwenty);
        thumbnail = (NetworkImageView) itemView.findViewById(R.id.thumbnail_topwenty);
        frame = (RelativeLayout) itemView.findViewById(R.id.layer);
        brandid = (TextView) itemView.findViewById(R.id.offerid);
        cardview = (CardView) itemView.findViewById(R.id.card_view);
        data = (TextView) itemView.findViewById(R.id.textView24);
        clip = (ImageView) itemView.findViewById(R.id.add_fav);
        Typeface face = Typeface.createFromAsset(AppController.getInstance().getAssets(), "font/trebuc.ttf");
        subtitle.setTypeface(face);
        txtViewTitle.setTypeface(face);
        //ripple=(MaterialRippleLayout)itemView.findViewById(R.id.ripple);

        clip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Resources res = AppController.getInstance().getResources();
                final Drawable drawable = res.getDrawable(R.drawable.ic_clipped_32);
                drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
                clip.setBackgroundDrawable(drawable);
                notifyItemChanged(getAdapterPosition());

            }
        });
    }

    public void bind(TopTwentyModel model) {

        data.setText(model.getFakeId());

        String hexColor = (String.format("#%06X", (0xFFFFFF & model.getColor())));

        frame.setBackgroundColor(model.getColor());
        subtitle.setText(model.getTitle());
        txtViewTitle.setText(model.getSubtitle());

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        String full_Url = "http://" + model.getBrandimage();
        thumbnail.setImageUrl(full_Url, imageLoader);


    }
}

解决方案

The reason it does not work is simple: You are setting the background to the View directly. NEVER do that. Modify your model and then use one of the notify...() methods to tell the Adapter it should update the View at that position.

The code you posted does not include the whole implementation of the Adapter, but I assume that you set the color in onBindViewHolder() as well? Since you call notifyItemChanged() the model at that position will be rebound and the color you set will be overwritten to the old one.


What you should be doing is something like this:

public class ExampleViewHolder extends RecyclerView.ViewHolder {

    private final View mSomeView;

    private ExampleModel mCurrentModel;

    public ExampleViewHolder(View itemView) {
        super(itemView);

        mSomeView = itemView.findViewById(R.id.someView);

        mSomeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Here we change the background saved in the current model and notify the `Adapter` that the model has changed.
                mCurrentModel.setBackgroundResourceId(R.drawable.someOtherBackground);
                notifyItemChanged(getAdapterPosition());
            }
        });
    }

    public void bind(ExampleModel model) {
        mCurrentModel = model;
        // In the bind method we set the background the appropriate `View`.
        mSomeView.setBackgroundResource(model.getBackgroundResourceId());
    }
}

这篇关于改变recyclerview的ImageView的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆