Reputation: 113
I have a Fragment A holds a view pager that display serval nested fragments. B and C are the fragments directly hold by the view pager. And each B and C holds a listView. When click listView item fragment D will show up. The problem is that, I could only hide the Fragment C and B from the viewPager, which means Fragment D will come over Fragment A. The question is how could I hide Fragment A from B and C not just hide the Fragment B and C from the viewPager.
here is the code of Fragment A
public class FragmentA extends Fragment {
private View view;
private PagerSlidingTabStrip tabs;
private DisplayMetrics dm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, null);
dm = getResources().getDisplayMetrics();
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
tabs.setViewPager(pager);
setTabsValue();
initView();
return view;
}
private void initView() {
TextView titleView = (TextView) view.findViewById(R.id.title_content_text);
titleView.setVisibility(View.VISIBLE);
titleView.setText(getString(R.string.label_alert));
}
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private final String[] titles = { "B", "C" };
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return titles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if (BFrag == null) {
BFrag = new BFrag();
}
return BFrag;
case 1:
if (CFrag == null) {
CFrag = new CFrag();
}
return CFrag;
default:
return null;
}
}
}
}
here is the code in Fragment B that trying to show fragment D:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
if (pos < Item.size()) {
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.hide(BFrag.this);
DFrag dFrag = new DFrag();
activity.dFeedFrag = dFrag;
transaction.add(R.id.home_frame_content, dFeedFrag, "dFrag");
transaction.addToBackStack(null);
transaction.commit();
}
}
}
});
The layout file of Fragment A:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/inculde_title_bar"/>
<com.example.blah.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="40dp" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabs" />
Anyone could help me, Thanks! =)
Upvotes: 1
Views: 1851
Reputation: 9082
Because B, C is hosted in the ViewPager, so they are in the fragment A, so you just need to find fragment A and hide it.
The real question is, where is the id home_frame_content
.
If this id is in Fragment A, D will be hidden once A is hidden. I think this is what you want.
So the id home_frame_content
must be somewhere not in A. Then you can find A, hide it, you will get what you want.
update 1
And there is a demo: https://github.com/liaohuqiu/ABCDFragment
Hope it would be helpful.
Upvotes: 1