Android片段FragmentTransaction隐藏/显示标题

时间:2017-04-07 06:14:47

标签: android fragment hide show fragmenttransaction

我的问题如下,我有一个ViewPagerAdapter的片段及其标题。当我使用FragmentTransaction.hide时,片段变为空(​​空白),但标题保持不变。 如何将标题与片段一起隐藏,以便我只能滑动显示的片段。

继承我的代码:

private void setupViewPager(CustomViewPager viewPager) {

    mAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    fragLog = new FrgLog();   
    fragInfo = new FrgInfo(); 
    fragText = new FrgText(); 
    fragFirm = new FrgFirm(); 
    fragHist = new FrgHist(); 

    mAdapter.addFragment( fragLog, getString(R.string.frag_title_log));
    mAdapter.addFragment( fragInfo, getString(R.string.frag_title_info));
    mAdapter.addFragment( fragText, getString(R.string.frag_title_text));
    mAdapter.addFragment( fragFirm, getString(R.string.frag_title_firm));
    mAdapter.addFragment( fragHist, getString(R.string.frag_title_hist));

    viewPager.setAdapter(mAdapter);
    viewPager.setCurrentItem(1);
    viewPager.setPagingEnabled(true);
}


class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> myFragmentList = new ArrayList<>();
    private final List<String> myFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return myFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return myFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        boolean found = false;
        for (int i = 0; i < myFragmentList.size(); i++) {
            if (myFragmentList.get(i).getClass().getName().equalsIgnoreCase(fragment.getClass().getName())) {
                found = true;
            }
        }
        if (!found) {
            myFragmentList.add(fragment);
            myFragmentTitleList.add(title);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return myFragmentTitleList.get(position);
    }

}


protected void displayFirst() {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.hide(fragText);
    ft.show(fragFirm);
    ft.show(fragHist);
    ft.commit();

}

protected void displaySecond() {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.hide(fragFirm);
    ft.hide(fragHist);
    ft.show(fragText);
    ft.commit();

}


<xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eu.angermayr.test.MainActivity">

<eu.angermayr.test.CustomViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="10dp"
        android:paddingTop="0dp" />

</eu.angermayr.test.CustomViewPager>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以使用OnPageChangeListener添加标题:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        // use this position to set title accordingly.  
        setTitle("You fragment specific title");
    }
});