Git Product home page Git Product logo

radapter's Introduction

RAdapter

Auto generate ListView or RecyclerView Adapter Code

Step 1

compile 'io.github.iamyours:radapter-lib:0.0.2'
annotationProcessor 'io.github.iamyours:radapter-compiler:0.0.2'

Step 2

Create XXXViewHolder for ListView or RecyclerView

@BindLayout(R.layout.item_student)// for RecyclerView use @BindLayout(value = R.layout.item_student,isRecycler = true)
public class StudentHolder extends BaseViewHolder<Student> {
    @Inject
    CallBack callBack2;

    boolean readOnly;
    @BindView(R.id.textView)//ButterKnife Annotation
    TextView tv;


    public interface CallBack {
        void call(Student student);
    }

    @Override
    public void bind(final Student item, int position) {
        tv.setText(item.name);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack2 != null) callBack2.call(item);
            }
        });
    }
}

Step 3

Create BaseViewHolder extends RViewHolder

  public abstract class BaseViewHolder<T> extends RViewHolder<T> {
    @Override
    public void setRoot(View root) {
        super.setRoot(root);
        ButterKnife.bind(this,root);//
    }
}

For ListView will generate code

public final class StudentRAdapter extends BaseAdapter {
  private Context mContext;

  private List<Student> mData;

  private StudentHolder.CallBack callBack2;

  public StudentRAdapter(Context mContext, List<Student> mData) {
    this.mContext = mContext;
    this.mData = mData;
  }

  @Override
  public int getCount() {
    return mData.size();
  }

  @Override
  public Object getItem(int position) {
    return mData.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    StudentHolder holder;
    if(convertView==null) {
      holder = new StudentHolder();
      convertView = LayoutInflater.from(mContext).inflate(2130968604, null);
      holder.setRoot(convertView);
      holder.setContext(mContext);
      holder.callBack2 = callBack2;
    }
    else{
       holder = (StudentHolder)convertView.getTag();
    }
    holder.bind(mData.get(position), position);
    return convertView;
  }

  public void setCallBack2(StudentHolder.CallBack callBack2) {
    this.callBack2 = callBack2;
  }
}

For RecyclerView will generate code

  public final class StudentRAdapter extends RecyclerView.Adapter<StudentRAdapter.ViewHolder> {
  private Context mContext;

  private List<Student> mData;

  private StudentHolder.CallBack callBack2;

  public StudentRAdapter(Context mContext, List<Student> mData) {
    this.mContext = mContext;
    this.mData = mData;
  }

  @Override
  public StudentRAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(2130968604, parent,false);
    ViewHolder holder = new ViewHolder(v);
    holder.mHolder.setContext(mContext);
    holder.mHolder.callBack2 = callBack2;
    return holder;
  }

  @Override
  public void onBindViewHolder(StudentRAdapter.ViewHolder holder, int position) {
    holder.mHolder.bind(mData.get(position),position);;
  }

  @Override
  public int getItemCount() {
    return mData.size();
  }

  public void setCallBack2(StudentHolder.CallBack callBack2) {
    this.callBack2 = callBack2;
  }

  public static final class ViewHolder extends RecyclerView.ViewHolder {
    StudentHolder mHolder;

    public ViewHolder(View view) {
      super(view);
      mHolder = new StudentHolder();
      mHolder.setRoot(view);
    }
  }
}

radapter's People

Contributors

iamyours avatar yan127422 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

annocx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.