生成RecyclerView和AbsListView适配器:AnnotatedAdapter
jopen
10年前
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); } }