• Android中Listview实现分页加载效果OnScrollListener


    activity_main.xml

    <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=".MainActivity" 
        android:background="#000000"
        >
    
        <ListView 
        android:id="@+id/listveiw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        ></ListView>
    
    </RelativeLayout>

    MainActivity.java

    package com.bawei.day05_onscrolllistener;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AbsListView;
    import android.widget.AbsListView.OnScrollListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnScrollListener{
    
        private ListView listView;
        private List<String> list;
        //是否需要加载更多
        private boolean isLoad = false;
        
        int count = 1;
        private ArrayAdapter<String> adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.listveiw);
            list = new ArrayList<String>();
            //赋值
            getData();
            //适配
            adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_expandable_list_item_1,list);
            listView.setAdapter(adapter);
            //监听
              listView.setOnScrollListener(this);
            
            
        }
       //赋值
        private void getData() {
        
            for (int i = 0; i <30; i++) {
                list.add(i+"");
            }
        }
    
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            Log.i("TAG", "firstVisibleItem="+firstVisibleItem);
            Log.i("TAG", "visibleItemCount="+visibleItemCount);
            Log.i("TAG", "totalItemCount="+totalItemCount);
            //isLoad为是否滚动到底部
            isLoad = ((firstVisibleItem+visibleItemCount)==totalItemCount);
        }
        /* (non-Javadoc)
         * 
         * SCROLL_STATE_FLING 手指做出抛的动作
         * SCROLL_STATE_IDLE 停止滚动
         * SCROLL_STATE_TOUCH_SCROLL 手指在屏幕上,屏幕滚动
         * @see android.widget.AbsListView.OnScrollListener#onScrollStateChanged(android.widget.AbsListView, int)
         */
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            Log.i("TAG", "onScrollStateChanged  isLoad="+isLoad+";是否是停止滚动:"+(scrollState==OnScrollListener.SCROLL_STATE_IDLE));
            Log.i("TAG", "scrollState"+scrollState);
            //当ListView滚动到底部,并且ListView停止滚动
            //isLoad为是否滚动到底部
            //scrollState为当前状态有三种 ,不断变化,只有当他停止滚动才加载数据 
            if(isLoad&&scrollState==OnScrollListener.SCROLL_STATE_IDLE){
                Toast.makeText(getApplicationContext(), "开始加载数据", 1).show();
                 //得到当前显示个数
                int id=adapter.getCount();
                //加载数据
                for (int i = id; i < id+10; i++) {
                list.add("加载第"+count+"次:"+i)    ;
                }
                //加载了几次
                count++;
                //刷新
                adapter.notifyDataSetChanged();
            }
        }
    
    
    }

  • 相关阅读:
    spring mvc ajax请求
    spring mvc 返回页面数据
    spring mvc 参数传递的三种方式
    spring mvc 注解示例
    spring mvc handler的三种方式
    MyBatis 一级、二级缓存
    MyBatis 调用存储过程
    MyBatis动态SQL与模糊查询
    spring mvc 注解入门示例
    MyBatis 一对多关联查询
  • 原文地址:https://www.cnblogs.com/1426837364qqcom/p/5386704.html
Copyright © 2020-2023  润新知