91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android編程實現的首頁左右滑動切換功能示例

發布時間:2020-09-14 16:28:22 來源:腳本之家 閱讀:273 作者:luck_apple 欄目:移動開發

本文實例講述了Android編程實現的首頁左右滑動切換功能。分享給大家供大家參考,具體如下:

很多軟件會選擇左右滑動的主界面,實現方式也很多,這里的僅供參考,勿噴。

不多說什么了,相信大家看看代碼就明白,自己也不善言辭,望大家諒解。

自定義接口,監聽滑動翻頁事件:

/** 滑動后翻頁事件 */
public interface OnViewChangedListener {
  public void OnViewChanged(int viewId);
}

滑動翻頁view(滑動翻頁不是很靈敏):

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.Scroller;
public class ScrollerView extends FrameLayout {
  private Scroller scroller;
  private Drawable drawable;
  private OnViewChangedListener listener;
  public ScrollerView(Context context) {
    this(context, null, 0);
  }
  public ScrollerView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public ScrollerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    scroller = new Scroller(context);
  }
  private PointF last = new PointF();
  private final int TOUCH_SLOP = ViewConfiguration.get(getContext())
      .getScaledTouchSlop();
  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
    final int x = (int) event.getX();
    boolean flag = false;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      last.x = x;
      break;
    case MotionEvent.ACTION_MOVE:
      final int deltaX = (int) (last.x - x);
      if (Math.abs(deltaX) > TOUCH_SLOP) {
        flag = true;
      }
      break;
    case MotionEvent.ACTION_UP:
      break;
    }
    return flag;
  }
  public boolean onTouchEvent(MotionEvent event) {
    final int x = (int) event.getX();
    final int width = getWidth();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      if (!scroller.isFinished()) {
        scroller.abortAnimation();
      }
      break;
    case MotionEvent.ACTION_MOVE:
      final int deltaX = (int) (last.x - x);
      if (Math.abs(deltaX) < TOUCH_SLOP) {
        break;
      }
      last.x = x;
      if (deltaX < 0) {
        if (getScrollX() > 0) {
          scrollBy(Math.max(-getScrollX(), deltaX), 0);
        }
      } else if (deltaX > 0) {
        final int availableToScroll = getChildAt(getChildCount() - 1)
            .getRight() - getScrollX() - getWidth();
        if (availableToScroll > 0) {
          scrollBy(Math.min(availableToScroll, deltaX), 0);
        }
      }
      break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
      final OnViewChangedListener changedListener = listener;
      int dx = (getScrollX() + width / 2) / width;
      if (dx < 0) {
        dx = 0;
      }
      if (dx > getChildCount() - 1) {
        dx = getChildCount() - 1;
      }
      changedListener.OnViewChanged(dx);
      dx *= width;
      dx -= getScrollX();
      scroller.startScroll(getScrollX(), 0, dx, 0, Math.abs(dx) * 3);
      break;
    }
    invalidate();
    return true;
  }
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int width = getWidth();
    final int count = getChildCount();
    int height = getHeight();
    int childLeft = 0;
    for (int i = 0; i < count; i++) {
      final View child = getChildAt(i);
      child.layout(childLeft, 0, childLeft + width, height);
      childLeft += width;
    }
  }
  @Override
  public void setBackgroundDrawable(Drawable d) {
    super.setBackgroundDrawable(drawable);
    drawable = d;
    super.setBackgroundDrawable(null);
  }
  @Override
  protected void dispatchDraw(Canvas canvas) {
    if (null != drawable) {
      drawable.setBounds(0, 0, getChildCount() * getWidth(), getHeight());
      drawable.draw(canvas);
    }
    super.dispatchDraw(canvas);
  }
  @Override
  public void computeScroll() {
    if (scroller.computeScrollOffset()) {
      scrollTo(scroller.getCurrX(), scroller.getCurrY());
      invalidate();
    }
  }
  /** 設置滑動后翻頁事件監聽 */
  public void setOnViewChangedListener(OnViewChangedListener listener) {
    this.listener = listener;
  }
}

主Activity:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnViewChangedListener {
  private ScrollerView container;
  private View view_1, view_2;
  private ImageView imgLeft, imgRight;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    initView();
  }
  private void initView() {
    LayoutInflater inflater = LayoutInflater.from(this);
    container = (ScrollerView) findViewById(R.id.main_container);
    container.setOnViewChangedListener(this);
    imgLeft = (ImageView) findViewById(R.id.img_left);
    imgRight = (ImageView) findViewById(R.id.img_right);
    view_1 = inflater.inflate(R.layout.layout_view_1, null);
    view_2 = inflater.inflate(R.layout.layout_view_2, null);
    container.addView(view_1);
    container.addView(view_2);
  }
  @Override
  public void OnViewChanged(int viewId) {
    switch (viewId) {
    case 0:
      imgLeft.setImageResource(R.drawable.main_icon_check);
      imgRight.setImageResource(R.drawable.main_icon_normal);
      break;
    case 1:
      imgLeft.setImageResource(R.drawable.main_icon_normal);
      imgRight.setImageResource(R.drawable.main_icon_check);
      break;
    }
  }
}

附:完整實例代碼點擊此處本站下載

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android布局layout技巧總結》、《Android視圖View技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

山西省| 正蓝旗| 轮台县| 富民县| 古交市| 迭部县| 金阳县| 嘉鱼县| 辽中县| 岳池县| 富锦市| 麦盖提县| 沂南县| 页游| 乌兰浩特市| 石渠县| 桦川县| 江安县| 鹤岗市| 弥渡县| 忻州市| 肇庆市| 西吉县| 扎囊县| 肥东县| 保靖县| 江门市| 荆门市| 达州市| 梁山县| 郴州市| 林口县| 黄大仙区| 双江| 宜阳县| 霍山县| 改则县| 尉氏县| 江城| 高邮市| 大理市|