Android自定义自定义listview布局实现上拉加载下拉刷新功能布局实现上拉加载下拉刷新功能
主要介绍了Android自定义listview布局实现上拉加载下拉刷新功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
listview实现上拉加载以及下拉刷新的方式有很多。下面是我写的一种自定义的布局,复用性也比较的强。首先就是继承的listview的自定义view。
AutoListView.Java:
package com.example.mic.testdemo.view;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.mic.testdemo.R;
import java.text.SimpleDateFormat;
/**
* Created by DFLENOVO on 2016/8/3.
*/
public class AutoListView extends ListView implements AbsListView.OnScrollListener {
public static final String FOOTER = "FOOTER";
public static final String HEADER = "HEADER";
private static final int PULL = 1;
private static final int NONE = 0;
// private static final int RELEASE = 2;
private static final int REFRESHING = 2;
private static final int SPACE = 20;
private static final String TAG = "AutoListView";
private RotateAnimation downAnimation;
private RotateAnimation upAnimation;
private Context context;
private LayoutInflater inflater;
private View footer;
private View header;
private int headerContentInitialHeight;
private int headerContentHeight;
private OnRefreshListener onRefreshListener;
private boolean loadEnable;
private OnLoadListener onLoadListener;
private int firstVisibleItem;
private boolean isLoadingMore = false;
private int startY;
private int state;
private ImageView iv_pull;
private int footerViewHeight;
private ProgressBar mProgressBar;
private TextView tvState;
private TextView tvLastUpdateTime;
private boolean isScrollToBottom;
public AutoListView(Context context) {
super(context);
initView(context);
}
public AutoListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public AutoListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AutoListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView(context);
}
private void initView(Context context) {
initHeaderView();
initFooterView();
this.setOnScrollListener(this);
//
}
private void initHeaderView() {
header = View.inflate(getContext(), R.layout.pull_to_refresh_header, null);
iv_pull = (ImageView) header
.findViewById(R.id.iv_pull);
mProgressBar = (ProgressBar) header
.findViewById(R.id.pb_listview_header);
tvState = (TextView) header
.findViewById(R.id.tv_listview_header_state);
tvLastUpdateTime = (TextView) header
.findViewById(R.id.tv_listview_header_last_update_time);
// 设置最后刷新时间
tvLastUpdateTime.setText("最后刷新时间: " + getLastUpdateTime());
header.measure(0, 0); // 系统会帮我们测量出headerView的高度
headerContentHeight = header.getMeasuredHeight();
header.setPadding(0, -headerContentHeight, 0, 0);
this.addHeaderView(header,HEADER,true); // 向ListView的顶部添加一个view对象
initAnimation();
}
private void initAnimation() {
upAnimation = new RotateAnimation(0f, -180f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
upAnimation.setDuration(500);
upAnimation.setFillAfter(true); // 动画结束后, 停留在结束的位置上
downAnimation = new RotateAnimation(-180f, -360f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
downAnimation.setDuration(500);
downAnimation.setFillAfter(true); // 动画结束后, 停留在结束的位置上
}
private String getLastUpdateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");