美文网首页
分页加载.listview.XRecyclerView.Smar

分页加载.listview.XRecyclerView.Smar

作者: 君袅 | 来源:发表于2019-02-21 23:04 被阅读0次

更多炫酷效果在官网:

https://github.com/scwang90/SmartRefreshLayout

一:ListView分页加载

1.手动加载

① View view=LayoutInflater.from(this).inflate(R.layout.foot,null); 加载更多控件
② addHeaderView(); 加到ListView的头,底部
addFooterView();
③ 点击按钮加载数据,适配器刷新
/**
* 手动加载
*/

                View inflate = LayoutInflater.from(this).inflate(R.layout.layout_bottom, null);
                lv.addFooterView(inflate);
                Button btn = inflate.findViewById(R.id.btn);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ++pno;//原来访问的页数上加1
                        initData();
                    }
                });

2.自动加载

① OnScrollListener() 设置滑动监听事假
② 重写onScroll(),onScrollStateChanged()
③ 在onScroll()中判断firstVisibleItem+visibleItemCount==totalItemCount&&totalItemCount&&total>0,就是最后一个item
④ 在OnScrollStateChanged()中判断是否是最后一条并且屏幕是静止状态
scrollState==SCROLL_STATE_IDLE 手指未接触屏幕,且屏幕页面保持静止
scrollState==SCROLL_STATE_TOUCH_SCROLL 手指按住屏幕滚动(未脱离屏幕)
scrollState==SCROLL_STATE_FLING 手指离开屏幕,页面依然保持滚动

 /**
         * 自动加载:
         *      ListView设置滑动监听
         */
                    lv.setOnScrollListener(this);
                }

                private boolean flag = false;//是否为最后一条数据,false  不是最后一条   true  是最后一条

                /**
                 * 滑动状态改变
                 *      scrollState:当前的状态
                 *      AbsListView.OnScrollListener.SCROLL_STATE_IDLE  静止状态
                 *
                 */
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {

                    //flag为最后一条数据   &&    静止状态
                    if (flag && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
                        ++pno;  //分页加载  pno加1
                        initData();   //加载更多
                    }
                }

                /**
                 * 滑动方法
                 * @param view
                 * @param firstVisibleItem  当前显示的第一个条目位置
                 * @param visibleItemCount  当前页面可显示的条目个数
                 * @param totalItemCount  总共条目数
                 */
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int
                        totalItemCount) {

                    //判断是否为最后一条数据:第一个条目 +  可显示条目数  =  总共条目数   &&   总共条目数  > 0
                    if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount >0){
                        flag = true;   //符合最后一条数据
                    }
                }
              }

3.放图片错乱

原因:在于convertView的重用
解决:viewHolder.img.setTag(student.getImg()); 给img设置Tag(路径)
if(img.getTag()!=null&&img.getTag().equals(str)){img.setImageBitmap(bitmap);} 当img.setImageBitmap之前判断设置的路径和Tag的路径是否一致

XRecyclerView

    mLv.setLoadingListener(new XRecyclerView.LoadingListener() {
    @Override
    public void onRefresh() {
       a=0;
       list.clear();
       initDada();
       mLv.refreshComplete();
    }

    @Override
    public void onLoadMore() {
        ++a;
        initDada();
        mLv.loadMoreComplete();
        }
    });

SmartRefreshLayout上下加载时会有不同的特效

依赖

//recyclerview
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'
XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.freshdemo.MainActivity"
    android:orientation="vertical">
 
 
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlAccentColor="#00000000"
        app:srlPrimaryColor="#00000000"
        app:srlEnablePreviewInEditMode="true">
 
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
 
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
 
</LinearLayout>

这是基本使用其中:

app:srlAccentColor="#00000000"//设置Header主题颜色
app:srlPrimaryColor="#00000000"//设置Footer主题颜色
app:srlEnablePreviewInEditMode="true"//开启和关闭预览功能

代码实现上拉刷新下拉加载

    private RecyclerView mRecyclerView;
    private RefreshLayout mRefreshLayout;
 
    //初始化
    mRecyclerView=findViewById(R.id.rv);
    mRefreshLayout = findViewById(R.id.refreshLayout);
 
           //刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mData.clear();
                mNameAdapter.notifyDataSetChanged();
                refreshlayout.finishRefresh();
            }
        });
        //加载更多
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                for(int i=0;i<30;i++){
                    mData.add("小明"+i);
                }
                mNameAdapter.notifyDataSetChanged();
                refreshlayout.finishLoadmore();
            }
        });
image

2.比较炫酷的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.freshdemo.MainActivity"
    android:orientation="vertical">
 
 
    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlAccentColor="#00000000"
        app:srlPrimaryColor="#00000000"
        app:srlEnablePreviewInEditMode="true">
 
        <com.scwang.smartrefresh.header.PhoenixHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
 
        <com.scwang.smartrefresh.layout.footer.BallPulseFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
 
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
 
</LinearLayout>
image

吃面条的

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:srlEnableFooterTranslationContent="true"
        app:srlPrimaryColor="@color/colorTextContent"
        app:srlEnableAutoLoadMore="false">
        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srlPrimaryColor="@color/colorPrimary"
            app:srlAccentColor="@android:color/white"
            app:srlDrawableProgress="@drawable/animation_loading_frame"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            tools:listitem="@android:layout/simple_list_item_2"/>
        <com.scwang.smartrefresh.header.MaterialHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

相关文章

  • 分页加载.listview.XRecyclerView.Smar

    更多炫酷效果在官网: https://github.com/scwang90/SmartRefreshLayout...

  • 分页加载、无限加载vs半自动加载

    分页加载 分页加载便是在内容区底部放置分页按钮,用户可以通过页码切换信息。 有以下几个特点: 1.对信息的数量以及...

  • Android简单实现 万能适配器 + 下拉刷新 + 无感分页加

    万能适配器 + 下拉刷新 + 无感分页 v3.1更新 完善更多下拉刷新场景 增加【上拉分页加载】和【无感分页加载】...

  • WKWebview 和 UIWebview的区别以及通信

    属性 加载 记录 执行JS 手势 分页 只有 UIWebView 有分页的属性 UIWebPaginationMo...

  • ListView分页加载数据小结

    ListView分页加载数据小结 效果描述 分页:当用户在ListView界面上的时候不会加载全部的数据,而是从服...

  • 大数据渲染解决方式

    大量数据放到select,并支持搜索 解决方式思路: 1、懒加载+分页:由前端完成分页 长列表优化,每次只加载能看...

  • 分页加载

    思路:当页面滑动到底部是,加载更多数据,如果无数据,停止加载提示已经到底。1.设置页面数和显示的数据数量。2.利用...

  • 分页加载

    前言 在需要展示大量的列表式数据时,我们都以分页加载的方式来展示,这也是行业内惯用的方法,这种方式加速了首页内容的...

  • iOS刷新小结

    列表加载更多问题,多页(分页加载) -(void)LoadNewData{[self getDataWithHea...

  • antd Table 上拉加载更多数据

    背景 table不分页 然而数据又很多,所以数据需要做分页加载 查找antd 没找到上下拉加载对应的API 故自己...

网友评论

      本文标题:分页加载.listview.XRecyclerView.Smar

      本文链接:https://www.haomeiwen.com/subject/djpjyqtx.html