自定义ListView下拉刷新上拉加载更多( 二 )


//属性-开始刷新状态private final int PULL_REFRESH_STATE = 0;private final int PULL_REFRESH_RELEASE = 1;//释放刷新private final int PULL_REFRESHING = 2;//正在刷新private int pull_current_state = PULL_REFRESH_STATE;//当前状态public void updateHeaderState() {switch (pull_current_state) {//开始case PULL_REFRESH_STATE:header_text.setText("开始刷新");viewHeader.setPadding(0, -heightHeader, 0, 0);break;//释放case PULL_REFRESH_RELEASE:header_text.setText("释放刷新");break;//正在case PULL_REFRESHING:header_text.setText("刷新中.......");viewHeader.setPadding(0, 0, 0, 0);break;}}我们只需要判断是否处于刷新中,如果不是,则计算是不是第一个item可见并且滑动距离大于0,证明手指滑动了 。然后不同滑出高度显示不同的文字即可 。
@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:downY = (int) event.getY();break;case MotionEvent.ACTION_MOVE:moveY = (int) event.getY();diffY = moveY - downY;//滑动的距离paddY = - heightHeader + diffY / 3;//如果是正在刷新中不做任何操作if (pull_current_state == PULL_REFRESHING) {return false;}//下拉//第一条,并且滑动距离大于0if (getFirstVisiblePosition() == 0 && diffY > 0) {if (paddY > 0 && pull_current_state != PULL_REFRESH_RELEASE) {//释放刷新pull_current_state = PULL_REFRESH_RELEASE;updateHeaderState();} else if (paddY < 0 && pull_current_state != PULL_REFRESH_STATE) {pull_current_state = PULL_REFRESH_STATE;header_text.setText("开始刷新");updateHeaderState();}viewHeader.setPadding(0, paddY, 0, 0);return true;}break;case MotionEvent.ACTION_UP:if (pull_current_state == PULL_REFRESH_STATE) {updateHeaderState();} else if (pull_current_state == PULL_REFRESH_RELEASE) {pull_current_state = PULL_REFRESHING;updateHeaderState();}break;}return false;}1.3、接口回调我们需要状态根据业务来动态调整,在可以刷新的时候做一些逻辑处理 , 同时处理完了,调整状态 。
public void setPullDownFinish() {pull_current_state = PULL_REFRESH_STATE;viewHeader.setPadding(0, -heightHeader, 0, 0);}public IPullDownRefreshService iPullDownRefreshService;public interface IPullDownRefreshService {void onPullDownRefresh();//下拉刷新void onLoadMore();//上拉加载更多刷新}public void setOnRefreshListener(IPullDownRefreshService iPullDownRefreshService) {this.iPullDownRefreshService = iPullDownRefreshService;}准备工作做好后 , 我们在更新状态的地方调用
public void updateHeaderState() {switch (pull_current_state) {//开始case PULL_REFRESH_STATE:break;//释放case PULL_REFRESH_RELEASE:break;//正在case PULL_REFRESHING:header_text.setText("刷新中.......");viewHeader.setPadding(0, 0, 0, 0);if (iPullDownRefreshService != null) {iPullDownRefreshService.onPullDownRefresh();}break;}}Activity中使用,来一个3秒刷新完成
mBinding.listview.setOnRefreshListener(new MyListview.IPullDownRefreshService() {@Overridepublic void onPullDownRefresh() {refreshSuccess();Toast.makeText(ActivityRefresh.this, "下拉-加载中.....", Toast.LENGTH_SHORT).show();}@Overridepublic void onLoadMore() {Toast.makeText(ActivityRefresh.this, "more-加载中.....", Toast.LENGTH_SHORT).show();}});CountDownTimer countDownTimer;public void refreshSuccess() {if (countDownTimer == null) {countDownTimer = new CountDownTimer(3000, 1000) {@Overridepublic void onTick(long millisUntilFinished) {}@Overridepublic void onFinish() {mBinding.listview.setPullDownFinish();countDownTimer.cancel();countDownTimer = null;}}.start();}}到这下拉刷新就结束了 。
上拉加载更多也是如此
2、上拉加载更多2.1、底部样式<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"tools:ignore="MissingDefaultResource"><ProgressBarandroid:id="@+id/footer_prggress"android:layout_width="20dp"android:layout_height="match_parent"style="?android:attr/progressBarStyle"android:layout_marginEnd="10dp"android:indeterminateTint="#E8AD56"/><TextViewandroid:id="@+id/footer_text"android:layout_width="wrap_content"android:layout_height="70dp"android:text="正在加载更多"android:gravity="center"android:textColor="#E8AD56"/></LinearLayout>

自定义ListView下拉刷新上拉加载更多

文章插图
2.2、布局添加public void init(Context context) {viewFooter = View.inflate(context, R.layout.view_footer, null);viewFooter.measure(0, 0);footer_text = viewFooter.findViewById(R.id.footer_text);footer_prggress = viewFooter.findViewById(R.id.footer_prggress);heightFooter = viewFooter.getMeasuredHeight();viewFooter.setPadding(0, -heightFooter, 0, 0);this.addFooterView(viewFooter);}

推荐阅读