jfeinstein10/slidingmenu,带 CirclePageIndicator 的 viewpager 不工作

jfeinstein10/slidingmenu, viewpager with CirclePageIndicator not working

我遇到了一个奇怪的情况。我正在使用 Slidingmenu,viewpager 和 CirclePageIndicator。当前查看寻呼机和滑动菜单正在工作,但 CirclePageIndicator 未指示确切的页面。知道为什么吗? 下面是带截图的 activity 代码。 我的代码-

public class MainActivity extends FragmentActivity {

// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;   
CirclePageIndicator mIndicator; 
private int mWidthScreen;
private int mHeightScreen;  
private Bundle bundle;
private List<Fragment> frgScreens;
private int selectedtheme;
private Handler mHandler = new Handler();
//public  mlayoutForeground;

//Menu buttons
private ImageView mBtnMenu;
private SlidingMenu menu;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the view from viewpager_main.xml
    setContentView(R.layout.viewpager_main);

    getScreenHeightWidhth();    
    buildargument();

    //setting the required theme
    this.selectedtheme=1;       
    initialisePaging(selectedtheme); //Page creating function

    generateMenu();



}

private void initialisePaging(int theme) {  

    // Locate the ViewPager in viewpager_main.xml
    viewPager = (ViewPager) findViewById(R.id.pager);

    //#FIXME try to implement like theme.basic
    //clearing old entries
    frgScreens=new Vector<Fragment>();

    if(theme==1)
    {
        frgScreens=this.basicThemes();
    }

    if(theme==2)
    {   

        frgScreens=this.forecastThemes();
    }


  //Bind result to ViewPagerAdapter 
    adapter =new ViewPagerAdapter(this.getSupportFragmentManager(), frgScreens);

    // Binds the Adapter to the ViewPager
    this.viewPager.setAdapter(adapter);     
    addAndConfigureCirclePagerAdapter();



}


/*
 * forecast theme fragment
 */
private List<Fragment> forecastThemes()
{
    Fragment frgForecast1=Fragment.instantiate(this,ForecastScreen1.class.getName());

    //#FIXME forecast2 problematic
    //Fragment frgForecast2=Fragment.instantiate(this,ForecastScreen2.class.getName());

    frgForecast1.setArguments(bundle);
    //frgForecast2.setArguments(bundle);


    List<Fragment> frgForecastScreens = new Vector<Fragment>();

    frgForecastScreens.add(frgForecast1);
    //frgForecastScreens.add(frgForecast2);

    return frgForecastScreens;

}
/*
 * Basic theme fragments
 */
private List<Fragment> basicThemes()
{
    //declaring fragments Group Basic
    Fragment frgBasic1=Fragment.instantiate(this,Basic1.class.getName());
    Fragment frgBasic2=Fragment.instantiate(this,Basic2.class.getName());
    Fragment frgBasic3=Fragment.instantiate(this,Basic3.class.getName());
    Fragment frgBasic4=Fragment.instantiate(this,Basic4.class.getName());

    //Passing arguments
    frgBasic1.setArguments(bundle);
    frgBasic2.setArguments(bundle);
    frgBasic3.setArguments(bundle);
    frgBasic4.setArguments(bundle);


    //Loading screens into Fragment list
    List<Fragment> frgBasicScreens = new Vector<Fragment>();

    frgBasicScreens.add(frgBasic1);
    frgBasicScreens.add(frgBasic2);
    frgBasicScreens.add(frgBasic3);
    frgBasicScreens.add(frgBasic4);


    return frgBasicScreens;

}


 /*
  * Applying circlepageradapter color
  */
 private void addAndConfigureCirclePagerAdapter()
 {

     this.viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                            super.onPageSelected(position);
                            if (position == 0) {
                                menu.removeIgnoredView(viewPager);
                            } else {
                                menu.addIgnoredView(viewPager);
                            }
                        }
                    });

    mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
    this.mIndicator.setBackgroundColor(Color.TRANSPARENT);
    this.mIndicator.setPadding(5, 5, 5, 5);
    this.mIndicator.setRadius(12);
    this.mIndicator.setFillColor(Color.argb(255, 175, 220, 243));
    final float density = getResources().getDisplayMetrics().density;
    this.mIndicator.setRadius(6 * density);
    this.mIndicator.setViewPager(viewPager); 


 }

/*
 * Function to get height and width
 * 
 */
private void getScreenHeightWidhth()
{
     DisplayMetrics localDisplayMetrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);            
     this.mWidthScreen = localDisplayMetrics.widthPixels;
     this.mHeightScreen = localDisplayMetrics.heightPixels; 
     mHandler.post(new DisplayToast(this, "Width Screen:"+ mWidthScreen + ". Height Screen: "+ mHeightScreen+ "."));
}

/*
 * Bundle that bind extra arguments with every fragments
 * mWidthScreen : Putting 'mWidthScreen'as a pasing argument
 */
private void buildargument()
{
    this.bundle = new Bundle();
    bundle.putInt("mWidthScreen", this.mWidthScreen);
}

public void generateMenu()
{
    //enable button
    //buttonsOn();

    mBtnMenu = (ImageView) findViewById(R.id.btnMenu);

    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidth(5);
    menu.setFadeDegree(0.0f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setBehindWidth(500);
    menu.setMenu(R.layout.menu_frame);

    mBtnMenu.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
         // menu.showMenu();

          menu.toggle();
         mBtnMenu.setEnabled(true);
        }
    });





}

public void buttonsOff()
  {
    this.mBtnMenu.setEnabled(false);

  }

  public void buttonsOn()
  {

    this.mBtnMenu.setEnabled(true);
  }


 @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}

当我 运行 代码时,我发现 circlepageindicator 指示第一页,如果我也滑动到不同的页面。 这是短片。

正常应该是这样的

如有任何代码帮助,我们将不胜感激。亲爱的投票者,请post请在投票前发表评论。请允许我纠正。谢谢您的合作。 问候 比斯瓦吉特

你错过了documentation中的一个关键点(我的重点):

(Optional) If you use an OnPageChangeListener with your view pager you should set it in the indicator rather than on the pager directly.

我会更具体一点。当你打电话给...

this.mIndicator.setViewPager(viewPager); 

... 指标将自身设置为给定 ViewPagerOnPageChangeListener,有效地覆盖任何已设置的侦听器。这意味着您在指标之前设置的这个监听器是无用的:

 this.viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
                        super.onPageSelected(position);
                        if (position == 0) {
                            menu.removeIgnoredView(viewPager);
                        } else {
                            menu.addIgnoredView(viewPager);
                        }
                    }
                });

如文档所述,您应该改为将侦听器设置为指示器(它基本上会代理事件)。因此,将您的代码更改为:

mIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { ... });

如果您使用 ViewPager 的最新版本,您会看到它是 setOnPageChangeListener() method is actually deprecated in favour of addOnPageChangeListener()。后者显然支持将多个侦听器附加到寻呼机。

上次检查ViewPager源代码时,setadd方法相互独立工作。因此,您也可以逃脱:

this.viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { ... });

虽然那不是我的第一选择。