安卓弹性View

来源:互联网 发布:蜂群算法 编辑:程序博客网 时间:2024/05/17 11:07
当前的安卓上拉加载下拉刷新框架都跟自己想的不太一样,于是决定自己打造一个提升下博客的热度,毕竟公司刚刚裁员了。下面是我的准备工作拆分:1.弹性容器2.头布局和尾布局

本篇就完成目标1所标识内容,由于读者大部分是跟我水平差不多或寥寥几个高于我的,所以本篇就只上代码即可,看不懂可以评论区加QQ,,,,,,,,,

此容器可以为任何view添加弹性滑动

/**
* Created by biyunlong on 2017/4/8.
*/

public class PullToRefresh extends LinearLayout {

// 滚动时间private static final long ANIM_TIME = 400;//原始位置private int startL, startR, startT, startB;//恢复位置阶段private boolean isMoved = false;//阻尼private static final float zuni = 0.8f;//Viewprivate View view;private float oldY;public PullToRefresh(Context context) {    super(context);}public PullToRefresh(Context context, AttributeSet attrs) {    super(context, attrs);}public PullToRefresh(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    //关闭右侧滚动条    this.setVerticalScrollBarEnabled(false);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {    super.onLayout(changed, l, t, r, b);    if (getChildCount() == 1) {        view = getChildAt(0);        startL = view.getLeft();        startR = view.getRight();        startT = view.getTop();        startB = view.getBottom();    } else if (getChildCount() > 1) {        throw new RuntimeException("只能有一个子View");    } else if (getChildCount() < 1) {        throw new RuntimeException("没有子View");    }}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {    if (view == null) {        return super.dispatchTouchEvent(ev);    }    boolean isTouchOutOfScrollView = ev.getY() >= startB || ev.getY() <= startT; //如果当前view的Y上的位置    if (isTouchOutOfScrollView) {//如果不在view的范围内        if (isMoved) {//当前容器已经被移动            recoverContainer();        }        return true;    }    switch (ev.getAction()) {        case MotionEvent.ACTION_DOWN:            //记录按下时的Y            oldY = ev.getY();        case MotionEvent.ACTION_MOVE:            float newY = ev.getY();            int scrollY = (int) (newY - oldY);            if ((downLicense() && scrollY > 0) || (upLicense() && scrollY < 0)) {                int offset = (int) (scrollY * zuni);                view.layout(startL, startT + offset, startR, startB + offset);                isMoved = true;                return true;            } else {                oldY = newY;                isMoved = false;                recoverContainer();                return super.dispatchTouchEvent(ev);            }        case MotionEvent.ACTION_UP:            if (isMoved) {                recoverContainer();                return true;            }    }    return super.dispatchTouchEvent(ev);}/** * 位置还原 */private void recoverContainer() {    if (!isMoved) {        return;    }    TranslateAnimation anims = new TranslateAnimation(0, 0, view.getTop(), startT);    anims.setDuration(ANIM_TIME);    view.startAnimation(anims);    view.layout(startL, startT, startR, startB);    isMoved = false;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {    return false;}public boolean downLicense() {    if (view instanceof RecyclerView) {        RecyclerView rv = (RecyclerView) view;        RecyclerView.Adapter adapter = rv.getAdapter();        int firstVisibleItemCount = ((LinearLayoutManager) (rv.getLayoutManager())).findFirstVisibleItemPosition();        if (firstVisibleItemCount == 0 && adapter.getItemCount() > 0) {            return true;        } else {            return false;        }    }    return true;}public boolean upLicense() {    if (view instanceof RecyclerView) {        RecyclerView rv = (RecyclerView) view;        RecyclerView.Adapter adapter = rv.getAdapter();        int pastVisiblesItems = rv.getLayoutManager().getChildCount();        //总条数        int totalItemCount = rv.getLayoutManager().getItemCount();        //可见条数        int firstVisibleItemCount = ((LinearLayoutManager) (rv.getLayoutManager())).findFirstVisibleItemPosition();        if (firstVisibleItemCount + pastVisiblesItems >= totalItemCount && adapter.getItemCount() > 0) {            return true;        } else {            return false;        }    }    return true;}

}

1 0