生成RecyclerView和AbsListView適配器:AnnotatedAdapter
AnnotatedAdapter是一個只需編寫少量代碼的Android注釋處理器,可生成RecyclerView和AbsListView適配器。
Check out the sample folder, but basically you have to create an adapter class like this and annotate the view types with @ViewType and provide some more information in its annotation:
public class SampleAdapter extends SupportAnnotatedAdapter
implements SampleAdapterBinder {
/**
* Specify a view type by annotating a public final int with @ViewType.
* Like for any other adapter the view types must be start with an integer = 0
*/
@ViewType(
layout = R.layout.row_medium, // The layout that will be inflated for this view type
fields = { // The fields of the view holder
@Field(
id = R.id.textView, // The id of this view
name = "text", // The name of this field in the generated ViewHolder
type = TextView.class) // The type (class) of view in the generated view holder
}
}
)
public final int mediumRow = 0; // The annotated ViewType constant
@ViewType(
layout = R.layout.row_with_pic,
fields = {
@Field(id = R.id.textView, name = "text", type = TextView.class),
@Field(id = R.id.imageView, name = "image", type = ImageView.class)
}
)
public final int rowWithPic = 1;
List<String> items;
public SampleAdapter(Context context, List<String> items) {
super(context);
this.items = items;
}
/**
* Get the number of items like in any other adapter
*/
@Override public int getItemCount() {
return items == null ? 0 : items.size();
}
/**
* Determine the view type for the cell at position (like you would do in any other adpater)
*/
@Override public int getItemViewType(int position) {
if (position % 2 == 0)
return mediumRow;
else
return rowWithPic;
}
}
/**
* Bind the data to this view type mediumRow; MediumRowViewHolder was generated
*/
@Override public void bindViewHolder(SampleAdapterHolders.MediumRowViewHolder vh,
int position) {
String str = items.get(position);
vh.text.setText(str);
}
/**
* Bind the data to this view type rowWithPic; RowWithPicViewHolder was generated
*/
@Override public void bindViewHolder(SampleAdapterHolders.RowWithPicViewHolder vh,
int position) {
String str = items.get(position);
vh.text.setText(str);
vh.image.setImageResource(R.drawable.ic_launcher);
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!