简体   繁体   中英

How to disable viewpager swiping with finger touch which extends PagerAdapter in android

i have slider with view pager. and i have adapter extends PagerAdapter . the problem is that: i don't want may slider swiping with finger. it means, i want it works just automatically.

public class TopFoodSlider_adapter extends PagerAdapter {

    private Context context;
    private List<top_food_slider> slideList;
    private int custom_postion = 0;
    private int t =0;

    public TopFoodSlider_adapter(Context context, List<top_food_slider> slideList) {
        this.context = context;
        this.slideList = slideList;
    }



    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return (view == object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position)
    {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.top_food_slider_item,container,false);

        top_food_slider slid = slideList.get(custom_postion);
        custom_postion++;
        int img=slid.getImg_src();
        ImageView imageView=view.findViewById(R.id.imageView);

        imageView.setImageResource(img);
        container.addView(view);

        if (custom_postion>=slideList.size())
            custom_postion = 0;

        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((LinearLayout)object);
    }

}

You can disable swiping by extending StoryViewPager and overriding onTouchEvent and onInterceptTouchEvent .

class NoSwipingViewPager @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ViewPager(context, attrs) {

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return false

    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        return  false
    }
}

You can read more about it in these answers: How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM