ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android中用SmartRefreshLayout实现ListView列表的数据刷新与加载更多(总结)

2018-09-29 10:31:57  阅读:340  来源: 互联网

标签:


这里用到的是第三方插件: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, onl oadMoreListener {

    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 onl oadMore(@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();
    }
}

标签:
来源: https://blog.csdn.net/lpCrazyBoy/article/details/82893607

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有