Last active
May 29, 2022 16:55
-
-
Save omeraydindev/209793d25d7933607f15d247ce46fc21 to your computer and use it in GitHub Desktop.
Easy-to-use generic helper class to help reduce RecyclerView adapter boilerplate by wrapping an adapter internally. This can be useful for when you just need a basic, easy, fast adapter solution without the usual boilerplate that we've grown used to with RecyclerView adapters. Supports drag-drop reordering out of the box. See below for an example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * This is free and unencumbered software released into the public domain. | |
| * For more information, please refer to <http://unlicense.org/> | |
| */ | |
| import android.content.Context; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import androidx.annotation.NonNull; | |
| import androidx.recyclerview.widget.ItemTouchHelper; | |
| import androidx.recyclerview.widget.RecyclerView; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| public class FastAdapter<T> { | |
| private final RVAdapter adapter; | |
| public FastAdapter(Context context, int resource, ArrayList<T> data, BindListener<T> bindListener) { | |
| adapter = new RVAdapter(context, resource, data, bindListener); | |
| } | |
| public void into(RecyclerView recyclerView) { | |
| into(recyclerView, false); | |
| } | |
| public void into(RecyclerView recyclerView, boolean dragDrop) { | |
| recyclerView.setAdapter(adapter); | |
| if (dragDrop) { | |
| attachDragDrop(recyclerView); | |
| } | |
| } | |
| public ArrayList<T> getList() { | |
| return adapter.mData; | |
| } | |
| public void refresh() { | |
| adapter.notifyDataSetChanged(); | |
| } | |
| public interface BindListener<T> { | |
| void onBind(View rootView, T item, int pos); | |
| } | |
| private void attachDragDrop(RecyclerView recyclerView) { | |
| ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END, 0) { | |
| @Override | |
| public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { | |
| int fromPosition = viewHolder.getAdapterPosition(); | |
| int toPosition = target.getAdapterPosition(); | |
| Collections.swap(adapter.mData, fromPosition, toPosition); | |
| recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition); | |
| return false; | |
| } | |
| @Override | |
| public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {} | |
| }; | |
| ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback); | |
| itemTouchHelper.attachToRecyclerView(recyclerView); | |
| } | |
| private class RVAdapter extends RecyclerView.Adapter<RVAdapter.ViewHolder> { | |
| private final int resource; | |
| public final ArrayList<T> mData; | |
| private final LayoutInflater mInflater; | |
| private final BindListener<T> bindListener; | |
| public RVAdapter(Context context, int resource, ArrayList<T> data, BindListener<T> bindListener) { | |
| this.resource = resource; | |
| this.mInflater = LayoutInflater.from(context); | |
| this.mData = data; | |
| this.bindListener = bindListener; | |
| } | |
| @NonNull | |
| @Override | |
| public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
| View view = mInflater.inflate(resource, parent, false); | |
| return new ViewHolder(view); | |
| } | |
| @Override | |
| public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | |
| T item = getItem(position); | |
| bindListener.onBind(holder.root, item, position); | |
| } | |
| @Override | |
| public int getItemCount() { | |
| return mData.size(); | |
| } | |
| private class ViewHolder extends RecyclerView.ViewHolder { | |
| View root; | |
| ViewHolder(View itemView) { | |
| super(itemView); | |
| root = itemView; | |
| } | |
| } | |
| T getItem(int id) { | |
| return mData.get(id); | |
| } | |
| } | |
| } |
Author
Under what license's the code? If you don't really care, I'd recommend The Unlicense. Ideally, a single comment like this in the systemd source repo is enough.
Thanks, added The Unlicense as the license. It goes without saying but I'm going to say it anyway -- same license applies to the example snippet above.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Under what license's the code? If you don't really care, I'd recommend The Unlicense. Ideally, a single comment like this in the systemd source repo is enough.