我尝试将classView ColorFilter设置在一个类中,该类定义了适配器avec the recyclerView。为此,我在我的适配器中定义了一个ImageView ArrayList,它从viewholder中存储了我的imageview。我也在我的适配器中使用带有position参数的自定义方法。但它并没有真正起作用,当我点击我的项目时,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();
}
}
我的itemData:
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();
}
}
}));
提前感谢您的时间:)
答案 0 :(得分:4)
来自Drawable文档:
默认情况下,从同一资源加载的所有drawables实例共享一个公共状态;如果修改一个实例的状态,则所有其他实例将收到相同的修改。
答案是调用Drawable.mutate()
保证不变的drawable不与任何其他drawable共享其状态。
我会将你的ItemTouchListener更改为:
emacs lisp