如何在android studio中用另一个片段替换一个片段
Posted
技术标签:
【中文标题】如何在android studio中用另一个片段替换一个片段【英文标题】:how to replace a fragment with another fragment in android studio 【发布时间】:2017-03-30 08:26:59 【问题描述】:我正在尝试用主屏幕片段替换 displaynews 片段,但正在添加片段而不是在此处的代码中替换
content_main.xml
<?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_
android:layout_
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_
android:layout_
/>
</LinearLayout>
mainacitivity.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.class
public class DisplayNews extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.displaynews, container, false);
displaynews.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:id="@+id/display_news"
android:layout_>
<TextView
android:layout_
android:layout_
android:text="helllooooo"/>
</LinearLayout>
HomeScreen.class
public class HomeScreen extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.home_screen, container, false);
home_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:id="@+id/text1"
android:text="this is home page"/>
<Button
android:layout_
android:layout_
android:text="click"/>
</LinearLayout>
【问题讨论】:
【参考方案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
【讨论】:
不,我无法得到解决方案 @AjiteshSakaray 因此我不明白你的问题 查看我的代码,我必须将片段命名为 displaynews 和 homescreen。我在这里有 content_main.xml 作为主要 xml 文件我想添加 displaynews.xml 文件,当我单击按钮时 displaynews 片段应该是替换为主屏幕片段我该怎么做以上是关于如何在android studio中用另一个片段替换一个片段的主要内容,如果未能解决你的问题,请参考以下文章