环顾该网站,其他教程发现了一个将 Material Design 应用到 android 应用程序的不错选择,它根据卡片中查看的图片选择 cardView 操作区域颜色。
在这个教程中,他们将图像从drawable导入到ciew中,我通过创建图像地址的arrayList从网站检索它,然后使用毕加索将其输入到cardView中,几乎与教程相似,不同之处在于我没有使用不同的类来解包信息并将其转换为 ArrayList。
因此,当尝试应用教程中演示的相同方法为 placeNameHolder(cardView 中图片下方的操作区域)选择颜色时,我对如何以与我的资源兼容的方式这样做感到困惑?
我将不胜感激有关如何实现这一目标和理解该方法的任何说明?
在教程 Activity_details
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_places, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Place place = new PlaceData().placeList().get(position);
holder.placeName.setText(place.name);
Picasso.with(mContext).load(place.getImageResourceId(mContext)).into(holder.placeImage);
Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), place.getImageResourceId(mContext));
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int mutedLight = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(mutedLight);
}
});
}
@Override
public int getItemCount() {
return new PlaceData().placeList().size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;
public ViewHolder(View itemView) {
super(itemView);
placeHolder = (LinearLayout) itemView.findViewById(R.id.mainHolder);
placeName = (TextView) itemView.findViewById(R.id.placeName);
placeNameHolder = (LinearLayout) itemView.findViewById(R.id.placeNameHolder);
placeImage = (ImageView) itemView.findViewById(R.id.placeImage);
placeHolder.setOnClickListener(this);
}
在我的应用程序中;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ImageView picture;
public LinearLayout placeNameHolder;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_card, parent, false));
picture = (ImageView) itemView.findViewById(R.id.placeImage);
name = (TextView) itemView.findViewById(R.id.card_title);
placeNameHolder = (LinearLayout) itemView.findViewById(R.id.placeNameHolder);
}
}
/**
* Adapter to display recycler view.
*/
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Picasso.with(mContext).load(mImages[position]).into(holder.picture);
holder.name.setText(author[position]);
Bitmap photo = BitmapFactory.decodeResource(mImages[position]);
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
}
不同的是我的资源是;
Firebase ref = new Firebase("https://wi4x4.firebaseio.com/data/images");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if(snapshot !=null) {
for (DataSnapshot child : snapshot.getChildren()) {
Log.i("MyTag", child.getValue().toString());
imagesfeedsList.add(child.child("address").getValue(String.class));
authorfeedsList.add(child.child("author").getValue(String.class));
}
Log.i("MyTag_imagesDirFinal", imagesfeedsList.toString());
mImages = imagesfeedsList.toArray(new String[imagesfeedsList.size()]);
author = authorfeedsList.toArray(new String[authorfeedsList.size()]);