我正在构建一个带有一些标签的应用,旁边有一个NavigationDrawer
。我的问题是我无法显示NavigationDrawer
中片段的内容:当我点击它时没有任何反应,我仍然看到我的标签,但没有新的片段。
我按照这些教程,一个用于NavigationDrawer,另一个用于选项卡,两个都单独工作。
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
MainActivity.java (如果您需要更多代码,请告诉我,我会编辑我的帖子并添加它)
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_favorite_white_24dp,
R.drawable.ic_lightbulb_outline_white_24dp,
R.drawable.ic_face_white_24dp
};
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
Log.i("USER ", mAuth.getCurrentUser().getEmail().toString());
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
Log.d("ACTION BAR?", actionBar.toString());
actionBar.setDisplayShowHomeEnabled(true);
}
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("TAG", "onAuthStateChanged:signed_out");
}
// ...
}
};
// ...
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
Log.i("DISPLAY VIEW", "HOME FRAGMENT DISPLAYED");
break;
case 1:
fragment = new FriendsFragment();
title = getString(R.string.title_friends);
Log.i("DISPLAY VIEW", "FRIENDS FRAGMENT DISPLAYED");
break;
case 2:
fragment = new MessagesFragment();
title = getString(R.string.title_messages);
Log.i("DISPLAY VIEW", "MSG FRAGMENT DISPLAYED");
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
我的displayView()
应显示NavigationDrawer
中的正确视图,但我也有tabLayout
。当我点击NavigationDrawer
中的不同项时,我在工具栏中更改了标题,但仅此而已。
这是很好的解释,但我可能错过了一些东西,我知道这可能是一些新手问题,但因为它是我的第一个Android应用程序,我想我有点像。如果有人可以看一看并告诉我应该做些什么来让这两个人一起工作,对我来说真的很有帮助。谢谢!