Android支持库V4的FragmentManager还是Android自带的FragmentManager?

8
我正在学习Android开发。我遇到了一个应该很简单的问题。
我正在创建一个只有一个Activity、两个Fragment和一个接口的应用程序。
android:minSdkVersion="11"
android:targetSdkVersion="19

在主活动中,我试图使用管理器创建对Fragment B的引用。我在这里卡住了,因为Eclipse告诉我要更改一些东西(见下文):

我的意图:

@Override
    public void respond(int i) {
        // TODO Auto-generated method stub

    FragmentManager manager =getFragmentManager();
    FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);

}`

如果我按照这种方式操作,会出现错误信息,需要进行一些更改。更改后,代码如下所示(但我仍然无法访问FragmentB):

    @Override
public void respond(int i) {
    // TODO Auto-generated method stub

    android.app.FragmentManager manager =getFragmentManager();
    android.app.Fragment f2=  manager.findFragmentById(R.id.fragment2);

}

为了更详细的说明,我在这里也会放置Activity的import头部:

  package com.example.modular_ui;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity implements Communicator{....

我在这里缺少什么?整个support.v4 / support.v7的东西对新手来说有点混乱。

编辑: 更改为:

    import android.app.Fragment;
import android.app.FragmentManager;

即使我扩展了FragmentActivity,仍然无法创建对FragmentB的引用:

@Override
public void respond(int i) {
    // TODO Auto-generated method stub

FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);

根据您的要求,我已经发布了FragmentB代码:

package com.example.modular_ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentB extends Fragment {

TextView text;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_b, container);
}

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        text = (TextView) getActivity().findViewById(R.id.textView1);
    }

主要XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.modular_ui.MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.modular_ui.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.modular_ui.FragmentB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fragment1"
        android:layout_marginTop="54dp" />

</RelativeLayout>

支持库主要是为了支持ActionBar和一些其他关键功能,这些功能在API 11之前不受支持。如果您的最小API为11,则最好使用常规库以避免添加不必要的体积。除非有我不知道的情况。 - zgc7009
感谢您的快速回复。Eclipse会自动实现支持库。所以您建议将其更改为:import android.app.Fragment; import android.app.FragmentManager; - Foo
1
你可以使用任何一种,但如果您在API 11或更高版本时,我认为没有必要,因为那个点上的原生库已经支持了所有我所知道的功能。如果是这种情况,它只会成为您应用程序中的负担,并且可能会导致头疼,因为您需要记住正在使用哪个库。 - zgc7009
4个回答

10

在成功添加支持库后,只需使用getSupportFragmentManager();。


7

OP离拥有一个适用于API 11及更高版本的解决方案非常接近,而不需要支持v4

他只需要改变他的片段(Fragment),使其在导入(import)语句中也不使用support.v4。

两种方法的总结。 所有你的活动和片段(Fragments)应该有像其中一种代码,不要混合它们! (并非所有行都需要在所有文件中使用;根据需要使用行。)

支持v4方法(support-v4 approach)

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;      <-- ".support.v4"
import android.support.v4.app.FragmentManager;

... MainActivity extends FragmentActivity ...

... = getSupportFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

API 11+方法

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

... MainActivity extends Activity ...

... = getFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

如果你的项目使用了上述的某一种方法,而你正在集成来自其他地方的代码,请确保查找这些行并将它们更改为与你所拥有的相匹配的内容。


4

首先:你的活动应该扩展FragmentActivity。

关于支持库。它们被引入以添加一些功能到较旧版本的Android系统中。例如,在Android 3.0(SDK编号:11)中引入了Fragments。实际上(根据文档),在Android 3.0以下的版本中,支持库使用系统实现的Fragments。


你能发布一下创建和附加FragmentB的代码吗?同时,主活动的XML也会很有帮助。 - RobertM
我已经在下面添加了这些内容。 - Foo
1
你的片段扩展了支持库的片段。将import android.support.v4.app.Fragment;更改为import android.app.Fragment;在FragmentB中。 - RobertM
过时的答案。因为 OP 的目标是 API 11 及更高版本,不需要支持 v4 的内容,因此不需要 FragmentActivity(它是支持 v4 的一部分)。正如 RobertM 所说,真正的问题是他的 Fragment 做了 import android.support.v4.app.Fragment; 这个操作,从而使他承担了这种旧方法的风险。删除所有对 support.v4 的引用。只需说 import android.app.Fragment;,使用来自 developer.android 或其他地方的现代 Fragment 示例,其中没有任何关于 support.v4 的内容。然后你就可以简单地说 getFragmentManager() - ToolmakerSteve

2

很简单。

如果你希望你的应用程序在旧设备(API级别低于11)上运行,请使用getSupportFragmentManager()

如果你希望你的应用程序在API级别高于11的设备上运行,请使用getFragmentManager()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接