如何在我的活动中动态添加片段?

时间:2015-07-15 08:49:27

标签: java android android-layout android-fragments

我正在使用Android Studio来实现片段类的所有功能。我遇到了运行时异常:

must implement OnFragmentInteractionListener

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

主要活动:

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) {}

}

BLANK FRAGMENT:

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 LinearLayout android:id`中的FrameLayout更改为file and also assigned并将其更改为引用。我的代码出了什么问题?

2 个答案:

答案 0 :(得分:3)

要与BlankFragment对象进行互动,我会使用Android Support Docs中推荐的以下方法 - 我相信这是您要实现的目标,并且它将适合您的{{1}对象由BlankFragment托管:

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();
    }
}

然后,您可以调用与if(fragment == null) { fragment = (BlankFragment) manager.findFragmentById(R.id.fragment_container); }

相关联的任何方法

如果它反过来(从片段到活动的通信),那么您需要在fragment中执行以下操作以与父BlankFragment形成链接:

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");
        }
    }

mCallback.onFragmentInteraction(uri);

进行沟通

答案 1 :(得分:1)

这对我有用,我刚刚在这里分享了你永远不会知道它会帮助其他人。

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();

只需一行代码。