Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2017 07:24
Show Gist options
  • Save anonymous/1fd67d80a17305757b80792f89db7113 to your computer and use it in GitHub Desktop.
Save anonymous/1fd67d80a17305757b80792f89db7113 to your computer and use it in GitHub Desktop.
public class HoppyCastPostingActivity extends BaseClass
{
ArrayList<CategoryModel> categoryArrayList;
ArrayList<CastBoxBlock> blockList;
CastDataPostAdapter blockAdapter;
public ItemTouchHelper mItemTouchHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hoppy_cast_posting);
//other code
setupRecyclerView();
}
private void setupRecyclerView()
{
try
{
blockList = new ArrayList<CastBoxBlock>();
blockList.add(new CastBoxBlock("0"));
blockAdapter = new CastDataPostAdapter(activity, blockList, blockItemInterface);
if (blockAdapter != null)
{
Log.e(TAG, "RecyclerView is ready. Set :)....");
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity);
recyclerViewPostCastData.setLayoutManager(mLayoutManager);
recyclerViewPostCastData.setItemAnimator(new DefaultItemAnimator());
recyclerViewPostCastData.setAdapter(blockAdapter);
blockAdapter.notifyDataSetChanged();
}
}catch (Exception e){
constant.printError(TAG, "setupRecyclerView()");
e.printStackTrace();
}
}
CastBoxBlockItemInterface blockItemInterface = new CastBoxBlockItemInterface() {
@Override
public void blockMoveToUp(ArrayList<CastBoxBlock> blockList, int position, String index) {
Log.e(TAG, "blockMoveToUp() method is called.....");
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(_ithCallback);
itemTouchHelper.attachToRecyclerView(recyclerViewPostCastData);
}
@Override
public void blockMoveToDown(ArrayList<CastBoxBlock> blockList, int position, String index) {
Log.e(TAG, "blockMoveToDown() method is called.....");
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(_ithCallback);
itemTouchHelper.attachToRecyclerView(recyclerViewPostCastData);
}
};
ItemTouchHelper.Callback _ithCallback = new ItemTouchHelper.Callback() {
//and in your imlpementaion of
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
// get the viewHolder's and target's positions in your adapter data, swap them
final int fromPosition = viewHolder.getAdapterPosition();
final int toPosition = target.getAdapterPosition();
if (fromPosition < toPosition)
{
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(blockList, i, i+1);
}
}
else
{
for (int i = fromPosition; i> toPosition; i--) {
Collections.swap(blockList, i, i-1);
}
}
blockAdapter.notifyItemChanged(fromPosition, toPosition);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//TODO
}
//defines the enabled move directions in each state (idle, swiping, dragging).
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.START | ItemTouchHelper.END);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment