【问题标题】:changing imageview's color in recyclerview?在recyclerview中改变imageview的颜色?
【发布时间】:2015-06-16 08:45:58
【问题描述】:

我正在使用 recyclerview 来列出我的数据,并且我需要将任何项目添加到用户最喜欢的列表中,所以我在适配器的视图中使用 png 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);


    }
}

【问题讨论】:

  • 这整个问题都与this one有关。
  • 您至少可以发布您的ViewHolder 的全部代码。如果没有整个代码,我只能猜测您可能做错了什么。
  • 好的,请稍候,我刚刚发布它已发布。请看一看。
  • 正如我所怀疑的,看看我的回答,它是解决你问题的方法。
  • 检查我的答案。由于无法在 cmets 中发布更多代码,将在解决问题后删除。

标签: android android-recyclerview


【解决方案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());
    }
}

【讨论】:

  • 我也想反之亦然。就像我在收藏列表中添加了一个项目,我也可以将其删除。点击。
  • 这很容易做到,你只需要在点击监听器中实现相应的逻辑。
  • 好的。所以setBackgroundResourceId(Drawable drawable)get...() 这两种方法必须在模型类中正确创建。并且返回值将是整数。在bind() 我必须提供图像的默认视图。并且在onClick 中必须更改图像对吗?
  • 在点击监听器中,您修改模型,更具体地说是存储背景的字段,在绑定中,您将模型中的值设置为视图。每次修改模型都需要调用notify...()方法告诉Adapter在提供的位置属于模型的View需要更新。
  • 为了修改模型我必须通过 resId。为此,我必须像Modelclass mModel=new Modelclass(); 那样做,但如果我这样做,我必须通过参数。我应该在模型类中创建一个空的构造函数吗?请您更简单地回答一下。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    相关资源
    最近更新 更多