【问题标题】:How to implement an interface in MainActivity by calling the method in fragment?如何通过调用fragment中的方法来实现MainActivity中的接口?
【发布时间】:2016-08-12 13:58:48
【问题描述】:

我必须在自定义适配器中定义的 MainActivity 中实现一个接口。但是该接口方法的所有相关对象都在片段中定义。那么如何在 MainActivity 和 Fragment 之间进行通信,以使接口的实现工作而不会出现空指针异常呢?

这是我必须实现的接口,它在自定义适配器中定义。

public interface UpdateMainClass{
        void updateItemList(int position);
        void updateListBackground(int position, boolean isChecked);
    }

在我的主要活动中,我覆盖了上述内容。

/**
     * <p>This method is used to remove the item from speicified position</p>
     * @param position location of the item to be removed
     */
    @Override
    public void updateItemList(int position) {
        myNewFragment.updateItemList(position);
    }

    /**
     * <p>Updates the background checkbox status in POJO class and helps to set the background color on long press.</p>
     * <p>Illegal state is checked to prevent changing of checkbox status while list is being scrolled.</p>
     * @param position position of the item in list where checkbox status is changed.
     * @param isChecked current status of the checkbox.
     */
    @Override
    public void updateListBackground(int position, boolean isChecked) {
        myNewFragment.updateListBackground(position, isChecked);
    }

这是我的 Fragment 类,我可以在其中实际实现该方法。

/**
     * <p>This method is used to remove the item from speicified position</p>
     *
     * @param position location of the item to be removed
     */
    public void updateItemList(int position) {
        myArrayList.remove(position);
        mAdapter.notifyItemRemoved(position);
    }

    /**
     * <p>Updates the background checkbox status in POJO class and helps to set the background color on long press.</p>
     * <p>Illegal state is checked to prevent changing of checkbox status while list is being scrolled.</p>
     *
     * @param position  position of the item in list where checkbox status is changed.
     * @param isChecked current status of the checkbox.
     */
    public void updateListBackground(int position, boolean isChecked) {
        try {
            if (isChecked)
                myArrayList.get(position).setCompleted(1);
            else
                myArrayList.get(position).setCompleted(0);
            mAdapter.notifyItemChanged(position);
        } catch (IllegalStateException e) {
            //do nothing
        }
    }

这显然不起作用,我最终在我的片段类中的myArrayList.get(position) 处得到一个空指针异常。那么有没有更好的编码方式呢?

【问题讨论】:

  • 你想从片段访问活动方法??
  • 其实是相反的

标签: android android-fragments interface


【解决方案1】:

这不会发生。

首先,您创建一个接口,该接口具有用于在您的片段和宿主活动之间进行通信的方法。

其次,您在主机活动中实现该接口并执行您的业务逻辑。

第三,你在片段的 onAttach() 方法中得到你的活动回调:

@Override
public void onAttach(Activity activity) {
   super.onAttach(activity);

   try {
     callback = (UpdateMainClass) activity;
   } catch (ClassCastException e) {
     throw new ClassCastException(activity.toString()
            + " must implement UpdateMainClass");
   }
}

第四,您可以通过活动的回调字段从片段中调用这些接口方法,例如:

callback.updateItemList(_data_);

这是Fragment -> Activity communication

如果您想从宿主活动中访问片段的方法,您应该从 FragmentManager 获取片段的引用。

最好的解决方案是使用 Otto、EventBus 或 RxBus 等 EventBus 库。

【讨论】:

  • 接口 UpdateMainClass 没有在片段中定义,而是在自定义适配器中。
  • 好的,那么你必须与你的 Fragment 通信的方式是:Adapter -> Activity -> Fragment。更好的方法是研究我上面提到的事件总线库。它们将为您节省大量时间!
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 2019-07-03
相关资源
最近更新 更多