android – 如何在recyclerview上启用和禁用拖放

前端之家收集整理的这篇文章主要介绍了android – 如何在recyclerview上启用和禁用拖放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Recyclerview,允许在编辑模式下拖放项目.一旦用户退出编辑模式,我需要禁用拖放功能.

我试过改变ItemTouchCallback中的东西,包括移动标志和isLongPressDragEnabled无济于事.长按继续开始拖动事件.

我目前的解决方案是分离并重新连接片段,然后再次进入编辑模式时添加回调,但是我希望有一些更清洁的东西,我想念.

打回来:

public class ItemTouchCallback extends ItemTouchHelper.Callback {


private final ItemTouchHelperAdapter mAdapter;

public ItemTouchCallback(ItemTouchHelperAdapter mAdapter) {
    this.mAdapter = mAdapter;
}

@Override
public int getMovementFlags(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) {
    int dragFlags = ItemTouchHelper.START | ItemTouchHelper.END |ItemTouchHelper.DOWN | ItemTouchHelper.UP;
    return makeMovementFlags(dragFlags,0);
}

@Override
public boolean onMove(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder,RecyclerView.ViewHolder target) {
    mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
    return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder,int direction) {

}

@Override
public boolean isLongPressDragEnabled() {
    return true;
}


@Override
public boolean isItemViewSwipeEnabled() {
    return false;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder,int actionState) {
    if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
        if (viewHolder instanceof ItemTouchHelperViewHolder) {
            ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
            itemViewHolder.onItemSelected();
        }
    }
    super.onSelectedChanged(viewHolder,actionState);
}

@Override
public void clearView(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) {
    if (viewHolder instanceof ItemTouchHelperViewHolder) {
        ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
        itemViewHolder.onItemClear();
    }
}
}

适配器:

public interface ItemTouchHelperAdapter {

void onItemMove(int fromPosition,int toPosition);
void onItemDismiss(int position);
}

分段:

@Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_rectangle_switches,container,false);

    ButterKnife.bind(this,rootView);

    // Note: Title set by AmbientPagerFragment

    layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new SwitchAdapter(switchList,dimmableList,ghostList,editMode,bus);
    recyclerView.setAdapter(adapter);

    displaySwitches();
    return rootView;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icon_edit:
            editMode = true;
            changeEditMode();
            break;
        case R.id.icon_cancel:
            editMode = false;
            changeEditMode();
            break;
    }
    return super.onOptionsItemSelected(item);
}

public void changeEditMode() {
    bus.post(new EditModeEvent(editMode));

    reattachFragment();

    displaySwitches();
    getActivity().invalidateOptionsMenu();
}

private void displaySwitches() {

    if (editMode) {
        callback = new ItemTouchCallback((ItemTouchHelperAdapter) adapter);
        mTouchHelper = new ItemTouchHelper(callback);
        mTouchHelper.attachToRecyclerView(recyclerView);
        }
    }

private void reattachFragment(){
    ft = getFragmentManager().beginTransaction();
    ft.detach(this).attach(this).commit();
}

谢谢.

解决方法

这就是我这样做的方式:
if(mode == MODE_CREATE){
  touchHelper.attachToRecyclerView(recyclerView);
}else if(mode == MODE_EDIT){
  touchHelper.attachToRecyclerView(null);
}
原文链接:https://www.f2er.com/android/309024.html

猜你在找的Android相关文章