23

让我们考虑一个我有Fragment A和的情况Fragment B

B声明:

public interface MyInterface {
    public void onTrigger(int position);
}

A实现这个接口。

Fragment B入堆栈时,我应该如何传递Fragment A对它的引用,Bundle以便在需要时A获取onTrigger回调。

我的用例场景是AListView物品和物品。两者都包含相同的项目,当用户在弹出之前离开时,它应该触发回调以更新其位置以匹配寻呼机位置。BViewPagerB -> ABAListViewB

谢谢。

4

6 回答 6

30
Passing interface to Fragment

我想你是在两个人之间交流Fragment

为此,您可以查看与其他片段通信

public class FragmentB extends Fragment{
    MyInterface mCallback;

    // Container Activity must implement this interface
    public interface MyInterface {
        public void onTrigger();
    }

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

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (MyInterface ) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement MyInterface ");
        }
    }

    ...
}
于 2014-04-30T05:29:04.753 回答
6

对于 Kotlin 1.0.0-beta-3595

interface SomeCallback {}

class SomeFragment() : Fragment(){

    var callback : SomeCallback? = null //some might want late init, but I think this way is safer

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        callback = activity as? SomeCallback //returns null if not type 'SomeCallback'

        return inflater!!.inflate(R.layout.frag_some_view, container, false);
    }
}
于 2015-12-18T03:50:28.787 回答
1

两个片段只通过一个活动进行通信是最佳的。因此,您可以在 Fragment B 中定义一个在活动中实现的接口。然后在活动中,在接口方法中定义您希望在片段 A 中发生的事情。

在片段 B 中,

MyInterface mCallback;
 public interface MyInterface {
        void onTrigger(int position);
    }

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (MyInterface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement MyInterface");
        }
}

判断用户是否从B到A的方法

public void onChangeFragment(int position){
//other logic here
 mCallback.onTrigger(position);
}

在活动中,

public void onTrigger(int position) {
    //Find listview in fragment A
    listView.smoothScrollToPosition(position);
    }

祝你好运!

于 2016-07-15T00:35:51.903 回答
0

使用@Amit 的答案,并适应 OPs 问题,这里是所有相关代码:

public class FragmentA extends BaseFragment implements MyInterface {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // THIS IS JUST AN EXAMPLE OF WHERE YOU MIGHT CREATE FragmentB
        FragmentB myFragmentB = new FragmentB();        
    }


    void onTrigger(int position){
        // My Callback Happens Here!
    }
}

...

public class FragmentB extends BaseFragment {

    private MyInterface callback;

    public interface MyInterface {
        void onTrigger(int position);
    }   

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

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            callback = (MyInterface ) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement MyInterface");
        }
    }
}
于 2014-11-27T15:15:13.833 回答
0

我认为你应该使用沟通,正如我在下面所写的。此代码来自Fragments 之间通信的这个 Android Dev 页面

头条新闻片段

public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;

public void setOnHeadlineSelectedListener(Activity activity) {
    mCallback = activity;
}

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

// ...
}

主要活动

public static class MainActivity extends Activity
    implements HeadlinesFragment.OnHeadlineSelectedListener{
// ...

@Override
public void onAttachFragment(Fragment fragment) {
    if (fragment instanceof HeadlinesFragment) {
        HeadlinesFragment headlinesFragment = (HeadlinesFragment) fragment;
        headlinesFragment.setOnHeadlineSelectedListener(this);
    }
}

public static class MainActivity extends Activity
    implements HeadlinesFragment.OnHeadlineSelectedListener {
...

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the HeadlinesFragment
    // Do something here to display that article
}
于 2018-11-20T23:12:46.333 回答
0

您可以通过这种方式创建回调接口。

 var screenVisibility=activity as YourActivity
 screenVisibility.setScreenVisibility("which screen you want")
于 2021-08-24T09:56:36.433 回答