安卓:难以启动的一个片段里的片段 [英] Android: Difficulty starting a fragment inside of a fragment

查看:148
本文介绍了安卓:难以启动的一个片段里的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我previously问了一个问题,并得到了一些帮助,但最终,我决定,我需要更加清晰。

So, I previously asked a question, and got a bit of help, but ultimately, I decided that I needed to be more clear.

基本上,我有一个应用程序。主要活动是一个标签式的活动/刷卡视图。第一页包含Main_Fragment,它由其他两个片段(A和B)在其中的。片段A包含一个按钮,我想有当pressed,将推出不同的片段要么去ontop片段A和B,代替或Main_fragment。

Basically, I have an app. The Main activity is a tabbed activity/swipe view. The first page consists of Main_Fragment, which consists of two other fragments (A and B) within them. Fragment A contains a button, that I am trying to have when pressed, will launch a different fragment to either go ontop of fragment A and B, replace them or Main_fragment.

我在哪里卡住是code我需要使用的片段交易。 previously我用这个code,当Main_Fragment,只是一个活动,和它的工作很好,但我现在不能确定什么,我需要做的。

Where I am getting stuck is what code I would need to use for the Fragment Transaction. Previously I used this code, when Main_Fragment, was just an activity, and it worked out well, but now I am unsure on what I would need to do.

    FragmentManager fm = getFragmentManager();
    fragment = new DayViewFragment();

    FragmentTransaction ft = fm.beginTransaction();
    ft.addToBackStack(null);
    ft.add(android.R.id.content, fragment);
    ft.commit();

首先,我想我会需要使用getParentFragment()不知何故,我不确定如何,其次我不能确定做替换什么android.R.id.content,尤其是,当code是内一个片段的片段的内侧。

Firstly I think I would need to use getParentFragment() somehow, and I am unsure on how, and secondly I am unsure what do replace for android.R.id.content, especially, when the code is inside a fragment inside of a fragment.

如果任何澄清是必要的,请询问。感谢您的帮助!

If any clarification is needed, please ask. Thanks for any help!

推荐答案

我想实现这种功能的最佳方法是使用一个接口回调从片段的活动。​​

I think the best way to implement this type of functionality is to use an interface callback from the Fragment to the Activity.

Android的工作室1.2.2甚至会自动创建界面回调code,如果你选中包含接口回调?复选框,创建一个新的片段时。

Android Studio 1.2.2 will even automatically create interface callback code if you check the "Include interface callbacks?" checkbox when creating a new Fragment.

下面是该片段一般code结构:

Here is the general code structure for the Fragment:

public class MainActivityFragment extends Fragment {

    private OnMainFragmentInteractionListener mListener;
    private Button exampleButton;

    public MainActivityFragment() {
    }

    public static MainActivityFragment newInstance() {
        MainActivityFragment fragment = new MainActivityFragment();
        return fragment;
    }

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

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        exampleButton = (Button) rootView.findViewById(R.id.button);

        exampleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.buttonClicked("test value");
            }
        });

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnMainFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnMainFragmentInteractionListener");
        }
    }

    public interface OnMainFragmentInteractionListener {
        void buttonClicked(String val);
    }
}

那么活动将实现接口:

Then the Activity will implement the interface:

public class MainActivity extends FragmentActivity
        implements MainActivityFragment.OnMainFragmentInteractionListener {

和实现接口的方法:

@Override
public void buttonClicked(String val) {

    FragmentManager fm = getFragmentManager();
    fragment = new DayViewFragment();

    FragmentTransaction ft = fm.beginTransaction();
    ft.addToBackStack(null);
    ft.add(android.R.id.content, fragment);
    ft.commit();

}

这篇关于安卓:难以启动的一个片段里的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆