ViewPager OnItemClickListener

我是新来的Viewpager,并从谷歌阅读http://developer.android.com/reference/android/support/v4/view/ViewPager.html后,我似乎无法find任何关于viewPager.setOnItemClickListener(new OnItemClickListener() {我们有其他的select作为项目点击?

testing出来

 viewPager.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(MainActivity.this, SingleItemView.class); i.putExtra("flag", flag); i.putExtra("position", position); startActivity(i); } }); 

错误:方法setOnItemClickListener(新的AdapterView.OnItemClickListener(){})未定义typesViewPager

ViewPager没有OnItemClickcallback方法。 如果你想在每个页面上点击事件,那么你将不得不在你的适配器中build立你的监听器到页面内容。

像这样的东西:

 @Override public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick() LayoutInflater inflater = (LayoutInflater) collection.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View page = inflater.inflate(R.layout.YOUR_PAGE, null); page.setOnClickListener(new OnClickListener(){ public void onClick(View v){ //this will log the page number that was click Log.i("TAG", "This page was clicked: " + pos); } }); ((ViewPager) collection).addView(page, 0); return page; } 

正是你需要的将取决于你还在做什么你在instantiateItem()里面你还没有发布,所以我不能给你一个更具体的答案。

我实现了自定义ViewPager

 public class ClickableViewPager extends ViewPager { private OnItemClickListener mOnItemClickListener; public ClickableViewPager(Context context) { super(context); setup(); } public ClickableViewPager(Context context, AttributeSet attrs) { super(context, attrs); setup(); } private void setup() { final GestureDetector tapGestureDetector = new GestureDetector(getContext(), new TapGestureListener()); setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { tapGestureDetector.onTouchEvent(event); return false; } }); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } public interface OnItemClickListener { void onItemClick(int position); } private class TapGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapConfirmed(MotionEvent e) { if(mOnItemClickListener != null) { mOnItemClickListener.onItemClick(getCurrentItem()); } return true; } } } 

和代码来实现onItemClickListener

 ClickableViewPager viewPager = (ClickableViewPager) findViewById(R.id.viewPager); viewPager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() { @Override public void onItemClick(int position) { // your code } }); 

我不确定你会期待什么与上面的听众对你做出回应。

有一个setOnClickListener(View.OnClickListener l) ,可以用来检测对viewPager的触摸。

您也可以使用onPageScrolled获取用户input, onPageSelectedonTabSelected

 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } @Override public void onPageScrollStateChanged(int state) { } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { Object tag = tab.getTag(); for (int i=0; i<mTabs.size(); i++) { if (mTabs.get(i) == tag) { mViewPager.setCurrentItem(i); } } }