16

我有一个带有按钮的活动,当单击时我用片段上的另一个按钮调用片段。但是当点击片段的按钮时,我不能调用第二个片段。这是我的来源,非常简单:activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >

<Button 
    android:id="@+id/btn_click"
    android:text="Call Fragment"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:onClick="onClick"
    />
</LinearLayout>

片段1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<TextView 
    android:id="@+id/fragment1"
    android:text="Fragment 1"
    android:textSize="25sp"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

<Button 
    android:id="@+id/btn_frag2"
    android:text="Call Fragment 2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

片段2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<TextView 
    android:id="@+id/fragment2"
    android:text="Fragment 2"
    android:textSize="25sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View v) {
    Fragment1 fragment1 = new Fragment1();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, fragment1);
    fragmentTransaction.commit();
}

}

片段1.java

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    public void onClick2(View view) {
        Fragment2 fragment2 = new Fragment2();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1, fragment2);
        fragmentTransaction.commit();
    }
}

片段2.java

public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, container, false);
    }
}

我的代码有什么问题?

4

7 回答 7

32

我认为现在可以使用片段嵌套,只需更新后面的可计算性 jar

现在让我们自己挖掘问题。

public void onClick2(View view) {
    Fragment2 fragment2 = new Fragment2();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment1, fragment2);
    fragmentTransaction.commit();
}

我认为R.id.fragment1属于 aTextView这不是一个包含子视图的好地方,因为它不是 a ViewGroup,您可以textView从 xml 中删除并用一个 let say 替换它,LinearLayout它会起作用,如果不告诉我错误是什么。

片段1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<LinearLayout 
    android:id="@+id/fragment1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

<Button 
    android:id="@+id/btn_frag2"
    android:text="Call Fragment 2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

更新评论中的错误

public class Fragment1 extends Fragment implements OnClickListener{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment1, container, false);
((Button) v.findViewById(R.id.btn_frag2)).setOnClickListener(this);
    return v;
}

public void onClick(View view) {
    Fragment2 fragment2 = new Fragment2();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction =        fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, fragment2);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

}
于 2013-01-31T14:23:52.440 回答
5

如果你想用 替换整个Fragment1Fragment2你需要在里面做MainActivity,通过使用:

Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment2);
fragmentTransaction.commit();

只需将此代码放在 中的方法中MainActivity,然后从 中调用该方法Fragment1

于 2013-01-31T14:20:16.663 回答
2

这是我的答案,它解决了同样的问题。Fragment2.java 是包含fragment2.xml 布局的片段类。

public void onClick(View v) {
                    Fragment  fragment2 = new Fragement2();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.frame_container, fragment2);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();   
                }
于 2015-11-20T09:53:22.153 回答
0

我会使用一个听众。Fragment1 调用监听器(主要活动)

于 2013-01-31T14:25:19.363 回答
0

此代码适用于我从 child_fragment 调用 parent_fragment 方法。

ParentFragment parent = (ParentFragment) activity.getFragmentManager().findViewById(R.id.contaniner);

parent.callMethod();

于 2017-06-02T12:05:28.010 回答
0

在 MainActivity

private static android.support.v4.app.FragmentManager fragmentManager;

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

    fragmentManager = getSupportFragmentManager();                                                                 


    }

public void secondFragment() {

    fragmentManager
            .beginTransaction()
            .setCustomAnimations(R.anim.right_enter, R.anim.left_out)
            .replace(R.id.frameContainer, new secondFragment(), "secondFragmentTag").addToBackStack(null)
            .commit();
}

在 FirstFragment 调用 SecondFrgment 像这样:

new MainActivity().secondFragment();
于 2018-03-04T05:22:15.363 回答
-2

只需这样做: getTabAt(标签的索引)

        ActionBar actionBar = getSupportActionBar();
        actionBar.selectTab(actionBar.getTabAt(0));
于 2016-02-08T11:30:36.717 回答