gpt4 book ai didi

android - TabLayout 中的选项卡不显示

转载 作者:行者123 更新时间:2023-11-29 19:22:29 24 4
gpt4 key购买 nike

我需要在 Container Activity 中的选项卡布局中显示选项卡,但不知何故选项卡未显示。如果有帮助,这个项目还有三个 Activity ,即登录 Activity 、 map Activity 、注册 Activity 。

注意: map fragment 不同于 map Activity 。

容器.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>

聊天 fragment 聊天.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>

提前感谢您的帮助。

最佳答案

你应该:

@Override
public int getCount() {
return mFragment.size();
}

因为你返回的是0,表示适配器是空的。

还有:

@Override
public Fragment getItem(int position) {
return mFragment.get(position);
}

返回给定位置的正确 fragment ,而不是 null。

关于android - TabLayout 中的选项卡不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com