Skip to content

Instantly share code, notes, and snippets.

@nisrulz
Last active March 11, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisrulz/886f3f591b1e7b3a6c34 to your computer and use it in GitHub Desktop.
Save nisrulz/886f3f591b1e7b3a6c34 to your computer and use it in GitHub Desktop.
RecyclerView implementation
// USAGE
// Data set
final ArrayList<ViewModel> viewModelArrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
viewModelArrayList.add(new Order(i, "VM Name " + i));
}
// Recyclerview
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
// Optimization
recyclerView.setHasFixedSize(true);
// Adapter
OrderAdapter adapter = new OrderAdapter(orderArrayList);
recyclerView.setAdapter(adapter);
// Setup onItemTouchHandler
ItemTouchHelper.Callback callback = new RVOnItemtouchHelper(adapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(recyclerView);
// Layout Manager
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Set the divider
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
// Handle on item click
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener
.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// Do something
}
})
);
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin +
Math.round(ViewCompat.getTranslationY(child));
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin +
Math.round(ViewCompat.getTranslationX(child));
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--Add the below attribute when working with collapsible toolbar, donot put recyclerview under NestedScrollView-->
<!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.Viewholder> {
private List<ViewModel> viewModelList;
public RVAdapter(List<ViewModel> viewModelList) {
this.viewModelList = viewModelList;
}
@Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View modelView = inflater.inflate(R.layout.item_view_rv, parent, false);
return new Viewholder(modelView);
}
@Override
public void onBindViewHolder(Viewholder holder, int position) {
ViewModel viewModel = viewModelList.get(position);
holder.txt_name.setText(viewModel.getName());
holder.txt_id.setText(String.valueOf(viewModel.get_id()));
}
@Override
public int getItemCount() {
return viewModelList.size();
}
public static class Viewholder extends RecyclerView.ViewHolder{
TextView txt_name, txt_id;
public Viewholder(View itemView) {
super(itemView);
txt_id = (TextView) itemView.findViewById(R.id.textView_id);
txt_name = (TextView) itemView.findViewById(R.id.textView_name);
}
}
public void remove(int position) {
viewModelList.remove(position);
notifyItemRemoved(position);
}
public void swap(int firstPosition, int secondPosition) {
Collections.swap(viewModelList, firstPosition, secondPosition);
notifyItemMoved(firstPosition, secondPosition);
}
}
public class RVOnItemtouchHelper extends ItemTouchHelper.SimpleCallback {
private RVAdapter rvAdapter;
public RVOnItemtouchHelper(RVAdapter rvAdapter) {
super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.rvAdapter = rvAdapter;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder dragged, RecyclerView.ViewHolder target) {
rvAdapter.swap(dragged.getAdapterPosition(), target.getAdapterPosition());
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder swipped, int direction) {
switch (direction) {
case ItemTouchHelper.LEFT:
//Remove item
rvAdapter.remove(swipped.getAdapterPosition());
System.out.println("Swipped Left");
break;
case ItemTouchHelper.RIGHT:
System.out.println("Swipped Right");
break;
}
}
}
public class ViewModel {
int _id;
String name;
public ViewModel(int _id, String name) {
this._id = _id;
this.name = name;
}
public int get_id() {
return _id;
}
public String getName() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment