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

android – 如何在recyclerview上启用和禁用拖放,第1张

概述我有一个Recyclerview,允许在编辑模式下拖放项目.一旦用户退出编辑模式,我需要禁用拖放功能. 我试过改变ItemTouchCallback中的东西,包括移动标志和isLongPressDragEnabled无济于事.长按继续开始拖动事件. 我目前的解决方案是分离并重新连接片段,然后再次进入编辑模式时添加回调,但是我希望有一些更清洁的东西,我想念. 打回来: public class It 我有一个RecyclervIEw,允许在编辑模式下拖放项目.一旦用户退出编辑模式,我需要禁用拖放功能.

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

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

打回来:

public class ItemtouchCallback extends itemtouchhelper.Callback {private final itemtouchhelperAdapter mAdapter;public ItemtouchCallback(itemtouchhelperAdapter mAdapter) {    this.mAdapter = mAdapter;}@OverrIDepublic int getMovementFlags(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder) {    int dragFlags = itemtouchhelper.START | itemtouchhelper.END |itemtouchhelper.DOWN | itemtouchhelper.UP;    return makeMovementFlags(dragFlags,0);}@OverrIDepublic boolean onMove(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder,RecyclerVIEw.VIEwHolder target) {    mAdapter.onItemmove(vIEwHolder.getAdapterposition(),target.getAdapterposition());    return true;}@OverrIDepublic voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder,int direction) {}@OverrIDepublic boolean isLongPressDragEnabled() {    return true;}@OverrIDepublic boolean isItemVIEwSwipeEnabled() {    return false;}@OverrIDepublic 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);}@OverrIDepublic 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;}@OverrIDepublic 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);}
总结

以上是内存溢出为你收集整理的android – 如何在recyclerview上启用和禁用拖放全部内容,希望文章能够帮你解决android – 如何在recyclerview上启用和禁用拖放所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存