一、抽屉的实现
1、原理
1.SlidingMenu继承HorizontalScrollView
2.activity的layout中,添加SlidingMenu,其下是LinearLayout,LinearLayout下是两个LinearLayout,分别代表menu和content
3.HorizontalScrollView本身就支持横向滑动,我们需要再onTouchEvent中判断,如果向右移动的距离大于menu界面的话,menu应该整个显示出来,否则不显示
2、代码实现
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xianbing.slidingmenudemo.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/menu"
android:layout_width="250dp"
android:layout_height="match_parent"
android:background="#ff0000"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="menu"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"
/>
</LinearLayout>
</LinearLayout>
</com.xianbing.slidingmenudemo.SlidingMenu>
SlidingMenu.java
public class SlidingMenu extends HorizontalScrollView {
private boolean runOnce = true;
private LinearLayout mMenu;
private LinearLayout mContent;
private int mWindowWidth;
private int mMenuMeasureWidth;
public SlidingMenu(Context context) {
super(context);
init();
}
public SlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
mWindowWidth = displayMetrics.widthPixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
if (runOnce) {
runOnce = false;
LinearLayout linearLayout = (LinearLayout)getChildAt(0);
mMenu = (LinearLayout)linearLayout.getChildAt(0);
mContent = (LinearLayout)linearLayout.getChildAt(1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams )mContent.getLayoutParams();
params.width = mWindowWidth;
mContent.setLayoutParams(params);
mMenuMeasureWidth = mMenu.getMeasuredWidth();
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
ViewTreeObserver viewTreeObserver = getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scrollTo(mMenuMeasureWidth, 0);
}
});
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
if (ev.getAction() == MotionEvent.ACTION_UP) {
int scrollX = getScrollX();
if (scrollX < mMenuMeasureWidth / 2) {
smoothScrollTo(0, 0);
}
else {
smoothScrollTo(mMenuMeasureWidth, 0);
}
}
return true;
}
}
二、下拉刷新
1、原理
- Padding是什么,可不可以是负值
- 一个view 的padding的值设置为负值是什么效果
- 什么是FrameLayout,用于什么情况
2、实现过程
1.新建RefreshLayout,继承于FrameLayout
2.在RefreshLayout中动态添加Header
3.Header的padding初始是负值,这个值应该和Header的高度是一致的
4.onInterceptTouchEvent中,如果已经是ListView中的第一个数据,并且,Y轴方向上已经有距离发生,开始拦截事件
5.拦截事件后,在onTouchEvent中的ACTION_MOVE,动态修改Padding值,然后用这个值来修改Header的Top Padding
6.在onTouchEvent中,在ACTION_UP中,如果已经滑动超过半个Header的高度值,就显示整个Header View,通知外界,开始loading。用Handler PostDelay一个Runnable,延迟事件设置为2秒,在Runnable中,重新将Header View的padding设置为负值(隐藏起来),通知外界,loading结束。
3、代码
activity_main,主View
<?xml version="1.0" encoding="utf-8"?>
<com.xianbing.demo23.RefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.xianbing.demo23.RefreshLayout>
refresh_layout,下拉的View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_green_light">
<TextView
android:id="@+id/textTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="加载中"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
item_list_layout,ListView中的数据的layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
RefreshLayout.java
public class RefreshLayout extends FrameLayout {
private static final int HEADER_HEIGHT = 200;
private LinearLayout mHeaderParent;//下拉的header
private ProgressBar progressBar;//下拉的header中的ProgressBar
private TextView textTv;//下拉的header中的TextView
private int paddingTop;//Header的top padding
private int mDownYPos;//ACTION_DOWN事件的时候记录的值,用于判断下拉的距离
private ListView listView;
private int firstVisiblePosition;
private IRefreshListener mRefreshListener;//回调
public RefreshLayout(@NonNull Context context) {
super(context);
}
public RefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initHeaderView();
}
public RefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initHeaderView() {
//下拉显示的头布局的根布局是LinearLayout
mHeaderParent = new LinearLayout(getContext());
mHeaderParent.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mHeaderParent.setOrientation(LinearLayout.VERTICAL);
addView(mHeaderParent);
View headerView = LayoutInflater.from(getContext()).inflate(R.layout.refresh_layout, null);
progressBar = headerView.findViewById(R.id.progressBar);
textTv = headerView.findViewById(R.id.textTv);
mHeaderParent.addView(headerView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, HEADER_HEIGHT));
paddingTop = -HEADER_HEIGHT;//padding是负的,到屏幕外面
mHeaderParent.setPadding(0, paddingTop, 0, 0);//将加载的UI默认隐藏到顶部,不显示出来
}
//布局完成
@Override
protected void onFinishInflate() {
super.onFinishInflate();
listView = (ListView) getChildAt(1);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.i("Simon", "onScroll firstVisiblePosition: " + firstVisiblePosition);
firstVisiblePosition = firstVisibleItem;
if (firstVisiblePosition == 0) {//已经是第一个元素了
Log.i("Simon", "已经是第一个元素了");
}
}
});
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("Simon", "onInterceptTouchEvent ACTION_DOWN");
mDownYPos = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
Log.i("Simon", "onInterceptTouchEvent ACTION_MOVE oldY: " + mDownYPos + " y:" + ev.getY());
if (ev.getY() > mDownYPos && firstVisiblePosition == 0) {
Log.i("Simon", "onInterceptTouchEvent ACTION_MOVE true");
return true;//如果等于0就拦截事件,显示下拉刷新的headerview
} else {
Log.i("Simon", "onInterceptTouchEvent ACTION_MOVE false");
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("Simon", "onTouchEvent ACTION_DOWN");
mDownYPos = (int) event.getY();//记录当前点击的位置在y轴的坐标
break;
case MotionEvent.ACTION_MOVE:
Log.i("Simon", "onTouchEvent ACTION_MOVE");
//确保手指是往下滑动,并且滑动距离不超过headerview的高度,如果超过就不用处理了
Log.i("Simon", "onTouchEvent ACTION_MOVE event.getY(): " + event.getY());
Log.i("Simon", "onTouchEvent ACTION_MOVE lastY: " + mDownYPos);
if (event.getY() > mDownYPos && event.getY() - mDownYPos <= HEADER_HEIGHT) {
float diffY = event.getY() - mDownYPos;
Log.i("Simon", "onTouchEvent ACTION_MOVE diffY: " + diffY);
float currentTop = paddingTop + diffY;
Log.i("Simon", "onTouchEvent ACTION_MOVE currentTop: " + currentTop);
mHeaderParent.setPadding(0, (int) currentTop, 0, 0);//改变头部的top的值,逐渐让它显示出来
}
break;
case MotionEvent.ACTION_UP:
Log.i("Simon", "onTouchEvent ACTION_UP event.getY():" + event.getY() + " lastY: " + mDownYPos + " headerViewHeight/2: " + HEADER_HEIGHT / 2);
if (event.getY() - mDownYPos >= HEADER_HEIGHT / 2) {//当你滑动的距离超过了headerview高度的一半,就会将headerView显示出来
mHeaderParent.setPadding(0, 0, 0, 0);
progressBar.setVisibility(VISIBLE);
textTv.setVisibility(VISIBLE);
if (mRefreshListener != null) {
mRefreshListener.onRefresh();
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//两秒钟之后再次隐藏
mHeaderParent.setPadding(0, paddingTop, 0, 0);
progressBar.setVisibility(GONE);
textTv.setVisibility(GONE);
if (mRefreshListener != null) {
mRefreshListener.onRefreshComplete();
}
}
}, 2000);
} else {
mHeaderParent.setPadding(0, paddingTop, 0, 0);
}
break;
}
return true;//消费事件
}
public void addRefreshListener(IRefreshListener iRefreshListener) {
this.mRefreshListener = iRefreshListener;
}
public interface IRefreshListener {
void onRefresh();
void onRefreshComplete();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements RefreshLayout.IRefreshListener {
private ListAdapter listAdapter;
private List<String> mDataList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
refreshLayout.addRefreshListener(this);
initListView();
}
private void initListView() {
ListView listView = findViewById(R.id.listView);
listAdapter = new ListAdapter();
listView.setAdapter(listAdapter);
for (int i = 0; i < 10; i++) {
mDataList.add(i + "");
}
listAdapter.setData(mDataList);
}
@Override
public void onRefresh() {
Toast.makeText(this, "启动网络请求,加载数据", Toast.LENGTH_SHORT).show();
Random ran = new Random();
for (int i = 0; i < 10; i++) {
int v = ran.nextInt();
mDataList.add(0, v + "");
}
listAdapter.setData(mDataList);
}
@Override
public void onRefreshComplete() {
}
private class ListAdapter extends BaseAdapter {
private List<String> mDataList = new ArrayList<>();
public ListAdapter() {
}
public void setData(List<String> list) {
if (list != null) {
mDataList.clear();
mDataList.addAll(list);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public Object getItem(int position) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_list_layout, parent, false);
TextView textView = view.findViewById(R.id.text_view);
textView.setText(mDataList.get(position));
return view;
}
}
}