SlidingTabLayout的Android片段事务无法在API级别工作< 21

时间:2015-03-28 17:06:34

标签: android android-fragments android-appcompat android-tabs

在我的应用程序中,我有一个ActionBarActivity(我使用支持库和AppCompat),它使用来自Google的SlidingTabLayout类(取自here)。所以这是活动布局的XML代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_series_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SeriesDetailsActivity">

    <com.my.package.SlidingTabLayout
        android:id="@+id/series_details_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/primary_material_dark" />
    <android.support.v4.view.ViewPager
        android:id="@+id/series_details_pager"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1" />
</LinearLayout>

在此活动中,当用户按下操作栏中的选项时,我想添加带有自定义动画的片段。这是处理菜单点击的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        /* ... OTHER CASES ... */
        case R.id.menu_voption:
            newFragment = MyNewFragment.newInstance();                
            getSupportFragmentManager().beginTransaction()
                    .setCustomAnimations(
                            R.anim.slide_up,
                            R.anim.slide_down
                    )
                    .add(R.id.activity_series_details, newFragment)
                    .commit();
            editing = true;
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;

这样做,我的新片段被正确添加到activty并替换currente片段但不替换制表符布局,仍然可见。所以我在开始交易之前尝试添加这一行:

tabsHost.setVisibility(View.GONE);

其中tabsHost是SlidingTabLayout。通过此修改,选项卡布局消失,新片段正确显示,但仅在API级别&gt; = 21.在我的Samsung Galaxy S4(运行API 19)和所有其他API级别低于21的仿真器中(我的目标是11+),标签布局消失但新片段不可见。我很确定是我的错,但我无法理解为什么。谢谢大家的关注。

1 个答案:

答案 0 :(得分:0)

由于SlidingTabLayout不是片段,因此无法由FragmentManager进行管理。您必须将其作为片段的一部分并将其添加到您的Activity中。这可以通过getChildFragmentManager进行。

将您的活动布局移动到片段:

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_series_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

fragment_sliding_tab.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_series_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.my.package.SlidingTabLayout
        android:id="@+id/series_details_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/primary_material_dark" />
    <android.support.v4.view.ViewPager
        android:id="@+id/series_details_pager"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1" />
</LinearLayout>

SlidingTabFragment.java

public class SlidingTabFragment extends Fragment {
    private PagerAdapter mPagerAdapter;
    private ViewPager mViewPager;

    public static SlidingTabFragment newInstance() {
        SlidingTabFragment fragment = new SlidingTabFragment();
        return fragment;
    }

    public SlidingTabFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mPagerAdapter = new PagerAdapter(getChildFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) view.findViewById(R.id.series_details_pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        SlidingTabLayout tabs = (SlidingTabLayout) view.findViewById(R.id.series_details_tabs);
        tabs.setViewPager(mViewPager);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sliding_tab, container, false);
    }
}

在您的活动onCreate

中添加您的片段
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Fragment slidingTabFragment = SlidingTabFragment.newInstance();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.activity_series_details, slidingTabFragment).commit();
}