在recyclerview中更改imageview的颜色?

时间:2015-06-16 08:45:59

标签: android android-recyclerview

我正在使用recyclerview来整理我的数据,并且我要求将任何项目添加到用户最喜欢的列表中,所以我在适配器的视图中使用了png imageview,但每当我点击imageview时要在我喜欢的列表中添加项目,所有图像的颜色都会发生变化。但我想改变点击后的imageview的颜色。 这是我的代码。

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);


    }
}

2 个答案:

答案 0 :(得分:1)

它不起作用的原因很简单:您直接将背景设置为View从不这样做。修改您的模型,然后使用其中一种notify...()方法告诉Adapter它应该更新该位置的View

您发布的代码不包含Adapter的整个实现,但我假设您也在onBindViewHolder()中设置了颜色?由于您调用notifyItemChanged(),该位置的模型将被反弹,您设置的颜色将被覆盖到旧模型。

你应该做的是这样的事情:

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());
    }
}

答案 1 :(得分:0)

如果您在列表中的多个位置使用相同的可绘制对象,并且只想对单个图像进行一些修改,则需要先对该可绘制对象调用“mutate()”,因为默认可绘制对象共享状态!

DrawableCompat.setTint(pImageView.getDrawable().mutate(), ContextCompat.getColor(pImageView.getContext(), R.color.colorPrimaryDark));