use of com.codeest.geeknews.ui.main.adapter.LikeAdapter in project GeekNews by codeestX.
the class LikeFragment method initEventAndData.
@Override
protected void initEventAndData() {
mList = new ArrayList<>();
mAdapter = new LikeAdapter(mContext, mList);
rvLikeList.setLayoutManager(new LinearLayoutManager(mContext));
rvLikeList.setAdapter(mAdapter);
mCallback = new DefaultItemTouchHelpCallback(new DefaultItemTouchHelpCallback.OnItemTouchCallbackListener() {
@Override
public void onSwiped(int adapterPosition) {
// 滑动删除的时候,从数据库、数据源移除,并刷新UI
if (mList != null) {
mPresenter.deleteLikeData(mList.get(adapterPosition).getId());
mList.remove(adapterPosition);
mAdapter.notifyItemRemoved(adapterPosition);
}
}
@Override
public boolean onMove(int srcPosition, int targetPosition) {
if (mList != null) {
// 更换数据库中的数据Item的位置
boolean isPlus = srcPosition < targetPosition;
mPresenter.changeLikeTime(mList.get(srcPosition).getId(), mList.get(targetPosition).getTime(), isPlus);
// 更换数据源中的数据Item的位置
Collections.swap(mList, srcPosition, targetPosition);
// 更新UI中的Item的位置,主要是给用户看到交互效果
mAdapter.notifyItemMoved(srcPosition, targetPosition);
return true;
}
return false;
}
});
mCallback.setDragEnable(true);
mCallback.setSwipeEnable(true);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(mCallback);
itemTouchHelper.attachToRecyclerView(rvLikeList);
mPresenter.getLikeData();
}
Aggregations