得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述android的ListView的分页加载相关的知识,希望能为你提供帮助。
main6.xml
< ?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="vertical" > < ListView android:id="@+id/listView6_1_1" android:layout_width="match_parent" android:layout_height="fill_parent" > < /ListView> < /LinearLayout>
main6_item.xml
< ?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="wrap_content" android:orientation="vertical" > < TextView android:id="@+id/textView_title_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="28sp" android:text="title" android:textColor="#55ff88" /> < TextView android:id="@+id/textView_content_2_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="duanluosdfsdf" /> < /LinearLayout>
MainActivity6
package com.example.listview; import java.util.ArrayList; import java.util.List; import java.util.Vector; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity6 extends Activity implements OnScrollListener{ private ListView listView; private List< News> news=new Vector< News> (); private MyAdapter myDapter; //线程之间通信的桥梁 private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: //通知适配器,更新数据 myDapter.notifyDataSetChanged(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main6); listView=(ListView) findViewById(R.id.listView6_1_1); //设置底部视图 View footView=getLayoutInflater().inflate(R.layout.load, null); listView.addFooterView(footView); listView.setOnScrollListener(this); //initDate(); new LoadDateThread().start(); //加载数据的线程 myDapter=new MyAdapter(); listView.setAdapter(myDapter); } /** * 初始化数据 * */ int index; //计数器 void initDate(){ for(int i=0; i< 10; i++){ News newss=new News(); newss.title="title-"+index; newss.content="content-"+index; index++; news.add(newss); } } /** * 自定义适配器 * */ class MyAdapter extends BaseAdapter{@Override public int getCount() { // TODO Auto-generated method stub return news.size(); }@Override public Object getItem(int position) { // TODO Auto-generated method stub return news.get(position); }@Override public long getItemId(int position) { // TODO Auto-generated method stub return position; }@Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Viewholer viewholer; if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.main6_item, null); viewholer=new Viewholer(); viewholer.textView=(TextView) convertView.findViewById(R.id.textView_title_1_1); viewholer.textContent=(TextView) convertView.findViewById(R.id.textView_content_2_2); convertView.setTag(viewholer); }else{ viewholer=(Viewholer) convertView.getTag(); } //从veclor中取出数据并填充 News newss=news.get(position); viewholer.textView.setText(newss.title); viewholer.textContent.setText(newss.content); return convertView; } } static class Viewholer{ TextView textView; TextView textContent; } // int scrollState=0; int visiableLastIndex=0; //滚动条状态发生变化事件方法 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub /* this.scrollState=scrollState; System.out.println("scrollState:"+scrollState); */ //滚动条在最下面并且滚动条是停止状态 if(myDapter.getCount()==visiableLastIndex& & scrollState==OnScrollListener.SCROLL_STATE_IDLE){ //启动线程加载数据 new LoadDateThread().start(); } } //滚动事件方法 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub System.out.println("view:"+view); System.out.println("firstVisibleItem:"+firstVisibleItem); System.out.println("visibleItemCount:"+visibleItemCount); System.out.println("totalItemCount"+totalItemCount); visiableLastIndex=firstVisibleItem+visibleItemCount-1; } //模拟加载数据 //子线程不允许访问主线程的UI组件(保证主线程UI组件安全) class LoadDateThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); initDate(); try { Thread.sleep(2000); //通知主线程,数据已加载完毕 handler.sendEmptyMessage(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
News
package com.example.listview; /** * 新闻对象 * */ public class News { String title; String content; }
【android的ListView的分页加载】
文章图片
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1882577
推荐阅读
- kaifyou Android 7.0 UICC 分析
- android免root兼容所有版本ui调试工具
- Android启动过程介绍
- Android 7.0 ActivityManagerService 广播(Broadcast)相关流程分析
- Android图表 MPAndroidChart折线图
- Android测试的坑——屏幕翻转
- Android Service和Thread的关系
- Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架
- Android快速开发偷懒必备 支持DataBinding啦~爽炸,一行实现花式列表