Android编程实现的首页左右滑动切换功能示例

发布时间:2020-09-14 16:28:22 作者:luck_apple
来源:脚本之家 阅读:276

本文实例讲述了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程序设计有所帮助。

推荐阅读:
  1. 微信小程序中如何实现左右滑动切换页面功能
  2. Android开发如何实现高仿优酷的客户端图片左右滑动切换功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 滑动 切换

上一篇:golang实现跨域访问的方法

下一篇:判断“命令按钮”是否被鼠标单击详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》