我的android应用程序中有一个viewPager图像和文本,我的目标是创建一个runnable让这个视图寻呼机自动滚动,而我的另一个目标是当用户点击此viewpager中的一个项目时我需要去另一项活动。
这是我的可运行的:
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES - 1) {
currentPage = -1;
}
pager.setCurrentItem(currentPage++,true);
pager.setScrollBarFadeDuration(R.styleable.CirclePageIndicator_fillColor);
pager.setScrollDurationFactor(4);
mIndicator.setCurrentItem(currentPage++);
}
};
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 1000, 3000);
问题是每次runnable调用" SetCurrentItem()"调用onpageselected函数,然后打开活动。那么如何防止每3秒调用一次setCurrentItem时调用onpageselected呢?
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Picture current = (Picture)pageAdapter.getItem(arg0);
Article a = getArticleById(current.getArticleId(), p);
Bundle bundle=new Bundle();
Intent intent = new Intent();
intent.setClass(HomePageActivity.this,DetailActivity.class);
//.getString(tag)));
bundle.putParcelable("article", a);
bundle.putParcelableArrayList("pic", a.getPic());
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
currentPage=arg0;
//mIndicator.setCurrentItem(arg0);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:1)
您应该为适配器内的ViewPager项添加一个单击侦听器。 onPageSelected
方法不会监听点击事件。
在ViewPager适配器的instantiateItem
方法中,执行以下操作:
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.your_layout, null);
rootView.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//do the click thing
}
});
container.addView(rootView);
return rootView;