I have extended the ViewPager class and implemented onPageChangeListener. I use this ViewPager in conjunction with a CirclePageIndicator inside a SherlockFragment. The ViewPager contains ImageViews and TextViews, nothing fancy.
我扩展了ViewPager类并实现了onPageChangeListener。我将此ViewPager与SherlockFragment中的CirclePageIndicator结合使用。 ViewPager包含ImageViews和TextViews,没什么特别的。
I need to do something below the ViewPager every time the Page is being changed. So, I figured I need to use the onPageScrollStateChanged and verify when the state is IDLE, or something.
每次更改页面时,我都需要在ViewPager下面做一些事情。所以,我想我需要使用onPageScrollStateChanged并验证状态是IDLE还是其他什么。
The problem is that from the 3 implemented methods (onPageScrollStateChanged, onPageSelected and onPageScrolled), only onPageScrolled gets called (I checked with a log message in each method). Why is that ? Code below.
问题是从3个实现的方法(onPageScrollStateChanged,onPageSelected和onPageScrolled),只调用onPageScrolled(我在每个方法中检查了一条日志消息)。这是为什么 ?代码如下。
public class UninterceptableViewPager extends ViewPager implements OnPageChangeListener{
private ArrayList<TwoRowListViewAdapter> mAdaptersList = null;
private ListView mListView = null;
private ProgressBar mProgressBar = null;
public UninterceptableViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public boolean onInterceptTouchEvent(MotionEvent event)
{
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(event);
}
public void setAdaptersList(ArrayList<TwoRowListViewAdapter> adaptersList)
{
mAdaptersList = adaptersList;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
super.onPageScrolled(position++, positionOffset, positionOffsetPixels);
if (positionOffset == 0) // FIXME
loadHistoryPage(position);
}
@Override
public void onPageSelected(int position) {}
@Override
public void onPageScrollStateChanged(int state) {}
// -------------------------------------------------------------------------------------------
// -------------- Helper Methods
// -------------------------------------------------------------------------------------------
public void setListView(ListView listview)
{
mListView = listview;
}
public void setProgressBar(ProgressBar progressBar)
{
mProgressBar = progressBar;
}
private void loadHistoryPage(int position)
{
mListView.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
mListView.setAdapter(null);
mListView.setAdapter(mAdaptersList.get(position-1));
mProgressBar.setVisibility(View.INVISIBLE);
mListView.setVisibility(View.VISIBLE);
}
}
2 个解决方案
#1
68
When you use PageIndicator in conjunction with the Viewpager then the onPageChangeListener of the ViewPager is not called. I don't know the reason for this but this happened when I was using both. May be cause the page indicator consumes the page change event. Instead you should set a page change listener to the PageIndicator and it will be called when the page changes. Here is how you can do this:
将PageIndicator与Viewpager结合使用时,不会调用ViewPager的onPageChangeListener。我不知道原因,但这发生在我使用它们的时候。可能是因为页面指示器消耗了页面更改事件。相反,您应该将页面更改侦听器设置为PageIndicator,并在页面更改时调用它。以下是如何执行此操作的方法:
indicator.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected (int page)
{
//page changed
}
@Override
public void onPageScrolled (int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged (int arg0)
{
}
});
I hope this will help. Feel free to discuss.
我希望这将有所帮助。随意讨论。
#2
0
setOnPageChangeListener
is deprecated. use addOnPageChangeListener
不推荐使用setOnPageChangeListener。使用addOnPageChangeListener
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
#1
68
When you use PageIndicator in conjunction with the Viewpager then the onPageChangeListener of the ViewPager is not called. I don't know the reason for this but this happened when I was using both. May be cause the page indicator consumes the page change event. Instead you should set a page change listener to the PageIndicator and it will be called when the page changes. Here is how you can do this:
将PageIndicator与Viewpager结合使用时,不会调用ViewPager的onPageChangeListener。我不知道原因,但这发生在我使用它们的时候。可能是因为页面指示器消耗了页面更改事件。相反,您应该将页面更改侦听器设置为PageIndicator,并在页面更改时调用它。以下是如何执行此操作的方法:
indicator.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected (int page)
{
//page changed
}
@Override
public void onPageScrolled (int arg0, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged (int arg0)
{
}
});
I hope this will help. Feel free to discuss.
我希望这将有所帮助。随意讨论。
#2
0
setOnPageChangeListener
is deprecated. use addOnPageChangeListener
不推荐使用setOnPageChangeListener。使用addOnPageChangeListener
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});