在android

时间:2015-10-21 06:35:15

标签: android android-fragments android-viewpager android-tablayout

在过去的几天里,我一直在努力寻找解决这个问题的方法: 我有一个活动,其中包含选项卡布局和视图寻呼机。视图寻呼机使用适配器填充片段。使用此viewpager创建选项卡并进行修复。我的问题是如何动态更改选项卡的布局(例如在单击按钮后)?

这是 xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:background="@color/white"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

这是活动

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupToolbar();

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
    setupTabLayout(tabLayout);
}

private void setupToolbar(){
   ...
}


public void setupViewPager(ViewPager viewPager) {
    pageAdapter = new FragmentPageAdapter(getApplicationContext(), getSupportFragmentManager());
    pageAdapter.addFragment(ContentFragment.newInstance(), "Following", R.drawable.following);
    pageAdapter.addFragment(ContentFragment.newInstance(), "Discover", R.drawable.flower);
    pageAdapter.addFragment(ContentFragment.newInstance(), "", R.drawable.camera);
    pageAdapter.addFragment(ProfileFragment.newInstance(), "Profile", R.drawable.profile);
    pageAdapter.addFragment(ContentFragment.newInstance(), "Task List", R.drawable.list);
    viewPager.setAdapter(pageAdapter);
}

public void setupTabLayout(TabLayout tabLayout) {
    try {
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(pageAdapter.getTabView(i));
        }
        tabLayout.requestFocus();
    }
    catch(Exception ex){
        Log.e("errr2", ex.getMessage());
    }
}

这是适配器

public class FragmentPageAdapter extends FragmentStatePagerAdapter {
    private Context mContext;
private List<Fragment> mFragments = new ArrayList<>();
private List<String> mFragmentTitles = new ArrayList<>();
private List<Integer> mFragmentIcons = new ArrayList<>();

public FragmentPageAdapter(Context context, FragmentManager fm) {
    super(fm);
    this.mContext = context;
}

public void addFragment(Fragment fragment, String title, int drawable) {
    mFragments.add(fragment);
    mFragmentTitles.add(title);
    mFragmentIcons.add(drawable);
}



@Override
public Fragment getItem(int position) {

    return mFragments.get(position);
}


@Override
public int getItemPosition(Object object)
{
    return POSITION_UNCHANGED;
}

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

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

public View getTabView(int position) {
    View tab = LayoutInflater.from(mContext).inflate(R.layout.tabbar_view, null);
    TextView tabText = (TextView) tab.findViewById(R.id.tabText);
    ImageView tabImage = (ImageView) tab.findViewById(R.id.tabImage);
    tabText.setText(mFragmentTitles.get(position));
    tabImage.setBackgroundResource(mFragmentIcons.get(position));
    if (position == 3) {
        tab.setSelected(true);
    }
    return tab;
}

}

替换片段的正常方法是调用

  fragmentManager.beginTransaction().replace(placeholderId, editPrifileFragment).commit();

但在这种情况下我应该为placeholderId使用什么?因为在活动布局中我没有片段的占位符,只有一个viewpager。 那么如何动态替换片段并保持后退按钮功能呢? 我经常搜索并找到一些hacky解决方案,但我认为这是一种非常常见的情况,应该有更好的解决方案。 谢谢。

1 个答案:

答案 0 :(得分:0)

您的ViewPager Fragment应该保留placeholderId。 因此,在FragmentViewPager的一部分)中,您可以像往常一样致电getChildFragmentManager()添加Fragment

childFragmentManager.beginTransaction().add(placeholderId, editPrifileFragment).addToBackStack(null).commit();