SoFunction
Updated on 2025-03-11

Android Custom ViewGroup implements scrolling operations limited by boundaries (3)

Previous article "Custom viewgroup (2)" Address:https:///article/

Code

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
  * Custom ViewGroup
  * On the basis of scrolling, the boundary limit has been added
  */
public class ScrollViewGroup extends ViewGroup {
 //Scrolling calculation auxiliary class private Scroller mScroller;
 //X coordinate of finger landing point private float mLastMotionX = 0;
 //Screen Width private int screenWidth;

 /**
   * Called when creating an object using the new keyword
   * @param context
   */
 public ScrollViewGroup(Context context) {
  this(context, null);
 }
 /**
   * Called when used in XML files
   * @param context
   * @param attrs attribute: such as android:layout_width="wrap_content"
   */
 public ScrollViewGroup(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 /**
   * Called in the xml file and use custom properties
   * @param context
   * @param attrs attribute: such as android:layout_width="wrap_content"
   * @param defStyleAttr id of custom attributes
   */
 public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init(context);
 }

 /**
   * Initialization method
   * Initialize the scrolling auxiliary class Scroller and calculate the screen width
   * @param context
   */
 private void init(Context context) {
  mScroller = new Scroller(context);
  WindowManager manager = (WindowManager) context
    .getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics outMetrics = new DisplayMetrics();
  ().getMetrics(outMetrics);
  screenWidth = ;
 }

 /**
   * Method that needs to be rewrite when scrolling, used to control scrolling
   */
 @Override
 public void computeScroll() {
  //Judge the stop when scrolling  if (()) {
   //Scroll to the specified position   scrollTo((), ());
   //This sentence must be written, otherwise it cannot be refreshed in real time   postInvalidate();
  }
 }

 /**
   * Finger touch screen event monitoring
   * @param event
   * @return
   */
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
  int action = ();
  float x = ();
  switch (action) {
   case MotionEvent.ACTION_DOWN:
    if (!()) {
     ();
    }
    mLastMotionX = ();
    break;
   case MotionEvent.ACTION_MOVE:
    float delt = mLastMotionX - x;
    mLastMotionX = x;
    scrollBy((int) delt, 0);
    break;
   case MotionEvent.ACTION_UP:
    View lastChild=getChildAt(getChildCount()-1);
    int finalyChild= (int) (()+()-screenWidth);
    if (getScrollX()<0){
     scrollTo(0,0);
    }
    if (getScrollX()>=finalyChild)
     scrollTo(finalyChild,0);
    invalidate();
    break;

   default:
    break;
  }

  return true;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  //Reset width and height  (measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));
 }

  /**
   * Measure width
   */
 private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
  // Width  int sizeWidth = (widthMeasureSpec);
  int modeWidth = (widthMeasureSpec);
  //The width of the parent control (wrap_content)  int width = 0;
  int childCount = getChildCount();

  //Remeasure the width of the subview and the maximum height  for (int i = 0; i < childCount; i++) {
   View child = getChildAt(i);
   measureChild(child, widthMeasureSpec, heightMeasureSpec);
   MarginLayoutParams lp = (MarginLayoutParams) ();
   int childWidth = () +  + ;
   width += childWidth;
  }
  return modeWidth ==  ? sizeWidth : width;
 }

 /**
   * Measure the height
   */
 private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
  //high  int sizeHeight = (heightMeasureSpec);
  int modeHeight = (heightMeasureSpec);
  //The parent control's high (wrap_content)  int height = 0;
  int childCount = getChildCount();

  //Remeasure the width of the subview and the maximum height  for (int i = 0; i < childCount; i++) {
   View child = getChildAt(i);
   measureChild(child, widthMeasureSpec, heightMeasureSpec);
   MarginLayoutParams lp = (MarginLayoutParams) ();
   int childHeight = () +  + ;
   height += childHeight;
  }
  height = height / childCount;
  return modeHeight ==  ? sizeHeight : height;
 }

 /**
   * Set position for sub-layout
   */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int childLeft = 0;//The spacing on the left side of the subview  int childWidth;//The width of the subview  int height = getHeight();//The width of the screen  int childCount = getChildCount();//The number of subviews  for (int i = 0; i < childCount; i++) {
   View child = getChildAt(i);
   MarginLayoutParams lp = (MarginLayoutParams) ();
   childWidth = () +  + ;
   (childLeft, 0, childLeft + childWidth, height);
   childLeft += childWidth;
  }
 }

 @Override
 public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new MarginLayoutParams(getContext(), attrs);
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.