我正在使用this庫來處理我的RecycleView
以添加項目。我有一個ImageView
和一個類的佈局。我有一個對象列表,我想將它們添加到RecycleView
。Recycleviewer - 如何重用佈局?
所以基本上是這樣的:
我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);
任何想法?謝謝。
它是否在logcat中引發任何錯誤? –
@ReazMurshed - 不,它正常啓動活動,加載數據(列表),甚至將其加載到RecycleView中,它只是不顯示圖像 –
user3024750
請參閱下面的答案。這裏有一個潛在的錯誤。 –