打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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="http://schemas.android.com/apk/res-auto"    android:orientation="vertical"    xmlns:android="http://schemas.android.com/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;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<Tb_Organization> tbOrganizationList;    private List<Tb_Organization> 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();    }}
来源:http://www.icode9.com/content-4-28901.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
android 滚动视图(ScrollView)学习与应用
Android之ScrollView设置了高度(android:layout_height=“match_parent“)但里面的组件不能充满问题
Android 软键盘盖住输入框的问题
Android 设置TextView滑动滚动条和滑动效果
android中关于scrollview内部组件android:layout
android实现TextView垂直或水平滚动 | 萝卜白菜的小站
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服