Android中⽤SmartRefreshLayout实现ListView列表的数据刷新与。。。这⾥⽤到的是第三⽅插件: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包起来。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="schemas.android/apk/res-auto"
android:orientation="vertical"
xmlns:android="schemas.android/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="@color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题栏"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@ id/Main_SRLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444444"
app:srlPrimaryColor="#444444"
app:srlAccentColor="@android:color/white"
app:srlEnablePreviewInEditMode="true">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="@ id/Main_scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorWhite">
<TextView
android:id="@ id/Main_tvRefreshInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="你好"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="@mipmap/corporation_detailinfo_topbg"/>
<com.deepreality.smartrefreshlayoutdemo.ListViewNesting
android:id="@ id/Main_lvNewsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.deepreality.smartrefreshlayoutdemo.ListViewNesting>            </LinearLayout>
</ScrollView>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlAccentColor="@color/colorWhite"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
3、布局⽂件知道怎么⽤了,下⾯说⼀下如何在Activity中使⽤,代码如下:
其实分为以下⼏步即可:
(1) 实现OnRefreshListener和OnLoadMoreListener接⼝⽅法。(刷新和加载)
(2) 给smartRefreshLayout添加监听事件。
(3) 调⽤finishRefresh()以及finishLoadMore()结束刷新和加载过程动画。
package com.deepreality.smartrefreshlayoutdemo;
t.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<Tb_Organization> tbOrganizationList;
private List<Tb_Organization> tempTbOrganizationList;
private Tb_Organization tb_organization;
private OrganizationListAdapter organizationListAdapter;
@Override
protected void onCreate(Bundle 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);
}
}
@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();
android layout布局Toast.makeText(mContext, "刷新完成!", Toast.LENGTH_SHORT).show();
}
}
来源:www.icode9/content-4-28901.html