代码库/自动显示/隐藏布局的具有弹性的ListView

1.给ListView增加一个HeaderView,避免第一个Item被Toolbar遮挡

    Tips:

getResources().getDimension(R.dimen.abc_action_bar_default_height_material)

获取系统ActionBar高度(即Toolbar高度)

2.获取最低滑动距离

    

touchSlop = ViewConfiguration.get(this).getScaledTouchSlop();

3.判断滑动事件+重写监听

    ObjectAnimator类.ofFloat->构建并返回一个objectanimator动画之间的浮点值

    第一个参数:动画的对象/第二个参数:动画名称/第三个参数:初始值->结束值

4.建立弹性ListView

    ListView源码中的控制滑动到边缘的处理方法

    overScrollBy中的maxOverScrollX/Y参数

    因为不同的density,需要在构造函数中初始化并获取此对象

overScrollView

        

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ListView;

import java.util.List;

/**
 * Created by YRC on 2017/10/15.
 */

public class OverScrollView  extends ListView{
    private static int mMaxOverScrollY=200;
    private Context mContext;
    public OverScrollView(Context context) {
        super(context);
        mContext=context;
        initView();
    }

    public OverScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        initView();
    }

    public OverScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        initView();
    }

    private void initView() {
        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        float density = metrics.density;
        mMaxOverScrollY = (int) (density * mMaxOverScrollY);
    }
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxOverScrollY, isTouchEvent);
    }


}

MainAc:

public class MainActivity extends AppCompatActivity {

private int touchSlop;
    Toolbar mToolbar;
    private String[] mStr = new String[20];
    ListView listView;
    private int mFirstY, mCurrentY, dis;
    private boolean mShow = true;
    private Animator animator;


    private ListView mListView;
    List data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        touchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        listView = (ListView) findViewById(R.id.listview);
        for (int i = 0; i < mStr.length; i++) {
            mStr[i] = "item" + i;
        }
        View header = new View(this);
        header.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.MATCH_PARENT,
                (int) getResources().getDimension(R.dimen.abc_action_bar_default_height_material)));
        listView.addHeaderView(header);
        ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
                android.R.layout.simple_expandable_list_item_1, mStr);
        listView.setAdapter(adapter);
        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mFirstY = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mCurrentY = (int) event.getY();
                        if (mFirstY - mCurrentY > touchSlop) {
                            dis = 0; //向下滑
                        } else if (mCurrentY - mFirstY > touchSlop) {
                            dis = 1;  //向上滑
                        }
                        if (dis == 0) {
                            if (mShow) {
                                toolbarAnim(0);
                                mShow = !mShow;
                            }
                        } else {
                            if (!mShow) {
                                toolbarAnim(1);
                                mShow = !mShow;
                            }
                        }
                }
                return false;
            }
        });
    }

    private void toolbarAnim(int flag) {
        if (animator != null && animator.isRunning()) {
            animator.cancel();
        }
        if (flag == 0) {
            animator = ObjectAnimator.ofFloat(mToolbar, "translationY", mToolbar.getTranslationY(), -mToolbar.getHeight());

        } else if (flag == 1) {
            animator = ObjectAnimator.ofFloat(mToolbar, "translationY", mToolbar.getTranslationY(), 0);
        }
        animator.start();
    }
}

你可能感兴趣的:(代码库)