动态更新片段

编程入门 行业动态 更新时间:2024-10-10 11:30:46
本文介绍了动态更新片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我基本上有一个拥有多个选项卡的MainActivity。每个选项卡都是一个ShowListFragment,它扩展了Fragment。现在,每个选项卡都包含从数据库中获取的数据。我有一个移动按钮,可以将数据从一个标签移动到另一个标签: onClick(DialogInterface对话框,int listIndex){ database.add(listIndex,object); database.remove(listIndex,object); }

片段不会直接更新,但在标签之间轻扫几次(正好3)。点击按钮后,我如何强制片段更新?我不想通过ViewPager中的onPageSelected来管理它,因为它不会更新我目前正在使用的片段,但是在我已经刷入下一个片段之后。而且我也不想在每次刷卡后更新数据。

我知道我可能需要使用某种观察者模式,例如:在扩展另一个类的时候,如何使类扩展Observable? b $ b但是,我仍然不确定如何直接更新片段,以及如何将观察者/事件模式应用于我的应用程序。

解决方案

由于片段可以通过 getActivity()轻松访问活动,因此我将使活动成为调度更新的中心中心。

听起来您已经有了数据库处理的持久性部分,并且您只需要一些更新事件。所以这里有:

  • 定义一个监听器接口。我通常将它作为活动中的内部接口:

    public interface DataUpdateListener { void onDataUpdate();

  • 将数据结构添加到您的活动中以跟踪监听者:

    私人列表< DataUpdateListener> mListeners;

    不要忘记在构造函数中初始化:

    mListeners = new ArrayList<>();

  • 将注册/取消注册方法添加到活动中:

    public synchronized void registerDataUpdateListener(DataUpdateListener listener){ mListeners.add(listener); } public synchronized void unregisterDataUpdateListener(DataUpdateListener listener){ mListeners.remove(listener); }

  • 将event方法添加到您的活动中:

    public synchronized void dataUpdated(){ for(DataUpdateListener listener:mListeners){ listener.onDataUpdate(); $ / code $
  • 您的片段是否实现了DataUpdateListener:

  • p>

    public class MyFragment extends Fragment implements DataUpdateListener {

    并实现该方法

    @Override public void onDataUpdate(){ //将您的用户界面更新逻辑放在这里 code $ <$ $ $ $ $ $ $ code> onAttach()和 onDestroy()注册/取消注册:

    @Override public void onAttach(Activity activity){ super.onAttach(activity); ((MainActivity)activity).registerDataUpdateListener(this); } @Override public void onDestroy(){ super.onDestroy(); ((MainActivity)getActivity())。unregisterDataUpdateListener(this);

  • 在片段的UI更新事件中触发事件:

    @Override public void onClick(DialogInterface对话框,int listIndex){ database.add(listIndex,object) ; database.remove(listIndex,object); ((MainActivity)getActivity())。dataUpdated(); }

  • I basically have a MainActivity that has multiple tabs. Each tab is a ShowListFragment and it extends Fragment. Now, each tab contains data that I fetch from a database. I have a MOVE-button that moves data from one tab to another in each Fragment:

    @Override public void onClick(DialogInterface dialog, int listIndex) { database.add(listIndex,object); database.remove(listIndex,object); }

    The fragment does not update directly, but after a few swipes in between the tabs (3 exactly). How do I force the Fragment to update instantaneous after I clicked the button? I don't want to manage it through onPageSelected in the ViewPager, since it does not update the fragment I'm currently on, but after I've swiped to the next fragment. And also I don't want to update the data after each swipe.

    I know that I maybe need to use some kind of observer pattern like this: How do I make a Class extend Observable when it has extended another class too? But still, I'm still not sure how to update the fragment directly, and how to apply the observer/event pattern in my application.

    解决方案

    Since the fragments can access the activity easily enough with getActivity(), I would make the activity be the central hub for dispatching updates.

    It sounds like you already have the persistence part handled with the database and all you need is some update events. So here goes:

  • Define a listener interface. I usually do this as an inner interface within the activity:

    public interface DataUpdateListener { void onDataUpdate(); }

  • Add a data structure to your activity to keep track of listeners:

    private List<DataUpdateListener> mListeners;

    Don't forget to initialize in the constructor:

    mListeners = new ArrayList<>();

  • Add the register/unregister methods to the activity:

    public synchronized void registerDataUpdateListener(DataUpdateListener listener) { mListeners.add(listener); } public synchronized void unregisterDataUpdateListener(DataUpdateListener listener) { mListeners.remove(listener); }

  • Add the event method to your activity:

    public synchronized void dataUpdated() { for (DataUpdateListener listener : mListeners) { listener.onDataUpdate(); } }

  • Have your fragments implement DataUpdateListener:

    public class MyFragment extends Fragment implements DataUpdateListener {

    and implement the method

    @Override public void onDataUpdate() { // put your UI update logic here }

  • Override onAttach() and onDestroy() in the fragments to register/unregister:

    @Override public void onAttach(Activity activity) { super.onAttach(activity); ((MainActivity) activity).registerDataUpdateListener(this); } @Override public void onDestroy() { super.onDestroy(); ((MainActivity) getActivity()).unregisterDataUpdateListener(this); }

  • Fire the event in your fragment's UI update event:

    @Override public void onClick(DialogInterface dialog, int listIndex) { database.add(listIndex,object); database.remove(listIndex,object); ((MainActivity) getActivity()).dataUpdated(); }

  • 更多推荐

    动态更新片段

    本文发布于:2023-11-27 14:21:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1638434.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:片段   动态

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!