国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義viewgroup可滾動布局 GestureDetector手勢監(jiān)聽(5)

2019-12-12 04:16:11
字體:
供稿:網(wǎng)友

這篇效果和上一篇://m.survivalescaperooms.com/article/100638.htm的效果是一樣的,但是不再在OnTouchEvent中寫代碼,而是使用系統(tǒng)自帶的類GestureDetector來監(jiān)聽手勢以及滑動事件等等,它內(nèi)置了滑動,點(diǎn)擊,長按等事件,而且有快速滑動,比較方便,比自己寫的細(xì)節(jié)處理要好。

代碼:

package com.example.libingyuan.horizontallistview.ScrollViewGroup;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Scroller;/** * 自定義ViewGroup(橫向滾動) */public class ScrollViewGroup extends ViewGroup { //滾動計算輔助類 private Scroller mScroller; //屏幕寬度 private int screenWidth; //可以移動的最大距離 private int mMaxDistance; //自定義手勢監(jiān)聽類 private ScrollTouchLisener mTouchLisener; //手勢監(jiān)聽 private GestureDetector mDetector; /**  * 使用new關(guān)鍵字創(chuàng)建對象的時候調(diào)用  */ public ScrollViewGroup(Context context) {  this(context, null); } /**  * 在XML文件中使用的時候調(diào)用  */ public ScrollViewGroup(Context context, AttributeSet attrs) {  this(context, attrs, 0); } /**  * 在xml文件中調(diào)用,并且使用了自定義屬性的時候調(diào)用  */ public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr);  init(context); } /**  * 初始化方法  * 初始化滾動輔助類Scroller以及計算出屏幕寬度  */ private void init(Context context) {  //初始化輔助類  mScroller = new Scroller(context);  //獲取屏幕寬度  WindowManager manager = (WindowManager) context    .getSystemService(Context.WINDOW_SERVICE);  DisplayMetrics outMetrics = new DisplayMetrics();  manager.getDefaultDisplay().getMetrics(outMetrics);  screenWidth = outMetrics.widthPixels;  //手勢指示器初始化  mTouchLisener = new ScrollTouchLisener();  mDetector = new GestureDetector(context, mTouchLisener); } /**  * 滾動時需要重寫的方法,用于控制滾動  */ @Override public void computeScroll() {  //判斷滾動時候停止  if (mScroller.computeScrollOffset()) {   //滾動到指定的位置   scrollTo(mScroller.getCurrX(), mScroller.getCurrY());   //這句話必須寫,否則不能實時刷新   postInvalidate();  } } /**  * 手指觸屏事件監(jiān)聽  */ @Override public boolean onTouchEvent(MotionEvent event) {  mDetector.onTouchEvent(event);  if (event.getAction() == MotionEvent.ACTION_UP) {   this.onUp(event);  }  return true; } /*  *測量方法,測量父布局的寬度和高度  */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  //重新設(shè)置寬高  this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec)); } /**  * 測量寬度  */ private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {  // 寬度  int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);  int modeWidth = MeasureSpec.getMode(widthMeasureSpec);  //父控件的寬(wrap_content)  int width = 0;  int childCount = getChildCount();  //重新測量子view的寬度,以及最大高度  for (int i = 0; i < childCount; i++) {   View child = getChildAt(i);   measureChild(child, widthMeasureSpec, heightMeasureSpec);   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();   int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;   width += childWidth;  }  return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width; } /**  * 測量高度  */ private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {  //高度  int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);  int modeHeight = MeasureSpec.getMode(heightMeasureSpec);  //父控件的高(wrap_content)  int height = 0;  int childCount = getChildCount();  //重新測量子view的寬度,以及最大高度  for (int i = 0; i < childCount; i++) {   View child = getChildAt(i);   measureChild(child, widthMeasureSpec, heightMeasureSpec);   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();   int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;   height += childHeight;  }  height = height / childCount;  return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height; } /**  * 給子布局設(shè)定位置  */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) {  int childLeft = 0;//子View左邊的間距  int childWidth;//子View的寬度  int height = getHeight();//屏幕的寬度  int childCount = getChildCount();//子View的數(shù)量  for (int i = 0; i < childCount; i++) {   View child = getChildAt(i);   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();   childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;   child.layout(childLeft, 0, childLeft + childWidth, height);   childLeft += childWidth;  } } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) {  return new MarginLayoutParams(getContext(), attrs); } /*  *按下事件 ACTION_DOWN  */ public boolean onDown(MotionEvent e) {  //如果停止?jié)L動則取消動畫(即手指按下就停止?jié)L動)  if (!mScroller.isFinished()) {   mScroller.abortAnimation();  }  return false; } /* *抬起事件 ACTION_UP */ public boolean onUp(MotionEvent e) {  //得到最后一個子View  View lastChild = getChildAt(getChildCount() - 1);  //獲取滑動的最大滑動距離(最后一個Child的右邊框的坐標(biāo)減去屏幕的寬度)  int finalyChild = (int) (lastChild.getX() + lastChild.getWidth() - screenWidth);  mMaxDistance = finalyChild;  //如果滑動的距離小于第一個控件的最左邊(0)則回彈至(0,0)點(diǎn)  if (getScrollX() < 0) {   scrollTo(0, 0);  }  //如果滑動的距離大于最大可滑動距離則滑動到最后一個子View  if (getScrollX() >= finalyChild)   scrollTo(finalyChild, 0);  //刷新界面  invalidate();  return false; } /*  *ACTION_DOWN 、短按不移動  */ public void onShowPress(MotionEvent e) { } /*  *短按ACTION_DOWN、ACTION_UP  */ public boolean onSingleTapUp(MotionEvent e) {  return false; } /*  *ACTION_DOWN 、慢滑動  */ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  //滾動  scrollBy((int) distanceX, 0);  return false; } // ACTION_DOWN 、長按不滑動 public void onLongPress(MotionEvent e) { } /*  *ACTION_DOWN 、快滑動、 ACTION_UP  */ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  mScroller.fling(getScrollX(), 0, (int) -velocityX, 0, 0, mMaxDistance, 0, 0);  return false; } /**  * 自定義手勢監(jiān)聽類  */ private class ScrollTouchLisener implements GestureDetector.OnGestureListener {  //按下事件  @Override  public boolean onDown(MotionEvent e) {   return ScrollViewGroup.this.onDown(e);  }  //單擊事件  @Override  public void onShowPress(MotionEvent e) {   ScrollViewGroup.this.onShowPress(e);  }  //手指抬起事件  @Override  public boolean onSingleTapUp(MotionEvent e) {   return ScrollViewGroup.this.onSingleTapUp(e);  }  //滾動事件  @Override  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {   return ScrollViewGroup.this.onScroll(e1, e2, distanceX, distanceY);  }  //長按事件  @Override  public void onLongPress(MotionEvent e) {   ScrollViewGroup.this.onLongPress(e);  }  //滑動事件  @Override  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {   return ScrollViewGroup.this.onFling(e1, e2, velocityX, velocityY);  } }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 法库县| 宜丰县| 库尔勒市| 乐至县| 米林县| 巴彦县| 金阳县| 星子县| 澜沧| 资源县| 清涧县| 鄂托克旗| 西盟| 新泰市| 台安县| 石首市| 洪洞县| 娄烦县| 眉山市| 扶余县| 潼关县| 泰来县| 宁武县| 民权县| 韩城市| 射洪县| 独山县| 应用必备| 惠安县| 安泽县| 宁陕县| 唐河县| 丰都县| 新野县| 平凉市| 五常市| 清河县| 宜春市| 嘉禾县| 五常市| 邳州市|