SoFunction
Updated on 2025-03-11

Android realizes the sliding guidance effect of partial picture

Android realizes the sliding guidance effect of partial picture

The event listener code of ViewPager is as follows:

// Sliding page to change event listener  private class ImagePageChangeListener implements OnPageChangeListener {
    @Override 
    public void onPageScrollStateChanged(int arg0) { 
      // TODO Auto-generated method stub 
 
    } 
 
    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
      // TODO Auto-generated method stub 
 
    } 
 
    @Override 
    public void onPageSelected(int index) { 
      pageIndex = index;
      (index);
      (()[index]);
      
      for (int i = 0; i < ; i++) { 
        imageCircleViews[index].setBackgroundResource(.dot_selected);
        
        if (index != i) { 
          imageCircleViews[i].setBackgroundResource(.dot_none); 
        } 
      }
    } 
  }
 

In the event listener, the title and dot picture are mainly transformed in the callback function onPageSelected(int index).

Since the content below the sliding area is unchanged, that is, it does not slide, as I mentioned above, the content may be beyond the scope of the screen, so we need to use a ScrollView to display the scrollbar when there is too much content. Maybe some friends will think that to display the scrollbar, I also know how to use ScrollView. What I want to say here is that there is a ViewPager control and a ScrollView here. If two Views are used separately, there will be no problem. Unfortunately, however, problems arise when the two are combined. What's the problem? It is a rebound when sliding the picture, which is that it is difficult to slide when sliding. I feel very difficult when sliding, and the picture just can't slide. This is the conflict between two views, because both views are sliding views, and they will calculate the corresponding position and judge the corresponding distance.

How do we resolve this conflict? Here we need to override the onInterceptTouchEvent() callback function of ScrollView. You need to add a new ScrollViewExtend class to the program and inherit it from ScrollView. The following is its code:

package ;

import ;
import ;
import ;
import ;

/**
  * ScrollView compatible with ViewPager
  * @Description: Solved the sliding rebound problem of ViewPager in ScrollView

  * @File:

  * @Package

  * @Author Hanyonglu

  * @Date 2012-6-18 01:34:50 pm

  * @Version V1.0
  */
public class ScrollViewExtend extends ScrollView {
  // Sliding distance and coordinates  private float xDistance, yDistance, xLast, yLast;

  public ScrollViewExtend(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (()) {
      case MotionEvent.ACTION_DOWN:
        xDistance = yDistance = 0f;
        xLast = ();
        yLast = ();
        break;
      case MotionEvent.ACTION_MOVE:
        final float curX = ();
        final float curY = ();
        
        xDistance += (curX - xLast);
        yDistance += (curY - yLast);
        xLast = curX;
        yLast = curY;
        
        if(xDistance > yDistance){
          return false;
        } 
    }

    return (ev);
  }
}

Thank you for reading, I hope it can help you, thank you for your support for this site!