我正在尝试创建使用RecyclerView安装的应用列表。但不是设备中的所有应用程序,只有一些。
我已将这些应用的软件包设置为字符串数组,代码会检查用户是否安装了该应用。每个应用程序都有一个彩色的drawable /图标来识别它,如果用户没有安装该应用程序,drawable / icon会变成黑白。
它可以正常工作,但在向下滚动然后再向上滚动后,彩色并且应该安装的项目变为黑白,甚至在一些滚动之后它也不会再次获得原始颜色。
有人可以告诉我如何预防或解决这个问题吗?提前谢谢。
这是我的适配器代码:
package jahirfiquitiva.project.adapters;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import jahirfiquitiva.project.activities.AppsListActivity;
import java.util.List;
import jahirfiquitiva.project.R;
import jahirfiquitiva.project.views.SquareImageView;
public class AppsAdapter extends RecyclerView.Adapter<AppsAdapter.AppHolder> implements View.OnClickListener {
public interface ClickListener {
void onClick(int index);
}
private final Context mContext;
private final List<AppsListActivity.App> apps;
private final ClickListener mCallback;
public AppsAdapter(Context context, List<AppsListActivity.App> apps, ClickListener callback) {
this.mContext = context;
this.apps = apps;
this.mCallback = callback;
}
@Override
public AppHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new AppHolder(inflater.inflate(R.layout.app_item, parent, false));
}
@Override
public void onBindViewHolder(AppHolder holder, int position) {
// Turns App name "Something Pro" to "l_something_pro"
int iconResource = mContext.getResources().getIdentifier(
"ic_" + apps.get(position).name.toLowerCase().replace(" ", "_"),
"drawable",
mContext.getPackageName()
);
holder.icon.setImageResource(R.drawable.placeholder);
if (apps.get(position).isInstalled(mContext)) {
holder.icon.setImageResource(iconResource);
} else {
holder.icon.setImageResource(iconResource);
makeBlackAndWhite(holder.icon);
}
holder.appname.setText(apps.get(position).name);
holder.view.setTag(position);
holder.view.setOnClickListener(this);
}
@Override
public int getItemCount() {
return apps.size();
}
@Override
public void onClick(View v) {
if (v.getTag() != null) {
int index = (Integer) v.getTag();
if (mCallback != null)
mCallback.onClick(index);
}
}
class AppHolder extends RecyclerView.ViewHolder {
final View view;
ImageView icon;
final TextView appname;
final CardView contentBg;
LinearLayout textsBg;
AppHolder(View v) {
super(v);
view = v;
icon = (ImageView) v.findViewById(R.id.appicon);
contentBg = (CardView) v.findViewById(R.id.app_item_card);
appname = (TextView) v.findViewById(R.id.appname);
textsBg = (LinearLayout) v.findViewById(R.id.texts_bg);
}
}
private void makeBlackAndWhite(ImageView iv) {
float[] colorMatrix = {
0.33f, 0.33f, 0.33f, 0, 30, //red
0.33f, 0.33f, 0.33f, 0, 30, //green
0.33f, 0.33f, 0.33f, 0, 30, //blue
0, 0, 0, 1, 0 //alpha
};
ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
iv.setColorFilter(colorFilter);
}
}
答案 0 :(得分:2)
在未安装图标时,您会将图标设为黑白图标,但视图会被重新定义,因此如果安装了colorFilter,则需要再次将其设置为彩色(在您的{ {1}}有条件的)。