我要做的是只使用一个Activity作为许多片段的宿主,例如,活动一次只能托管一个片段,片段应按特定顺序附加到活动。
我的片段代码:
public class FirstFragment extends Fragment{
//some code here to instantiate FirstFragment here.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) getActivity()).onFirstFragmentFinished(position);
}
});
return view;
}
// Container Activity must implement this interface
public interface FirstFragmentListener {
public void onFirstFragmentFinished(int position);
}
}
简单地说,在每个片段中,按下它时会有一个按钮,它将获得主机活动并调用FirstFragmentListener
接口的实现方法。
public class MainActivity extends AppCompatActivity implements FirstFragmentListener {
@Override
public void onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTra = fragmentManager.beginTransaction();
fragmentTra .replace(R.id.main_layout, new FirstFragment());
fragmentTra.commit();
}
@Override
public void onFirstFragmentFinished(int position)
{
if(position == 1)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTra = fragmentManager.beginTransaction();
fragmentTra .replace(R.id.main_layout, new SecondFragment());
fragmentTra.commit();
}
}
}
创建活动时,请始终使用FirstFragment布局替换此活动内的任何内容
当FirstFragment完成其工作并按下该片段上的按钮时,它将在此活动上调用FirstFragment接口的实现方法,然后检查位置是否有值,如果条件正确则转到并将SecondFragment替换为SecondFragment实例。
调用commit()
方法时,应用程序崩溃。
答案 0 :(得分:0)
在Fragment中,你需要View来绑定像这样的片段:
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position == 1){
SecondFragment secondFrag= new SecondFragment();
getActivity().getFragmentManager().beginTransaction()
.replace(R.id.main_layout, secondFrag)
.addToBackStack(null)
.commit();
}
}
});
return view;
}
}
答案 1 :(得分:0)
使用onFirstFragmentFinished是错误的。看看官方文件: android-comunication-activity-ffragment
更喜欢覆盖onAttach()方法。
答案 2 :(得分:0)
我看看我的堆栈跟踪,我发现了这个异常:
com.example.exchange.msbexchange E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.exchange.msbexchange, PID: 17975
java.lang.RuntimeException: com.example.exchange.msbexchange.MainActivity@1e7d93b must implement OnFragmentInteractionListener
at com.example.exchange.msbexchange.CurrencyFragment.onAttach(CurrencyFragment.java:83)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1030)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
很明显,因为我的MainActivity没有实现SecondFragment类的接口。我不知道碎片是否必须使用。我只需要实现界面,现在它正在运行。
答案 3 :(得分:0)
您需要在片段
中的onAttach中实现您的界面FirstFragmentListener
FirstFragmentListener mCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (FirstFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
然后像点击或任何你想要的那样使用它