选项卡视图中的Android UI元素未加载

时间:2015-10-19 15:27:54

标签: android android-fragments android-viewpager android-tabs android-pageradapter

我正在使用FragmentStatePagerAdapter来管理三个标签Fragments,每个标签都有ListViewbutton。当父Fragment通过ViewPagerPagerAdapter加载时,Button会丢失,但ListView仍会显示。切换到Fragment中的另一个标签会导致按钮按预期显示。

为什么第一个视图上没有显示按钮?

public class DetailsPagerAdapter extends FragmentStatePagerAdapter {
    private Creature creature;

    public DetailsPagerAdapter(FragmentManager fm, Creature creature) {
        super(fm);
        this.creature = creature;
    }


    @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                return StatFragment.newInstance(creature);
            case 1:
                return ModFragment.newInstance(creature);
            case 2:
                return ResourceFragment.newInstance(creature);
        }

        return new Fragment();
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch(position) {
            case 0:
                return "Stats";
            case 1:
                return "Mods";
            case 2:
                return "Resources";
        }

        return "";
    }
}

详情片段 - 包含标签

public class CreatureDetailFragment extends Fragment {

    private Creature creature;
    private DetailsPagerAdapter detailsPagerAdapter;
    private ViewPager viewPager;
    private TabLayout tabs;

    public CreatureDetailFragment() {
    }

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

        if (getArguments().containsKey(Creature.ID_INTENT_MESSAGE)) {
            this.creature = Creature.get(this.getContext(), getArguments().getLong(Creature.ID_INTENT_MESSAGE, 0l));

            Activity activity = this.getActivity();
            CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);

            if (collapsingToolbarLayout != null) {
                collapsingToolbarLayout.setTitle(this.creature.name);
            }

            this.detailsPagerAdapter = new DetailsPagerAdapter(this.getActivity().getSupportFragmentManager(), creature);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_creature_details_tabbed, container, false);

        this.viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        this.viewPager.setAdapter(this.detailsPagerAdapter);

        this.tabs = (TabLayout) view.findViewById(R.id.tabLayout);
        this.tabs.setupWithViewPager(this.viewPager);

        return view;
    }
}

包含标签的父布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/main_content"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:context=".DetailActivity">

    <android.support.design.widget.AppBarLayout android:id="@+id/appbar"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/detail_toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"
            />

        <android.support.design.widget.TabLayout android:id="@+id/tabLayout"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:fillViewport="false" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager android:id="@+id/viewPager"
        android:layout_width="match_parent" android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

标签布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabListView"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/btnAddItem" />

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnAddItem"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:textColor="#d5d5d5"
        android:background="#000"
        android:textSize="24dp"
        android:alpha=".7"
        android:src="@drawable/ic_action_add" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

似乎CoordinatorLayout不是用于我的标签片段的最佳布局。 ListView在第一次加载时将按钮推出可视空间,并且由于某种原因,仅在重绘视图时呈现。

我将其切换为RelativeLayout并向ViewPager添加了一些属性:

<android.support.v4.view.ViewPager android:id="@+id/viewPager"
        android:layout_width="match_parent" android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/appbar" />