我尝试过关注developer.android.com
的开发者指南这真的很令人困惑,因为并非所有内容都是最新的,并且大多数指南在网站上已经过时,许多方法已被弃用。所以我留下了所有这些新内容,例如AppCompatActivity
和FragmentStatePagerAdapter
。
无论如何我尽我所能,但我仍然无法在我的片段之间滑动,我不明白为什么。 这里需要注意的重要一点是,我尽量不使用标签。
此外,我讨厌发布大量代码,但为了将来参考,这可能很重要,所以请随意检查我的github project
中的整个代码MainActivity
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Drawer drawer;
private ViewPager viewPager;
private PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initTabLayout(); --> needed?
toolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initViewPager();
initMaterialDrawer();
}
private void initViewPager() {
viewPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(),CustomPagerAdapter.mNumOfTabs);
viewPager.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
if(drawer != null && drawer.isDrawerOpen())
drawer.closeDrawer();
else
super.onBackPressed();
}
private void initMaterialDrawer() {
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withName("Profile");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withName("Organisations");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withName("Extra Option");
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withName("Extra Option");
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.drawer_background1)
.withAlternativeProfileHeaderSwitching(false)
.addProfiles(
new ProfileDrawerItem()
.withIsExpanded(false)
.withName("name here")
.withEmail("emailhere.com")
.withEnabled(false)
.withIcon(new IconDrawable(this, FontAwesomeIcons.fa_reddit))
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean current) {
return false;
}
})
.build();
//create the drawer and remember the `Drawer` result object
drawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(headerResult)
.withTranslucentStatusBar(true)
.addDrawerItems(
new PrimaryDrawerItem().withName("111111111").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out)),
new PrimaryDrawerItem().withName("111111111").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out)),
new PrimaryDrawerItem().withName("111111111").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out)),
new SectionDrawerItem().withName("SECTION"),
new SecondaryDrawerItem().withName("22222").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out)),
new SecondaryDrawerItem().withName("22222").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out)),
new SecondaryDrawerItem().withName("22222").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out)),
new SecondaryDrawerItem().withName("22222").withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out))
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem instanceof Nameable) {
Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
}
//true: do nothing
//false: close drawer
return true;
}
}).build();
drawer.addStickyFooterItem(new PrimaryDrawerItem()
.withName("Log out")
.withIcon(new IconDrawable(this, FontAwesomeIcons.fa_sign_out).alpha(100)));
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
drawer.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
}
}
主要布局
<?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"
tools:context="be.kdg.kandoe.kandoe.activity.MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar">
</include>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAINACTIVITY"
android:id="@+id/main_title"
android:layout_below="@+id/toolbar"
android:layout_alignParentStart="true" />
</RelativeLayout>
PagerAdapter
public class CustomPagerAdapter extends FragmentStatePagerAdapter {
public static int mNumOfTabs;
public CustomPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ChatFragment();
case 1:
return new GameFragment();
case 2:
return new CardFragment();
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
答案 0 :(得分:1)
pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(),CustomPagerAdapter.mNumOfTabs);
mNumOfTabs
未在CustomPagerAdapter
你需要这样做..
pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(),3);
或
public class CustomPagerAdapter extends FragmentStatePagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ChatFragment();
case 1:
return new GameFragment();
case 2:
return new CardFragment();
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
}
适配器以这种方式设置
pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager());
您的主要布局需要具有垂直方向的LinearLayout才能拥有完美的外观。只需在将其更改为
之前使用现有代码即可看出差异