android – DialogFragment中的RecyclerView – >拖放(交换)动画滞后

android – DialogFragment中的RecyclerView – >拖放(交换)动画滞后,第1张

概述我在DialogFragment中使用RecyclerView(精确地是AlertDialog),具有拖放功能(ItemTouchHelper.Callback – > onMove(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder,RecyclerView.ViewHolder target)). 问题是当我向上或向下移动( 我在DialogFragment中使用RecyclerVIEw(精确地是AlertDialog),具有拖放功能(itemtouchhelper.Callback – > onMove(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder,RecyclerVIEw.VIEwHolder target)).

问题是当我向上或向下移动(拖动)项目时动画滞后.我在Activity中尝试了与Fragment相同的RecyclerVIEw,它运行顺畅.我怀疑问题是Activitytakes全屏和DialogFragment没有(对话框周围显示模糊的背景).我想在AlertDialog中移动项目时会进行一些额外的计算.

编辑

我的适配器类:

public class listadapter extends RecyclerVIEw.Adapter<listadapter.TabItemVIEwHolder> implements itemtouchhelperAdapter {    private List<Tab> tabs;    public listadapter(List<Tab> tabs) {        this.tabs = tabs;    }    @OverrIDe    public TabItemVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {        VIEw itemVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_List_tab_List,parent,false);        return new TabItemVIEwHolder(itemVIEw);    }    @OverrIDe    public voID onBindVIEwHolder(TabItemVIEwHolder holder,final int position) {        Tab tab = tabs.get(position);        holder.tvTabname.setText(tab.getTabcategory().toString());    }    @OverrIDe    public int getItemCount() {        return tabs.size();    }    @OverrIDe    public voID onItemmove(int fromposition,int toposition) {        if (fromposition < toposition) {            for (int i = fromposition; i < toposition; i++) {                Collections.swap(tabs,i,i + 1);            }        } else {            for (int i = fromposition; i > toposition; i--) {                Collections.swap(tabs,i - 1);            }        }        notifyItemmoved(fromposition,toposition);    }    @OverrIDe    public voID onItemdismiss(int position) {        tabs.remove(position);        notifyItemRemoved(position);    }    public static class TabItemVIEwHolder extends RecyclerVIEw.VIEwHolder {        public TextVIEw tvTabname;        public TabItemVIEwHolder(final VIEw itemVIEw) {            super(itemVIEw);            tvTabname = (TextVIEw) itemVIEw.findVIEwByID(R.ID.cl__tv_tab_name);        }    }}

函数voID onItemmove(int fromposition,int toposition)和voID onItemdismiss(int position)从itemtouchhelper调用.

DialogFragment类:

public Dialog onCreateDialog(Bundle savedInstanceState) {    final RecyclerVIEw recyclerVIEw = (RecyclerVIEw) getActivity().getLayoutInflater().inflate(R.layout.recycler_vIEw,null);    listadapter listadapter = new listadapter (tabs);    recyclerVIEw.setAdapter(listadapter );    recyclerVIEw.setLayoutManager(new linearlayoutmanager(getActivity()));    SimpleitemtouchhelperCallback simpleitemtouchhelperCallback = new SimpleitemtouchhelperCallback(listadapter);    itemtouchhelper itemtouchhelper = new itemtouchhelper(simpleitemtouchhelperCallback);    itemtouchhelper.attachToRecyclerVIEw(recyclerVIEw);    return new AlertDialog.Builder(getActivity())            .setTitle(R.string.Title)            .setVIEw(recyclerVIEw)            .setPositivebutton(R.string.ok,new DialogInterface.OnClickListener() {                @OverrIDe                public voID onClick(DialogInterface dialog,int which) {                }            })            .setNegativebutton(R.string.cancel,int which) {                }            }).create();}
解决方法 问题似乎在你的itemtouchhelper.Callback实现中.尝试使用以下类重新实现拖放功能.

public class SimpleitemtouchhelperCallback extends itemtouchhelper.Callback {private final itemtouchhelperAdapter mAdapter;public SimpleitemtouchhelperCallback(itemtouchhelperAdapter adapter) {    mAdapter = adapter;}@OverrIDepublic boolean isLongPressDragEnabled() {    return true;}@OverrIDepublic boolean isItemVIEwSwipeEnabled() {    return true;}@OverrIDepublic int getMovementFlags(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder vIEwHolder) {        int dragFlags = itemtouchhelper.UP | itemtouchhelper.DOWN;        int swipeFlags = itemtouchhelper.START | itemtouchhelper.END;        return makeMovementFlags(dragFlags,swipeFlags);}@OverrIDepublic boolean onMove(RecyclerVIEw recyclerVIEw,RecyclerVIEw.VIEwHolder target) {    mAdapter.onItemmove(vIEwHolder.getAdapterposition(),target.getAdapterposition());    return true;}@OverrIDepublic voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder,int direction) {    mAdapter.onItemdismiss(vIEwHolder.getAdapterposition());}}

这是itemtouchhelper.Callback的自定义实现将启用拖放功能.您还需要在适配器上实现接口以更新数据的状态.

public interface itemtouchhelperAdapter {voID onItemmove(int fromposition,int toposition);voID onItemdismiss(int position);}

/ **
 *从{@@R_301_6862@ itemtouchhelper.Callback}收听移动或解雇事件的界面.
 *
 * @author Paul Burke(ipaulpro)
 * /

接口的适配器实现也在下面提到.

@OverrIDepublic int getItemCount() {    return data.size();}@OverrIDepublic voID onItemmove(int fromposition,int toposition) {    if (fromposition < toposition) {        for (int i = fromposition; i < toposition; i++) {            Collections.swap(data,i + 1);        }    } else {        for (int i = fromposition; i > toposition; i--) {            Collections.swap(data,i - 1);        }    }    notifyItemmoved(fromposition,toposition);}@OverrIDepublic voID onItemdismiss(int position) {    data.remove(position);    notifyItemRemoved(position);}

/ ****对话片段*** /

@Nullable@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {     super.onCreateVIEw(inflater,container,savedInstanceState);    VIEw root = inflater.inflate(R.layout.dialog_fragment_layout,false);    List = (RecyclerVIEw) root.findVIEwByID(R.ID.List);    data = getResources().getStringArray(R.array.List);    linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(getActivity());    linearlayoutmanager.setorIEntation(linearlayoutmanager.VERTICAL);    List.setLayoutManager(linearlayoutmanager);    ArrayList<String> ListData = new ArrayList<>();    ListData.addAll(Arrays.asList(data));    listadapter = new listadapter(getActivity(),ListData);    List.setAdapter(listadapter);    SimpleitemtouchhelperCallback callback = new SimpleitemtouchhelperCallback(listadapter);    itemtouchhelper touchHelper = new itemtouchhelper(callback);    touchHelper.attachToRecyclerVIEw(List);    return root;}
总结

以上是内存溢出为你收集整理的android – DialogFragment中的RecyclerView – >拖放(交换)动画滞后全部内容,希望文章能够帮你解决android – DialogFragment中的RecyclerView – >拖放(交换)动画滞后所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存