android - ViewPager 不显示任何内容

标签 android android-viewpager

我的 TabLayout 和 ViewPager 有问题。它没有显示任何内容。这是我的适配器:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int mNumOfTabs) {
    super(fm);
    this.mNumOfTabs = mNumOfTabs;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return ChoiceFragment.newInstance("Mobile Games",
                    R.drawable.ic_stay_current_portrait_white_48dp, "1");
        case 1:
            return ChoiceFragment.newInstance("Computer Games",
                    R.drawable.ic_payment_white_48dp, "5");
        default: return ChoiceFragment.newInstance("Mobile Games",
                R.drawable.ic_stay_current_portrait_white_48dp, "1");
    }
}

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

@Override
public CharSequence getPageTitle(int position) {
    String title;

    if (position == 0)
        title = "Mobile";
    else
        title = "Computer";

    return title;
}

这是我的 TabFragment 类:

public class TabGroupFragment extends Fragment {



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


public static TabGroupFragment newInstance() {
    TabGroupFragment fragment = new TabGroupFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {

    }
}

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

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Mobile"));
    tabLayout.addTab(tabLayout.newTab().setText("Computer"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager);
    final PagerAdapter adapter = new PagerAdapter(
            getActivity().getSupportFragmentManager(),
            tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(
            new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    return view;
  }

}

这是上面类的布局:

FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TabGroupFragment">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/utility_white"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:id="@+id/tab_layout" />

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view_pager"/>

    </LinearLayout>

</ScrollView>

我看不出有什么不妥。我只是按照教程。让我知道这里发生了什么。谢谢

最佳答案

感谢@MikeM。现在一切正常了。我删除了 TabLayoutViewPager 周围的 LinearLayout 并将 FrameLayout 更改为 RelativeLayout他们都出现了。谢谢!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TabGroupFragment">

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/utility_white"
    android:elevation="6dp"
    android:id="@+id/tab_layout" />

<android.support.v4.view.ViewPager
    android:layout_below="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/view_pager"/>

</RelativeLayout>

关于android - ViewPager 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33992070/

相关文章:

android - Xamarin 表格 : Push notification is not working on Android 7. 1.2

java - 如何解决viewpager中相同内容复制的问题

android - CoordinatorLayout 与 ViewPager 的奇怪行为

java - Android Studio 中的 webview 出现错误 ""无法加载网页,因为 net::ERR_ACCESS_DENIED""

java - Android将EditText转换为String

android - 如何在 ViewPager 的开头添加一个页面?

android - 以编程方式创建/添加 View 寻呼机

android - fragment View 膨胀错误 : Resource is not a drawable

android - 为安卓应用提供私钥和证书

Android:操作栏中的动画不适用于异步任务