无法替换/导航片段
我试图从一个片段导航到另一个片段。 这两个片段是100%不同的,没有ViewPager
或类似的东西来连接它们。 这是代码。
fragment_view1.xml
custom_view.xml
FirstView.java (使用fragment_view1.xml)
package com.example.fragmenttest; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class FirstView extends DropDownMenu { private TextView firstText; private Button btn; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_view1,container,false); firstText = (TextView)view.findViewById(R.id.viewOneText); btn = (Button)view.findViewById(R.id.viewOneBtn); btn.setOnClickListener(new ButtonEvent()); return view; } private class ButtonEvent implements OnClickListener { @Override public void onClick(View v) { // Create new fragment and transaction Fragment newFragment = new CustomView(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_view, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } } }
CustomView.java(使用custom_view.xml)
package com.example.fragmenttest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class CustomView extends Fragment { private TextView secondText; private Button secondViewBtn; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.custom_view,container,false); return view; } }
在FirstView.java
,当我单击按钮时,我需要移动到Fragment CustomView
。 但相反,它只是取代了CustomView
顶部的FirstView
。 下面的图片将更好地解释它。
FirstView
CustomView
当按钮被FirstView
点击时
为什么会这样?
您正在尝试将firstView
替换为新片段,但firstView
不是片段,它是一个RelativeLayout
。
您无法替换布局文件中静态定义的片段。 您只能替换通过FragmentTransaction
动态添加的FragmentTransaction
。
您的布局都没有包含布局中的片段。
新的Fragment将替换先前添加到容器中的现有Fragment。
看看这个 。
如果你需要用另一个片段替换一个片段,那么下面提到:
TalkDetail fragment = new TalkDetail(); // TalkDetail is name of fragment which you need to put while replacing older one. Bundle bundle = new Bundle(); bundle.putString("title", m_ArrayList.get(arg2).title); bundle.putString("largeimg", m_ArrayList.get(arg2).largeimg); bundle.putString("excert", m_ArrayList.get(arg2).excert); bundle.putString("description", m_ArrayList.get(arg2).description); bundle.putString("cat", m_ArrayList.get(arg2).cat); bundle.putString("header_title", "Talk"); //bundle.putInt("postid", m_ArrayList.get(arg2).postid); fragment.setArguments(bundle); ((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
这是你的BaseContainerFragment.java类,它将为你管理很多东西。
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.util.Log; import app.drugs.talksooner.R; public class BaseContainerFragment extends Fragment { public void replaceFragment(Fragment fragment, boolean addToBackStack) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); if (addToBackStack) { transaction.addToBackStack(null); } transaction.replace(R.id.container_framelayout, fragment); transaction.commit(); getChildFragmentManager().executePendingTransactions(); } public boolean popFragment() { Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount()); boolean isPop = false; if (getChildFragmentManager().getBackStackEntryCount() > 0) { isPop = true; getChildFragmentManager().popBackStack(); } return isPop; } }
有关更多具体细节,请在此处查看我的完整post。
动态更改片段选项卡主机内的片段?