0
我有ViewPager
與三個Fragment
s幾乎相同。他們在每張圖片下方顯示一張GridLayout
圖片和文字。我已經完成了其中一個代碼。 我使用Retrofit
服務從服務器獲取數據。除了1-2個字段外,對象是相同的,我只需要它們進行反序列化。重複使用相同的適配器多個幾乎相似的片段
的對象中的一個的實施例:
public class TagsItem implements Serializable{
@SerializedName("post_id")
private String postId;
@SerializedName("account_id")
private int accountId;
private String tags;
private String description;
private String image;
private String name;
// removed getters/setters for brevity
}
我只需要從每一類圖像和名稱。
我想使用,如果它們中的每一個可能都有相同的適配器,因爲它們不需要爲每個項目單獨佈局。這只是圖像和名稱。
我該如何以更通用的形式聲明適配器,以便它可以用於所有3?
眼下適配器的代碼是:
public class TagsAdapter extends RecyclerView.Adapter<TagsAdapter.ViewHolder> {
private Context context;
private List<TagsItem> dataset;
public TagsAdapter(Context context) {
this.context = context;
dataset = new ArrayList<>();
}
// more adapter code
}
正如你可以看到它的名字是TagsAdapter
和數據集類型爲TagsItem