我正在使用 this处理我的图书馆 RecycleView
添加项目。我有一个 ImageView
的布局和一个类(class)。我有一个对象列表,我想将它们添加到 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>
还有我的类(class)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);
有什么想法吗?谢谢。
最佳答案
我在您的 ViewHolder
中发现了一个潜在的错误,尽管我真的不知道它在 MainActivity
中是如何工作的。
您已经在 getLayoutRes
函数中设置了 item_main.xml
,并且在 ViewHolder
中您获得了 ImageView
来自错误布局 ID 的引用。
因此您的 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);
}
}
更新
由于您在问题中错误地输入了错误的布局 ID,因此我认为该错误不存在。但是无论如何,我在这里发现了您可能需要检查的其他问题。
在您的 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 来加载图像。您也可以查看用于图像加载的 Glide。
Glide.with(viewHolder.itemView.getContext())
.load(linkToImage)
.into(viewHolder.image);
关于java - Recycleviewer - 如何重用布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41001656/