Onclick on view pager in android does not work in my code

前端 未结 3 1043
谎友^
谎友^ 2021-02-14 06:08

i have implemented view pager in my code and i have put on-click listener on view pager but my on-click listener does not working i\'ll post my code

viewpager.s         


        
相关标签:
3条回答
  • 2021-02-14 07:05

    There is no onClick listener in ViewPager, You can use the same effect by using a Recyclerview instead of a ViewPager is shown as below

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_scroll_page"
            android:layout_width="wrap_content"
            android:layout_height="250dp"
            android:orientation="horizontal"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
    

    Using Recyclerview as horizontal then you can scroll it and get the click events. Please note that use the RecyclerView.Adapter

    0 讨论(0)
  • 2021-02-14 07:10

    Use clicklistener inside the item of ViewPager. This is not working because Child of ViewPager is intercepting event of parent(i.e ViewPager). E.G if you are adding ImageView inside ViewPager using PagerAdapter then use click of ImageView to make it simple

    0 讨论(0)
  • 2021-02-14 07:11

    I implemented custom ViewPager:

    public class ClickableViewPager extends ViewPager {
    
        private OnClickListener mOnClickListener;
    
        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 setOnViewPagerClickListener(OnClickListener onClickListener) {
            mOnClickListener = onClickListener;
        }
    
        public interface OnClickListener {
            void onViewPagerClick(ViewPager viewPager);
        }
    
        private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
    
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if(mOnClickListener != null) {
                   mOnClickListener.onViewPagerClick(ClickableViewPager.this);
                }
    
                return true;
            }
        }
    }
    

    And example how to implement onClickListener:

    ClickableViewPager viewPager = (ClickableViewPager) findViewById(R.id.viewPager);
    mViewPager.setOnViewPagerClickListener(new ClickableViewPager.OnClickListener() {
                @Override
                public void onViewPagerClick(ViewPager viewPager) {
                    //yourCode
                }
            });
    
    0 讨论(0)
提交回复
热议问题