这里用到的是第三方插件:SmartRefreshLayout
效果图如下:
使用步骤如下:
1、添加远程依赖
/*刷新和加载*/
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'//没有使用特殊Header,可以不加这行
2、如何在布局文件中使用,代码如下:
(备注:SmartRefreshLayout分为三块:Header布局,Content布局,Footer布局。其中,Content内容布局必须是一个整体。例如,下面的布局包括图片,文字,列表等等,用一个ScrollView包起来。)
3、布局文件知道怎么用了,下面说一下如何在Activity中使用,代码如下:
其实分为以下几步即可:
(1) 实现OnRefreshListener和OnLoadMoreListener接口方法。(刷新和加载)
(2) 给smartRefreshLayout添加监听事件。
(3) 调用finishRefresh()以及finishLoadMore()结束刷新和加载过程动画。
package com.deepreality.smartrefreshlayoutdemo;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
private Context mContext;
private SmartRefreshLayout smartRefreshLayout;
private TextView tvRefreshInfo;
private ListViewNesting lvNewsList;
private ScrollView scrollView;
private List tbOrganizationList;
private List tempTbOrganizationList;
private Tb_Organization tb_organization;
private OrganizationListAdapter organizationListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseDataInit();
bindViews();
viewsAddListener();
viewsDataInit();
scrollView.smoothScrollTo(0, 0);
}
private void baseDataInit() {
mContext = this;
tb_organization = null;
tbOrganizationList = new ArrayList<>();
tempTbOrganizationList = new ArrayList<>();
}
private void bindViews() {
smartRefreshLayout = findViewById(R.id.Main_SRLayout);
tvRefreshInfo = findViewById(R.id.Main_tvRefreshInfo);
lvNewsList = findViewById(R.id.Main_lvNewsList);
scrollView = findViewById(R.id.Main_scrollView);
}
private void viewsAddListener() {
smartRefreshLayout.setOnRefreshListener(this);
smartRefreshLayout.setOnLoadMoreListener(this);
}
private void viewsDataInit() {
newsListDataRefresh();
}
private void newsListDataRefresh() {
tbOrganizationList.clear();
for (int i = 0; i < 10; i ++) {
tb_organization = new Tb_Organization();
tbOrganizationList.add(tb_organization);
}
organizationListAdapter = new OrganizationListAdapter(mContext, tbOrganizationList);
lvNewsList.setAdapter(organizationListAdapter);
}
private void newsListDataLoadMore() {
for (int i = 0; i < 10; i ++) {
tb_organization = new Tb_Organization();
tbOrganizationList.add(tb_organization);
}
organizationListAdapter.notifyDataSetChanged();
}
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
newsListDataLoadMore();
smartRefreshLayout.finishLoadMore();
Toast.makeText(mContext, "没有更多数据了!", Toast.LENGTH_SHORT).show();
}
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
newsListDataRefresh();
smartRefreshLayout.finishRefresh();
Toast.makeText(mContext, "刷新完成!", Toast.LENGTH_SHORT).show();
}
}
public class MyCustomRefreshHeader extends LinearLayout implements RefreshHeader { private TextView mHeaderText;//标题文本 private PathsView mArrowView;//下拉箭头 private ImageView mProgressView;//刷新动画视图 private ProgressDrawable mProgressDrawable;//刷新动画 public MyCustomRefreshHeader(Context context) { super(context); initView(context); } public MyCustomRefreshHeader(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); } public MyCustomRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { setGravity(Gravity.CENTER); mHeaderText = new TextView(context); mProgressDrawable = new ProgressDrawable(); mArrowView = new PathsView(context); mProgressView = new ImageView(context); mProgressView.setImageDrawable(mProgressDrawable); mArrowView.parserPaths("M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"); addView(mProgressView, SmartUtil.dp2px(20), SmartUtil.dp2px(20)); addView(mArrowView, SmartUtil.dp2px(20), SmartUtil.dp2px(20)); addView(new View(context), SmartUtil.dp2px(20), SmartUtil.dp2px(20)); addView(mHeaderText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); setMinimumHeight(SmartUtil.dp2px(60)); } @NonNull @Override public View getView() { return this; } @NonNull @Override public SpinnerStyle getSpinnerStyle() { return SpinnerStyle.Translate; } @Override public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) { mProgressDrawable.start();//开始动画 } @Override public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) { mProgressDrawable.stop();//停止动画 if (success){ mHeaderText.setText("刷新完成"); } else { mHeaderText.setText("刷新失败"); } return 500;//延迟500毫秒之后再弹回 } @Override public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) { switch (newState) { case None: case PullDownToRefresh: mHeaderText.setText("下拉开始刷新"); mArrowView.setVisibility(VISIBLE);//显示下拉箭头 mProgressView.setVisibility(GONE);//隐藏动画 mArrowView.animate().rotation(0);//还原箭头方向 break; case Refreshing: mHeaderText.setText("正在刷新"); mProgressView.setVisibility(VISIBLE);//显示加载动画 mArrowView.setVisibility(GONE);//隐藏箭头 break; case ReleaseToRefresh: mHeaderText.setText("释放立即刷新"); mArrowView.animate().rotation(180);//显示箭头改为朝上 break; } } @Override public void setPrimaryColors(int... colors) { } @Override public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) { } @Override public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) { } @Override public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) { } @Override public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) { } @Override public boolean isSupportHorizontalDrag() { return false; } }