zoukankan      html  css  js  c++  java
  • android 批量加载数据

    public class MainActivity extends Activity {
        private ListView listView;
        private List<String> data = new ArrayList<String>();
        ArrayAdapter<String> adapter;
        View footer;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ///通过xml布局文件生成View对象
            footer = getLayoutInflater().inflate(R.layout.footer, null);
            
            listView = (ListView) this.findViewById(R.id.listView);
    
            //为ListView设置滑动监听事件处理器
            listView.setOnScrollListener(new ScrollListener());
            
            data.addAll(DataService.getData(0, 20));        
            adapter = new ArrayAdapter<String>(this, R.layout.listview_item, R.id.textView, data);
            listView.addFooterView(footer);//添加页脚(放在ListView最后,并且要放在setAdapter()方法之前,因为内部会有所包装)
            listView.setAdapter(adapter);
            listView.removeFooterView(footer);//刚开始时删除页脚
        }
        
        private int number = 20;//每次获取多少条数据
        private int maxpage = 5;//总共有多少页
        private boolean loadfinish = true;
        private final class ScrollListener implements OnScrollListener{
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.i("MainActivity", "onScrollStateChanged(scrollState="+ scrollState+ ")");
            }
            
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.i("MainActivity", "onScroll(firstVisibleItem="+ firstVisibleItem+ ",visibleItemCount="+
                        visibleItemCount+ ",totalItemCount="+ totalItemCount+ ")");
                
                final int loadtotal = totalItemCount;
                int lastItemid = listView.getLastVisiblePosition();//获取当前屏幕最后Item的ID
                if((lastItemid+1) == totalItemCount){//达到数据的最后一条记录
                    if(totalItemCount > 0){
                        //当前页
                        int currentpage = totalItemCount%number == 0 ? totalItemCount/number : totalItemCount/number+1;
                        int nextpage = currentpage + 1;//下一页
                        if(nextpage <= maxpage && loadfinish){
                            loadfinish = false;
                            //加载数据时显示页脚
                            listView.addFooterView(footer);
                            
                            new Thread(new Runnable() {                        
                                public void run() {
                                    try {
                                        Thread.sleep(3000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    List<String> result = DataService.getData(loadtotal, number);
                                    handler.sendMessage(handler.obtainMessage(100, result));
                                }
                            }).start();
                        }        
                    }
                            
                }
            }
        }
        
        Handler handler = new Handler(){
            public void handleMessage(Message msg) {
                data.addAll((List<String>) msg.obj);
                adapter.notifyDataSetChanged();//告诉ListView数据已经发生改变,要求ListView更新界面显示
                if(listView.getFooterViewsCount() > 0) listView.removeFooterView(footer);
                loadfinish = true;
            }        
        };
        
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <!--系统中的圆形滚动条-->
    
        <ProgressBar android:id="@+id/c81_forthBar"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
             style="?android:attr/progressBarStyle" /> 
        <TextView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:text="数据正在加载..."
        />
    </LinearLayout>
    <!--页脚xml-->
  • 相关阅读:
    IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET
    mysql 存储过程 循环
    Mysql 遇到过的自带函数使用
    Mysql:is not allowed to connect to this MySQL server
    mysql 删除或更新时出现 you are usering safe update model 的解决方案
    将一个服务器的表数据插入到另一个服务器的表中
    C#操作Excel无法删除worksheet解决方案
    安装MongoDB数据库的注意事项
    Python爬取小猪短租,用的是lxml解析器
    使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ​​​​
  • 原文地址:https://www.cnblogs.com/heml/p/3522027.html
Copyright © 2011-2022 走看看