gpt4 book ai didi

java - 从 RecyclerView 的适配器设置 ImageView 颜色过滤器

转载 作者:搜寻专家 更新时间:2023-11-01 07:53:27 27 4
gpt4 key购买 nike

我实际上尝试在定义适配器avec recyclerView 的类中设置imageView ColorFilter。为此,我在我的 Adapter 中定义了一个 ImageView ArrayList,它从 viewholder 中存储我的 imageview 我也在我的适配器中使用带有位置参数的自定义方法来获取它们。但它并没有真正起作用,当我点击我的项目时,colorfilter 随机应用在我的 recyclerviex 项目上:/

这是我的代码:

The adapter :

package com.ylly.hypred.process.adapter;

/**
* Created by YLLY on 24/06/2015.
*/
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.ylly.hypred.process.recyclerView.ItemDataAtelier;
import com.ylly.hypred.R;
import com.ylly.hypred.custom.MyTextView;

import java.io.File;
import java.util.ArrayList;

public class AtelierAdapter extends RecyclerView.Adapter<AtelierAdapter.ViewHolder> {
private ArrayList<ItemDataAtelier> itemsData;
private Context context;

public AtelierAdapter(ArrayList<ItemDataAtelier> itemsData, Context context) {
this.itemsData = itemsData;
this.context = context;
}

// Create new views (invoked by the layout manager)
@Override
public AtelierAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, null);

// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData

Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/drawable/" + itemsData.get(position).getDrawable());
Picasso.with(context).load(R.drawable.hypred_atelier_atelier).into(viewHolder.imgViewIcon);
viewHolder.nomAtelierTextView.setText(itemsData.get(position).getLabel());

}

// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imgViewIcon;
public MyTextView nomAtelierTextView;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
nomAtelierTextView = (MyTextView) itemLayoutView.findViewById(R.id.item_layout_nom_atelier_textView);
}
}


// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return itemsData.size();
}
}

我的项目数据:

package com.ylly.hypred.process.recyclerView;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;

/**
* Created by YLLY on 07/07/2015.
*/
public class ItemDataCriteres {

private String label;
private int drawable;
private Boolean isActive;
private ImageView imageView;

public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}

public Boolean getIsActive() {

return isActive;
}

public ItemDataCriteres(String label, int drawable){

this.label = label;

this.drawable = drawable;
}

public void setLabel(String label) {
this.label = label;
}

public String getNomAtelier() {

return label;
}

public void setDrawable(int drawable) {
this.drawable = drawable;
}

public int getDrawable() {

return drawable;
}


public void setImageView(ImageView imageView) {
this.imageView = imageView;
}

public ImageView getImageView() {

return imageView;
}

}

还有我定义 recyclerView 和更改过滤器的类:

final CriteresAdapter criteresAdapter = new CriteresAdapter(itemDataDetailsEtThemes, v.getContext());
recyclerViewCriteresOne.setAdapter(criteresAdapter);

recyclerViewCriteresOne.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (!criteresAdapter.getCritere(position).getIsActive()) {
criteresActifs[0]++;
setLancerRechercheTextViewVisibility(v);
criteresAdapter.getImageView(position).setColorFilter(v.getResources().getColor(R.color.hypred_gris), PorterDuff.Mode.MULTIPLY);
criteresAdapter.getCritere(position).setIsActive(true);
criteresAdapter.notifyDataSetChanged();
} else {
criteresActifs[0]--;
setLancerRechercheTextViewVisibility(v);
criteresAdapter.getImageView(position).clearColorFilter();
criteresAdapter.getCritere(position).setIsActive(false);
criteresAdapter.notifyDataSetChanged();
}
}
}));

提前感谢您的宝贵时间:)

最佳答案

来自 Drawable 文档:

By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

答案是调用 Drawable.mutate()

A mutable drawable is guaranteed to not share its state with any other drawable.

我会将您的 ItemTouchListener 更改为如下内容:

recyclerViewCriteresOne.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Drawable drawable = criteresAdapter.getImageView(position);
drawable.mutate();
if (!criteresAdapter.getCritere(position).getIsActive()) {
criteresActifs[0]++;
setLancerRechercheTextViewVisibility(v);
drawable.setColorFilter(v.getResources().getColor(R.color.hypred_gris), PorterDuff.Mode.MULTIPLY);
criteresAdapter.getCritere(position).setIsActive(true);
criteresAdapter.notifyDataSetChanged();
} else {
criteresActifs[0]--;
setLancerRechercheTextViewVisibility(v);
drawable.clearColorFilter();
criteresAdapter.getCritere(position).setIsActive(false);
criteresAdapter.notifyDataSetChanged();
}
}
}));

关于java - 从 RecyclerView 的适配器设置 ImageView 颜色过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31478813/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com