java – Android中的Clickable View Pager

前端之家收集整理的这篇文章主要介绍了java – Android中的Clickable View Pager前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想在我的View Pager中设置click事件.我的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.它无论如何都无效.

最佳答案
使用此类进行查看分页器单击.

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) {
    }
 });
原文链接:https://www.f2er.com/android/429978.html

猜你在找的Android相关文章