TabLayout 中的选项卡不显示
Tabs in TabLayout are not displaying
我需要在 Container Activity 中的选项卡布局中显示选项卡,但不知何故选项卡未显示。如果有帮助,这个项目还有三个活动,分别是登录 Activity、地图 Activity、注册 Activity。
注意:地图片段不同于地图 Activity。
Container.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MapFragment(), "Map");
adapter.addFragment(new Chat(),"Chat");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragment = new ArrayList<>();
private List<String> mFragmentTitle = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) { return null; }
@Override
public int getCount() {
return 0;
}
public void addFragment(Fragment fragment, String fragmentTitle){
mFragment.add(fragment);
mFragmentTitle.add(fragmentTitle);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitle.get(position);
}
}
activity_container.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:background="@android:color/darker_gray"
app:layout_scrollFlags="scroll|enterAlways"
android:minHeight="22dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
/>
</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>
聊天片段
Chat.java
public class Chat extends Fragment {
public Chat(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_chat, container,false);
}
}
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.downy.gpslocation.Chat">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey there this is chat"
android:layout_centerInParent="true"/>
</RelativeLayout>
提前感谢您的帮助。
我在你的代码中看到的东西 ViewPagerAdapter
得到了 getCount return 0
和 getItem return null
你应该有:
@Override
public int getCount() {
return mFragment.size();
}
因为你是returning 0,说适配器是空的
还有:
@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}
到 return 给定位置的正确片段,不为空。
我需要在 Container Activity 中的选项卡布局中显示选项卡,但不知何故选项卡未显示。如果有帮助,这个项目还有三个活动,分别是登录 Activity、地图 Activity、注册 Activity。
注意:地图片段不同于地图 Activity。
Container.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new MapFragment(), "Map");
adapter.addFragment(new Chat(),"Chat");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragment = new ArrayList<>();
private List<String> mFragmentTitle = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) { return null; }
@Override
public int getCount() {
return 0;
}
public void addFragment(Fragment fragment, String fragmentTitle){
mFragment.add(fragment);
mFragmentTitle.add(fragmentTitle);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitle.get(position);
}
}
activity_container.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:background="@android:color/darker_gray"
app:layout_scrollFlags="scroll|enterAlways"
android:minHeight="22dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
/>
</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>
聊天片段 Chat.java
public class Chat extends Fragment {
public Chat(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_chat, container,false);
}
}
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.downy.gpslocation.Chat">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey there this is chat"
android:layout_centerInParent="true"/>
</RelativeLayout>
提前感谢您的帮助。
我在你的代码中看到的东西 ViewPagerAdapter
得到了 getCount return 0
和 getItem return null
你应该有:
@Override
public int getCount() {
return mFragment.size();
}
因为你是returning 0,说适配器是空的
还有:
@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}
到 return 给定位置的正确片段,不为空。