初学者在Android应用程序开发这里,我正在制作一个游戏新闻应用程序,在回收站视图网格中显示很多游戏(项目)。 除了来自网址的图片外,所有内容都显示正确。 以下是我的一些代码:
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
public ArrayList<HashMap<String, String>> mDataSource;
protected RecyclerView mRecyclerView;
protected RecyclerView.LayoutManager mLayoutManager;
protected HomeItemAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initDataset();
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new HomeItemAdapter(this, mDataSource);
mRecyclerView.setAdapter(mAdapter);
}
private void initDataset() {
mDataSource = new ArrayList();
HashMap<String, String> h1 = new HashMap();
h1.put("name", "Super Meat Boy");
h1.put("city", "Indie");
h1.put("image_url","https://66.media.tumblr.com/tumblr_ldi4d79gyD1qd9jlto1_500.jpg");
mDataSource.add(h1);
HomeItemAdapter.java
public class HomeItemAdapter extends RecyclerView.Adapter<HomeItemAdapter.ViewHolder> {
private static final String TAG = "NTR-HomeItemAdapter";
public ArrayList<HashMap<String, String>> mDataSource;
private Context mContext;
public HomeItemAdapter(Context context, ArrayList<HashMap<String, String>> mDataSource) {
this.mDataSource = mDataSource;
this.mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_home_item, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
Target target;
Log.d(TAG, "Element " + position + " set.");
viewHolder.getName().setText(mDataSource.get(position).get("name"));
viewHolder.getCity().setText(mDataSource.get(position).get("city"));
//Get Marquee
viewHolder.getName().setSingleLine();
viewHolder.getName().setEllipsize(TextUtils.TruncateAt.MARQUEE);
viewHolder.getName().setHorizontallyScrolling(true);
viewHolder.getName().setFocusableInTouchMode(true);
viewHolder.getName().setSelected(true);
viewHolder.getName().setMarqueeRepeatLimit(-1);
viewHolder.getCity().setSingleLine();
viewHolder.getCity().setEllipsize(TextUtils.TruncateAt.MARQUEE);
viewHolder.getCity().setHorizontallyScrolling(true);
viewHolder.getCity().setFocusableInTouchMode(true);
viewHolder.getCity().setSelected(true);
viewHolder.getCity().setMarqueeRepeatLimit(-1);
// Get Dynamic Color
target = new com.squareup.picasso.Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
if (palette !=null) {
viewHolder.getImageCover().setImageBitmap(bitmap);
Palette.Swatch vibrant = palette.getVibrantSwatch();
if(vibrant!=null) {
viewHolder.getCardItemHome().setBackgroundColor(vibrant.getRgb());
viewHolder.getName().setTextColor(vibrant.getTitleTextColor());
viewHolder.getCity().setTextColor(vibrant.getTitleTextColor());
}
}
}
});
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
viewHolder.getImageCover().setImageResource(R.drawable.cover);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Log.d(TAG,mDataSource.get(position).get("image_url"));
Picasso.with(mContext)
.load(mDataSource.get(position).get("image_url"))
.into(target);
}
@Override
public int getItemCount() {
return mDataSource.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final CardView cardItemHome;
private final ImageView imgCover;
private final TextView txtName;
private final TextView txtCity;
public ViewHolder(View v) {
super(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Element " + getPosition() + " clicked.");
}
});
cardItemHome = (CardView) v.findViewById(R.id.card_item_home);
txtName = (TextView) v.findViewById(R.id.txt_name);
txtCity = (TextView) v.findViewById(R.id.txt_city);
imgCover = (ImageView) v.findViewById(R.id.img_cover);
}
public CardView getCardItemHome() {
return cardItemHome;
}
public TextView getName() {
return txtName;
}
public TextView getCity() {
return txtCity;
}
public ImageView getImageCover() {
return imgCover;
}
}
}