美文网首页
自己做ListView的下拉刷新和自动加载更多

自己做ListView的下拉刷新和自动加载更多

作者: 码圣 | 来源:发表于2016-10-03 22:27 被阅读174次

    首先主布局文件是SwipeRefreshLayout+ListView

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context="com.yus.litepaldemo.MainActivity">
    
       <android.support.v4.widget.SwipeRefreshLayout
           android:id="@+id/swp"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    
           <ListView
               android:id="@+id/lv"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
           </ListView>
       </android.support.v4.widget.SwipeRefreshLayout>
    
    </RelativeLayout>
    

    加载更多作为脚布局添加到ListView

            lv = (ListView) findViewById(R.id.lv);
            View footView = View.inflate(getApplicationContext(), R.layout.loading, null);
            ll_loading_detail = (LinearLayout) footView.findViewById(R.id.ll_loading_detail);
            tvNoMoreData = (TextView) footView.findViewById(R.id.tvNoMoreData);
            lv.addFooterView(footView);
            myAdapter = new MyAdapter();
            lv.setAdapter(myAdapter);
            lv.setOnScrollListener(this);
    

    ListView的滚动状态监听,在这里判断是否已经滑到当前的最后一条item

    @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                if (view.getLastVisiblePosition() == view.getCount() - 1) {//最后一条
                    Log.d("alan","add5Item--");
                    add5Item();
                }
            }
        }
    

    到当前最后一条后,加载5条数据,加载四次后,模拟没有更多数据的情况

    private void add5Item() {
            currentLoadTime++;
            if (currentLoadTime>NO_MORE_DATA){
                return;
            }
            for (int i = 0; i < 5; i++) {
                mLists.add("new");
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (currentLoadTime>=NO_MORE_DATA){
                        tvNoMoreData.setVisibility(View.VISIBLE);
                        ll_loading_detail.setVisibility(View.GONE);
                    }
                    myAdapter.notifyDataSetChanged();
                }
            }, 200);
        }
    

    在这里处理刷新:

        @Override
        public void onRefresh() {
            Log.d("alan","刷新---");
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    swp.setRefreshing(false);
                }
            }, 700);
        }
    

    下拉刷新和自动加载更多就做好了,这算是一种简单方便的实现方式。

    RefreshLoadMore.gif

    相关文章

      网友评论

          本文标题:自己做ListView的下拉刷新和自动加载更多

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