Click here to Skip to main content
15,437,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to implement OnClickListener in this ViewPager2 Image Slide. I want to click different images to take me to different activities. Like clicking the first slider image will open the second activity, the second image will open the third activity and so on. I have tried the code bellow for the Event image slider. But can't add onclicklistner.

Can anyone help me to implement the onclicklistner?


What I have tried:

Java
List<SliderItem> sliderItems = new ArrayList<>();
        sliderItems.add(new SliderItem(R.drawable.sliderimg1));
        sliderItems.add(new SliderItem(R.drawable.sliderimg2));
        sliderItems.add(new SliderItem(R.drawable.sliderimg3));
        sliderItems.add(new SliderItem(R.drawable.sliderimg4));
        sliderItems.add(new SliderItem(R.drawable.sliderimg5));

        viewPager2.setAdapter(new SliderAdapter(sliderItems,viewPager2));
        viewPager2.setClipToPadding(false);
        viewPager2.setClipChildren(false);
        viewPager2.setOffscreenPageLimit(3);
        viewPager2.getChildAt(0).setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);

        CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
        compositePageTransformer.addTransformer(new MarginPageTransformer(40));
        compositePageTransformer.addTransformer(new ViewPager2.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                float r = 1 - Math.abs(position);
                page.setScaleY(0.85f + r*0.15f);
               
            }
        });

        viewPager2.setPageTransformer(compositePageTransformer);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                sliderHandler.removeCallbacks(sliderRunnable);
                sliderHandler.postDelayed(sliderRunnable,3000);
            }
        });
Posted
Updated 28-Mar-21 20:48pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900