简体   繁体   English

ViewPager不显示任何内容

[英]ViewPager does not show anything

There's something wrong with my TabLayout and ViewPager. 我的TabLayout和ViewPager出了点问题。 It does not show anything. 它什么也没显示。 Here's my adapter: 这是我的适配器:

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;
}

Here's my TabFragment class: 这是我的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;
  }

}

And here's the layout for the above class: 这是上述类的布局:

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>

I don't see any wrong here. 我在这里没有发现任何错误。 I just followed the tutorial. 我只是按照教程学习。 Let me know what's happening here. 让我知道这里发生了什么。 Thank you 谢谢

Thanks @MikeM. 谢谢@MikeM。 it all worked now. 现在一切正常。 I removed the LinearLayout surrounding the TabLayout and ViewPager and changed the FrameLayout to RelativeLayout and they all showed up. 我删除了TabLayoutViewPager周围的LinearLayout ,并将FrameLayout更改为RelativeLayout ,它们都出现了。 Thank you! 谢谢!

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM