我目前正在开发一个Android应用程序,我使用导航抽屉以及使用viewPager浏览片段的滑动手势。
基本上,我希望用户能够使用导航抽屉选择页面(片段),并能够滑动到下一页(片段)。
我现在的问题是他们重叠了。滑动手势按预期工作,但当我在导航抽屉上选择另一个页面时,它们的底层页面不会消失。我需要一些东西来刷新"每个片段。
这是我在MainActivity.java页面中的代码
package com.example.home.cloud;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
private static final int NUM_PAGES = 10;
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().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);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter
{
public MyFragmentStatePagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
final Fragment result;
switch (position) {
case 0:
result= new HomeFragment();
break;
case 1:
result= new OverviewFragment();
break;
case 2:
result= new BenFragment();
break;
case 3:
result= new TechFragment();
break;
case 4:
result= new ArcFragment();
break;
case 5:
result= new DmodFragment();
break;
case 6:
result= new SmodFragment();
break;
case 7:
result= new VirtFragment();
break;
case 8:
result= new StorageFragment();
break;
case 9:
result= new SecurityFragment();
break;
default: result=null;
break;
}
/* if (result != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, result);
fragmentTransaction.commit();
}*/
return result;
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
@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;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
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);
break;
case 1:
fragment = new OverviewFragment();
title = getString(R.string.title_overview);
break;
case 2:
fragment=new BenFragment();
title="Benefits and Risks";
break;
case 3:
fragment=new TechFragment();
title="Technologies behind Cloud Computing";
break;
case 4:
fragment = new ArcFragment();
title="Architecture";
break;
case 5:
fragment= new DmodFragment();
title="Deployment Models";
break;
case 6:
fragment = new SmodFragment();
title="Service Models";
break;
case 7:
fragment = new VirtFragment();
title="Virtualization";
break;
case 8:
fragment = new StorageFragment();
title="Data Storage";
break;
case 9:
fragment = new SecurityFragment();
title="Security";
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);
}
}
}
答案 0 :(得分:2)
问题在于您以两种不同的方式显示片段。您的displayView()
方法正在创建新片段并显示它们,而不是显示ViewPager
中已加载的片段。要解决此问题,您需要displayView()
来设置ViewPager
位置。
private void displayView(int position) {
mPager.setCurrentItem(position);
getSupportActionBar().setTitle(mPagerAdapter.getPageTitle(position));
}