Search in sources :

Example 1 with LikeAdapter

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();
}
Also used : DefaultItemTouchHelpCallback(com.codeest.geeknews.widget.DefaultItemTouchHelpCallback) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) LikeAdapter(com.codeest.geeknews.ui.main.adapter.LikeAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Aggregations

LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 LikeAdapter (com.codeest.geeknews.ui.main.adapter.LikeAdapter)1 DefaultItemTouchHelpCallback (com.codeest.geeknews.widget.DefaultItemTouchHelpCallback)1