如何动态添加片段到我的活动中? [英] How to add a fragment in my activity dynamically?

查看:49
本文介绍了如何动态添加片段到我的活动中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio,它实现了片段类的所有功能.我收到以下运行时异常:

I am using Android Studio which implements all the functions of a fragment class. I am getting following runtime exception:

must implement OnFragmentInteractionListener

这是我的代码,我已经在主要活动中实现了OnFragmentInteractionListener.

Here is my code and I have implemented the OnFragmentInteractionListener in my main activity.

主要活动:

public class MainActivity extends FragmentActivity implements BlankFragment.OnFragmentInteractionListener {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (findViewById(R.id.fragment_container) != null) {
            BlankFragment fragment = new BlankFragment();
            android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
            ft.add(R.id.fragment_container, fragment).commit();
        }
    @Override            
    public void onFragmentInteraction(Uri uri) {}

}

空白片段:

public class BlankFragment extends android.support.v4.app.Fragment {
    private OnFragmentInteractionListener mListener;
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }

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

默认情况下,也实现片段类和主要活动的其余方法.我也将main_activity.xml file and also assigned android:id`中的LinearLayoutLinearLayout更改为它,这被很好地引用了.我的代码有什么问题?

The remaining methods of fragment class and main activity are also implemented by default. I have also changed LinearLayout with FrameLayout in the main_activity.xmlfile and also assignedandroid:id` to it which is referenced fine. What is wrong with my code?

推荐答案

要与您的BlankFragment对象进行交互,我将使用

To interact with your BlankFragment object, I would use the following method recommended in the Android Support Docs - I believe this is what you are trying to achieve, and it will be suitable as your BlankFragment object is hosted by MainActivity:

public class MainActivity extends FragmentActivity implements BlankFragment.OnFragmentInteractionListener    {

private BlankFragment fragment = null;
private android.support.v4.app.FragmentManager manager = null;
private android.support.v4.app.FragmentTransaction ft;

public void onFragmentInteraction(Uri uri){
        Log.i("Tag", "onFragmentInteraction called");
    }

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (manager == null) manager = getSupportFragmentManager();            

    if(manager.findFragmentById(R.id.fragment_container) == null) {

        //If a fragment is not already loaded into your container, then add one...

        fragment = new BlankFragment();
        ft= manager.beginTransaction();
        ft.add(R.id.fragment_container,fragment).commit();
    }
}

为了与您的片段进行通信,您需要执行以下操作:

In order to communicate with your fragment, you would do the following:

if(fragment == null) {
    fragment = (BlankFragment) manager.findFragmentById(R.id.fragment_container);
}

然后您可以调用与fragment

如果是相反的方式(从片段到活动的通信),则需要在BlankFragment中执行以下操作以与父Activity形成链接:

If it's the other way round (communication from fragment to activity), then you would need to do the following in BlankFragment to form a link with the parent Activity:

//Class variable...
OnFragmentInteractionListener mCallback;

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

您可能已经忘记了这最后一步,它可以解释您的错误.然后,您将使用:

You may have forgotten about this last step, which could explain your error. You would then use:

mCallback.onFragmentInteraction(uri);

MainActivity

这篇关于如何动态添加片段到我的活动中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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