如何在android studio中用另一个片段替换片段

时间:2016-11-16 08:18:13

标签: android

我正在尝试用homescreen片段替换一个displaynews片段,但片段正在添加而不是替换在这里的代码

content_main.xml

manyReqsWrapper(args).then((res) => {
      //success handler
    }).catch((err) => {
      //error handler
    });

mainacitivity.class

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ajitesh.newson.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/home_fragment"
        android:name="com.ajitesh.newson.DisplayNews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

displaynews.class

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

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.home_fragment,fragment).commit();

displaynews.xml

 public class DisplayNews extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.displaynews, container, false);
    }
}

HomeScreen.class

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:id="@+id/display_news"
    android:layout_height="wrap_content">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="helllooooo"/>
</LinearLayout>

home_screen.xml

public class HomeScreen extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.home_screen, container, false);
    }
}

1 个答案:

答案 0 :(得分:1)

如果在xml中进行硬编码,则无法替换碎片,您应该动态添加碎片以将碎片替换为另一个碎片。

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

source // example - 仍然遇到问题?您可以找到解决方案here