I have the following xml file:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我有一个视图页面中包含三个片段。在其中一个片段中,我尝试使用以下代码启动一个新的片段:
FragmentTransaction trans = getFragmentManager()
.beginTransaction();
trans.replace(R.id.pager, new SubscriptionFragment());
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.addToBackStack(null);
trans.commit();
我能成功地替换ViewPager中现有的片段,但这并不符合我的目的。我想做的是不要替换已经存在于ViewPager中的现有片段,而是将ViewPager本身从视图中删除,并启动一个新的Fragment,使顶部标签不再可见且ViewPager消失在视图中。当我使用意图启动新活动时,这可以实现,但对于片段则无法实现。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new MainFragment()).commit();
}
}
MainFragment:
public class MainFragment extends Fragment {
FragmentPagerAdapter adapterViewPager;
ViewPager viewPager;
String title[] = {"Services", "You", "History"};
// TODO: Rename and change types and number of parameters
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
View view = inflater.inflate(R.layout.fragment_view_pager, container, false);
/*************************************/
//CALLING HISTORY API
// loadSubscriptions();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPagerAdapter mAdapter = new ViewPagerAdapter(getFragmentManager());
ViewPager mPager = (ViewPager) getView().findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int num) {
if (num == 0) {
return new ServicesFragment();
} else {
return new HistoryFragment();
}
}
@Override
public int getCount() {
return 2;
}
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>
fragment_view_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在ServiceFragment中:我正在使用以下方式打开新的Fragment:
physiotherapyView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction trans = getFragmentManager()
.beginTransaction();
/*
* IMPORTANT: We use the "root frame" defined in
* "root_fragment.xml" as the reference to replace fragment
*/
trans.replace(R.id.pager, new SubscriptionFragment());
/*
* IMPORTANT: The following lines allow us to add the fragment
* to the stack and return to it later, by pressing back
*/
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
trans.addToBackStack(null);
trans.commit();
}
});