当前位置:操作系统 > 安卓/Android >>

ListView动态加载数据分页(使用Handler+线程和AsyncTask两种方法)

在android开发中,经常需要使用数据分页,比如要实现一个新闻列表的显示,或者博文列表的显示,不可能第一次加载就展示出全部,这就需要使用分页的方法来加载数据,在android中Handler经常用来在耗时的工作中,它接收子线程发送的数据,并使用数据配合更新UI,AsyncTask是在一个线程中执行耗时操作然后把结果传给UI线程,不需要你亲自去管理线程和句柄。
一、使用Handler+线程方法
1、基础知识
Handler在android系统中,主要负责发送和接收消息,它的用途主要有以下两种:
(1)按照计划来处理一个消息(sendMessage(Message)方法)或者执行某个runnable实例(post(Runnable)方法)
(2)把其他的线程对象放入消息队列中,避免线程冲突。
     消息的发送通过post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int),sendMessage(Message), sendMessageAtTime(Message, long)和 sendMessageDelayed(Message, long) 方法完成。对于postXXX方法通过Runnable对象给消息队列,并在消息队列到达后被调用。对于sendMessageXXX方法,则传递一个包含message对象,该对象可以被Handler类的handlerMessage(Message)方法处理。
2、主要代码
public class HandlerDemo extends Activity implements OnScrollListener {
 
    private ListView mListView;
    LinearLayout loadingLayout;
    private Thread mThread;
    private ListViewAdapter adapter;
 
    private int startIndex = 1;// 从第1条开始
    private int size = 10;// 每次下载十条数据
    private List<News> newsList;
    List<Map<String, String>> data ;
 
    /*
     * 设置布局显示属性
     */
    private LayoutParams mLayoutParams = new LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
 
    private LayoutParams ffLayoutParams = new LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT);
 
    private ProgressBar progressBar;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_main);
        data=new ArrayList<Map<String, String>>();
        addView();
    }
 
    private void addView() {
        if (startIndex == 1) {
            newsList = new ArrayList<News>();
            newsList = getNewsList();
        }
        getdata(newsList);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        progressBar = new ProgressBar(this);
        layout.addView(progressBar, mLayoutParams);
        TextView textView = new TextView(this);
        textView.setText("加载中...");
        textView.setGravity(Gravity.CENTER_VERTICAL);
        layout.addView(textView, ffLayoutParams);
        layout.setGravity(Gravity.CENTER);
        loadingLayout = new LinearLayout(this);
        loadingLayout.addView(layout, mLayoutParams);
        loadingLayout.setGravity(Gravity.CENTER);
 
        // 得到一个ListView用来显示条目
        mListView = (ListView) findViewById(R.id.listView);
        mListView.addFooterView(loadingLayout);
        adapter = new ListViewAdapter();
        mListView.setAdapter(adapter);
        mListView.setOnScrollListener(this);
        mListView.setTextFilterEnabled(true);
    }
 
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        if (firstVisibleItem + visibleItemCount == totalItemCount) {
            if (mThread == null || !mThread.isAlive()) {
                mThread = new Thread() {
 
                    @Override
                    public void run() {
                        newsList = new ArrayList<News>();
                        newsList = getNewsList();
                        getdata(newsList);
                        Message msg = new Message();
                        msg.what = 1;
                        handler.sendMessage(msg);
                    }
                };
                mThread.run();
            }
        }
    }
     
    Handler handler = new Handler() {
 
        @Override
        public void handleMessage(Message msg) {
 &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,