Left-right arrow indicators over a ViewPager

前端 未结 5 1432
小蘑菇
小蘑菇 2021-01-30 15:04

I want to show left and right arrows over my ViewPager, to indicate swiping.

I added two ImageButtons over the ViewPager-element but those areas then block the ViewPager

5条回答
  •  旧时难觅i
    2021-01-30 15:14

    The code below worked for me perfectly well. NB: Use FrameLayout as it allows overlapping views

            
    
                
    
                
    
                
    
            
    

    The following part I used to handle ImageButton's click events

    viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    
    leftNav = (ImageButton) view.findViewById(R.id.left_nav);
    rightNav = (ImageButton) view.findViewById(R.id.right_nav);
    
    // Images left navigation
        leftNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tab = viewPager.getCurrentItem();
                if (tab > 0) {
                    tab--;
                    viewPager.setCurrentItem(tab);
                } else if (tab == 0) {
                    viewPager.setCurrentItem(tab);
                }
            }
        });
    
        // Images right navigatin
        rightNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tab = viewPager.getCurrentItem();
                tab++;
                viewPager.setCurrentItem(tab);
            }
        });
    

    Output

提交回复
热议问题