onPageScrolled()未被调用

问题描述 投票:24回答:4

我正在努力熟悉ViewPager。我的代码如下。问题是没有调用onPageChangeListener方法。可能是什么问题?

public class TabsViewPagerFragmentActivity extends SherlockFragmentActivity implements ViewPager.OnPageChangeListener {


private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private PageIndicator mIndicator;

private static final String[] CONTENT = new String[] { "Home", "Popular", "Chat", "Profile"};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar bar = getSupportActionBar();

    View customview = LayoutInflater.from(this).inflate(R.layout.actionbar_custom_layout, null);

    ImageView im = (ImageView)customview.findViewById(R.id.add);

    /*im.getLayoutParams().height = h;
    im.getLayoutParams().width = h;*/

    bar.setCustomView(customview);
    bar.setDisplayShowCustomEnabled(true);
    bar.setDisplayShowTitleEnabled(false);
    bar.setDisplayShowHomeEnabled(false);
    // Inflate the layout
    setContentView(R.layout.tabs_viewpager_layout);


    // Intialise ViewPager
    this.intialiseViewPager();

    mIndicator = (TitlePageIndicator)findViewById(R.id.titles);
    mIndicator.setViewPager(mViewPager);

    TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.titles);

    indicator.setTextColor(0xAAfb891e);
    indicator.setSelectedColor(0xFFfb891e);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    menu.add("Search")
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);


    return true;
}



public int getCount() {
    return CONTENT.length;
}


protected void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);
}
private void intialiseViewPager() {


    this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(),
                                            fragments);

    this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
    this.mViewPager.setAdapter(this.mPagerAdapter);
    this.mViewPager.setOnPageChangeListener(this);



}

public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {
    // TODO Auto-generated method stub

    System.out.println(positionOffsetPixels);

}

public void onPageSelected(int position) {
    // TODO Auto-generated method stub

    System.out.println(position);
}


public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub

    System.out.println(state);

}
}

我的意思是我在滚动页面时没有在Logcat中打印任何内容。

android android-viewpager
4个回答
119
投票

好吧,我在一些试错后自己解决了这个问题。

由于我引入了Title Page Indicator'm Indicator',我不得不调用mIndicator.setOnPageChangeListener(this);而不是mViewPager.setOnPageChangeListener(this);


2
投票

System.out流不会打印到Logcat。使用Log类而不是像Log.d("MyApp","message")


0
投票

你编写代码的方式我猜你正在实现ViewPager.OnPageChangeListener

更改

this.mViewPager.setOnPageChangeListener(mIndicator);

this.mViewPager.setOnPageChangeListener(this);


0
投票

我跟着ProgrAmmar的提示,但它对我不起作用。就我而言,我用过

mIndicator.setCurrentItem(position);

它工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.