我使用这库来处理我的RecycleView来添加项目。我有一个ImageView和一个类的布局。我有一个对象列表,我想将它们添加到RecycleView中。
基本上是这样的:
我的item_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ImageView
android:id="@+id/cover_image"
android:layout_width="180dp"
android:layout_height="264dp"
android:scaleType="centerCrop" />
</FrameLayout>我的activity_main.xml (不是全部):
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>还有我的班级MyObject
public class MyObject extends AbstractItem<MyObject, MyObject.ViewHolder> implements Serializable {
@SerializedName(value="id")
private int id;
@SerializedName(value="name")
private String name;
@Expose
private String linkToImage;
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int getType() {
return R.id.image;
}
//The layout to be used for this type of item
@Override
public int getLayoutRes() {
return R.layout.item_main;
}
@Override
public void bindView(ViewHolder viewHolder, List<Object> payloads) {
//call super so the selection is already handled for you
super.bindView(viewHolder, payloads);
//bind our data
//set the text for the name
Picasso.with(viewHolder.itemView.getContext())
.load(linkToImage)
.into(viewHolder.image);
}
//The viewHolder used for this item. This viewHolder is always reused by the RecyclerView so scrolling is blazing fast
protected static class ViewHolder extends RecyclerView.ViewHolder {
protected ImageView image;
public ViewHolder(View view) {
super(view);
this.image = (ImageView) view.findViewById(R.id.cover_image);
}
}
}效果很好。现在的问题是,我希望在另一个Activity中使用不同的RecycleView,但是使用相同的List<MyObject>和相同的item_main.xml来重用它。
我试图使用与MainActivity中相同的代码。它似乎添加了项(它在onClick中打印出名称),但它不加载图像。
这是我在MainActivity中的代码
RecyclerView mRecyclerView;
mRecyclerView = (RecyclerView) findViewById(R.id.mainRecycleView);
mRecyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));
FastItemAdapter fastAdapter = new FastItemAdapter();
mRecyclerView.setAdapter(fastAdapter);
fastAdapter.add(listOfMyObjects);
DefaultItemAnimator animator = new DefaultItemAnimator() {
@Override
public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
return true;
}
};
fastAdapter.withSelectable(true);
fastAdapter.withOnClickListener(new FastAdapter.OnClickListener<MyObject>() {
@Override
public boolean onClick(View v, IAdapter<MyObject> adapter, MyObjectitem, int position) {
Log.d("DEBUG", item.getName());
return true;
}
});
mRecyclerView.setItemAnimator(animator);有什么想法吗?谢谢。
发布于 2016-12-06 18:04:26
我在您的ViewHolder中发现了一个潜在的bug,尽管我真的不知道它在MainActivity中是如何工作的。
在您的item_main.xml函数中设置了getLayoutRes,在ViewHolder中,您从错误的布局id中获得了ImageView引用。
所以你的ViewHolder应该是这样的。
protected static class ViewHolder extends RecyclerView.ViewHolder {
protected ImageView image;
public ViewHolder(View view) {
super(view);
// Set the proper layout id cover_image here
this.image = (ImageView) view.findViewById(R.id.cover_image);
}
}更新
由于您错误地在问题中输入了错误的布局标识,所以我认为错误并不存在。但是无论如何,我在这里发现了其他的问题,你可能需要检查。
在MainActivity中,首先设置适配器,然后在适配器中添加对象。所以你可以这么做
// Add the items first
fastAdapter.add(listOfMyObjects);
// Then set the adapter
mRecyclerView.setAdapter(fastAdapter);或者你可以用你的方式
mRecyclerView.setAdapter(fastAdapter);
fastAdapter.add(listOfMyObjects);
// Notify the adapter that new elements have added
fastAdapter.notifyDatasetChanged();请检查图像链接是否有效。
就我个人而言,我使用幻灯片加载图像。您可能会看到幻灯片的图像加载也。
Glide.with(viewHolder.itemView.getContext())
.load(linkToImage)
.into(viewHolder.image);https://stackoverflow.com/questions/41001656
复制相似问题