This is the bare minimum boilerplate for quickly creating a RecyclerView.
Usage:
In your build.gradle
// implementation "com.android.support:recyclerview-v7:23.0.1"
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha04'
In code
mRecyclerView = (RecyclerView) findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new BasicListAdapter(this));
The LayoutManager can also be set like so:
<androidx.recyclerview.widget.RecyclerView
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Adapter
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
protected List<Item> mData = new ArrayList<Item>();
protected LayoutInflator mInflater;
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
return new MyViewHolder(
mInflater.inflate(R.layout.li_item, viewGroup, false)
);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.bind(mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
}
ViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.label);
}
public void bind(Entity entity){
mTextView.setText(entity.getTitle());
}
}