出于学习目的,我正在从事一个聊天应用程序项目。我在mainactvity上添加了Tabs。我已经尝试了很多事情,但是无法上班。 这是我的MainActivity类
public class MainActivity extends AppCompatActivity {
FirebaseAuth firebaseAuth;
Toolbar toolbar;
ViewPager viewPager;
public Pagesection pagesection;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.main_app);
// setSupportActionBar(toolbar);
getSupportActionBar().setTitle("MAD");
viewPager = (ViewPager) findViewById(R.id.mainpager);
pagesection = new Pagesection(getSupportFragmentManager());
tabLayout = (TabLayout) findViewById(R.id.main_tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Pagesection adapter = new Pagesection(getSupportFragmentManager());
adapter.addFragment(new Friends(), "TAB1");
adapter.addFragment(new Chat(), "TAB2");
adapter.addFragment(new Request(), "TAB3");
viewPager.setAdapter(pagesection);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu , menu);
return super.onCreateOptionsMenu(menu);
}
}
这是我的网页板块类
class Pagesection extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public Pagesection(FragmentManager fm) {
super(fm);
}
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
这是我的Main.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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
android:orientation="vertical"
tools:context="justeat.mobileproj.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/main_app"
layout="@layout/appbar_layout" />
<android.support.design.widget.TabLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/mainpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appbar">
</android.support.v4.view.ViewPager>
</RelativeLayout>
当我运行该应用程序时,它没有显示错误。但Tablayout是空的,我的片段活动没有出现。由于我是android开发的新手,将不胜感激。谢谢!!