1
我有一個片段A包含一個顯示僕人嵌套片段的視圖分頁器。 B和C是視圖尋呼機直接保存的片段。並且每個B和C都擁有一個listView。當點擊listView項目片段D時會出現。問題是,我只能從viewPager隱藏片段C和B,這意味着片段D會超過片段A.問題是我怎麼能隱藏片段A從B和C不只是隱藏片段B和C從viewPager。在viewPager中從片段中隱藏父代片段
這裏是片段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;
}
}
}
}
這裏是試圖展示片段d在片段B的代碼:
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();
}
}
}
});
片段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" />
任何人都可以幫助我,謝謝! =)
不錯。現在每個人都能正常工作。感謝您的演示!對我的問題有很大的幫助。 – Maggie
也許你想嘗試改變:transaction.add(R.id.home_frame_content,dFeedFrag,「dFrag」); - > transaction.replace(R.id.home_frame_content,dFeedFrag,「dFrag」); –
@RichardLiang,這很有道理。我已經更新了演示。謝謝。 – srain