我想在我的View Pager中设置点击事件。我的ViewPagerAdapter
看起来像这样 -
public class ViewPagerAdapder extends PagerAdapter {
Activity activity;
String image[];
public ViewPagerAdapder(Activity act, String[] imgArra) {
image = imgArra;
activity = act;
}
public int getCount() {
return image.length;
}
public Object instantiateItem(View collection, final int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
new ImageDownloaderTask(view).execute(image[position]);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setAdjustViewBounds(true);
((ViewPager) collection).addView(view, 0);
// view.setBackgroundResource(imageArray[position]);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
我正在我的主要活动中设置OnClickListener
myPager = (ViewPager) view.findViewById(R.id.viewpager);
myPager.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Ckecking Success",
Toast.LENGTH_SHORT).show();
}
}
我的ViewPager工作正常。我试图在Main Activity和ViewPagerAdapter类中实现OnClickListener。它无论如何都无效。
答案 0 :(得分:2)
使用此类进行查看分页器点击。
ClickableViewPager.java
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;
}
}
}
点击活动
clickablePager.setOnItemClickListener(new ClickableViewPager.OnItemClickListener() {
@Override
public void onItemClick(int position) {
}
});
答案 1 :(得分:1)
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d(TAG, "onPageScrolled")
}
@Override
public void onPageSelected(int position) {
Log.d(TAG, "onPageScrollStateChanged")
}
@Override
public void onPageScrollStateChanged(int state) {
Log.d(TAG, "onPageScrollStateChanged")
}
});
将上述片段用于您的活动并执行您在方法中执行的操作
答案 2 :(得分:0)
您不能对viewPager使用onClickListener。您需要以下列方式使用,
将MotionEvent发送到GestureDetector
tapGestureDetector = new GestureDetector(this, new TapGestureListener());
viewPager.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
tapGestureDetector.onTouchEvent(event);
return false;
}
});