本文介绍了onPageScrolled()不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想熟悉ViewPager。我的code如下。的问题是,onPageChangeListener方法不被调用。可能是什么问题?

I'm trying to get familiar with ViewPager. My code is given below. The problem is that the onPageChangeListener methods are not being called. What can be the problem?

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中的滚动我的网页。

What I mean is that I dont get anything printed in the Logcat on scrolling my pages.

推荐答案

嗯,我经过一番试错误解决自己的问题。

Well I solved the problem myself after some trial error.

由于我是引入TitlePagerIndicatormIndicator',我只好打电话 mIndicator.setOnPageChangeListener(本); 并没有mViewPager.setOnPageChangeListener(本);

Since I was introducing a TitlePagerIndicator 'mIndicator', I had to call mIndicator.setOnPageChangeListener(this); and not mViewPager.setOnPageChangeListener(this);

这篇关于onPageScrolled()不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:40