<p>对于ListView分页加载数据,是正常情况下是非常有必要。一般对于少量的数据一次性把全部数据加载到ListView中显示,对于数据量多比较大,特别在资源有限的手机设备中更有重要,由用户去点击加载想要数据更为合适。在一些博客上看到关于ListView分页加载,但不太全面,实用性不高,因此我模拟正常情况下ListView分页加载,对于有方面帮助的朋友肯定有用,由于本人技术有限,可能存在些问题,欢迎指正,谢谢!</p><p>1. 先看效果吧,<br/></p><p><img src="/up_pic/201812/140439219780.png" title="140439219780.png"/></p><p><img src="/up_pic/201812/140439212995.png" title="140439212995.png"/></p><p><img src="/up_pic/201812/140439212865.png" title="140439212865.png"/></p><p><img src="/up_pic/201812/140439219258.png" title="140439219258.png"/></p><p><img src="/up_pic/201812/140439218911.png" title="140439218911.png"/></p><p>2. java代码:MainActivity.java</p><pre class="brush:java;toolbar:false">/** *ListView分页加载数据 *@authorzhangkai281 * */ publicclassMainActivityextendsActivity{ privateListViewlistView; privateList&lt;Map&lt;String,Object&gt;&gt;data; privatelistViewAdapteradapter; //分页加载的数据的数量 privateintpageSize=10; privatefinalintpageType=1; //查看更多 privateTextViewmoreTextView; //正在加载进度条 privateLinearLayoutloadProgressBar; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.list_page); listView=(ListView)findViewById(R.id.lv_id); //第一个参数:1起始数第二个参数:显示的数目 data=InitValue.initValue(1,15); //在ListView中添加&quot;加载更多&quot; addPageMore(); //添加&quot;加载更多&quot;一定要在设置Adapter之前 adapter=newlistViewAdapter(); listView.setAdapter(adapter); } privateclasslistViewAdapterextendsBaseAdapter{ intcount=data.size(); @Override publicintgetCount(){ returncount; } @Override publicObjectgetItem(intposition){ returnposition; } @Override publiclonggetItemId(intposition){ returnposition; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ Viewview=LayoutInflater.from(MainActivity.this).inflate(R.layout.list_page_item,null); TextViewtitle=(TextView)view.findViewById(R.id.tv_id); TextViewtext=(TextView)view.findViewById(R.id.title_id); title.setText(data.get(position).get(&quot;title&quot;).toString()); text.setText(data.get(position).get(&quot;text&quot;).toString()); returnview; } } /** *加载下一页的数据 *@parampageStart *@parampageSize */ privatevoidchageListView(intpageStart,intpageSize){ List&lt;Map&lt;String,Object&gt;&gt;data=InitValue.initValue(pageStart,pageSize); for(Map&lt;String,Object&gt;map:data){ this.data.add(map); } data=null; } /** *在ListView中添加&quot;加载更多&quot; */ privatevoidaddPageMore(){ Viewview=LayoutInflater.from(this).inflate(R.layout.list_page_load,null); moreTextView=(TextView)view.findViewById(R.id.more_id); loadProgressBar=(LinearLayout)view.findViewById(R.id.load_id); moreTextView.setOnClickListener(newButton.OnClickListener(){ @Override publicvoidonClick(Viewv){ //隐藏&quot;加载更多&quot; moreTextView.setVisibility(View.GONE); //显示进度条 loadProgressBar.setVisibility(View.VISIBLE); newThread(newRunnable(){ @Override publicvoidrun(){ //休眠3秒,用于模拟网络操作时间 try{ Thread.sleep(3000); }catch(InterruptedExceptione){ e.printStackTrace(); } //加载模拟数据:下一页数据,在正常情况下,上面的休眠是不需要,直接使用下面这句代码加载相关数据 chageListView(data.size(),pageSize); Messagemes=handler.obtainMessage(pageType); handler.sendMessage(mes); } }).start(); } }); listView.addFooterView(view); } privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ switch(msg.what){ casepageType: //改变适配器的数目 adapter.count+=pageSize; //通知适配器,发现改变操作 adapter.notifyDataSetChanged(); //再次显示&quot;加载更多&quot; moreTextView.setVisibility(View.VISIBLE); //再次隐藏&quot;进度条&quot; loadProgressBar.setVisibility(View.GONE); break; default: break; } super.handleMessage(msg); } }; }</pre><p>3. 模拟数据,通常是通过网络取得服务器的数据,显示出来,得向服务传递参数包括分页相关,当时启起行数,每页显示多行数据。也可以加载本地的SQLite库中的数据。加载网络的比较比吧,以下只是模拟数据:</p><pre class="brush:java;toolbar:false">publicclassInitValue{ publicstaticintpage=1; /** *模拟数据分页加载, *@parampageStart起始数 *@parampageSize每页显示数目 *@return */ publicstaticList&lt;Map&lt;String,Object&gt;&gt;initValue(intpageStart,intpageSize){ Map&lt;String,Object&gt;map; List&lt;Map&lt;String,Object&gt;&gt;list=newArrayList&lt;Map&lt;String,Object&gt;&gt;(); for(inti=0;i&lt;pageSize;i++){ map=newHashMap&lt;String,Object&gt;(); map.put(&quot;text&quot;,&quot;zhangkai281发表文章&quot;); map.put(&quot;title&quot;,page+&quot;_ListView分页显示&quot;); ++page; list.add(map); } returnlist; } }</pre><p>4. 布局文件:main.xml:</p><pre class="brush:xml;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;ScrollViewandroid:layout_width=&quot;fill_parent&quot;xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_height=&quot;wrap_content&quot;android:scrollbars=&quot;vertical&quot;&gt; &lt;LinearLayoutandroid:orientation=&quot;vertical&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;&gt; &lt;TextViewandroid:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;android:text=&quot;@string/app_name&quot;/&gt; &lt;ViewFlipperandroid:id=&quot;@+id/vf_id&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;/&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt;</pre><p>5. 布局文件:list_page.xml:</p><pre class="brush:java;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;&gt; &lt;ListViewandroid:id=&quot;@+id/lv_id&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;/&gt; &lt;/LinearLayout&gt;</pre><p>6. 布局文件:list_page_load.xml</p><pre class="brush:java;toolbar:false">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot;android:layout_height=&quot;fill_parent&quot; android:orientation=&quot;vertical&quot;android:gravity=&quot;center_horizontal&quot; android:paddingTop=&quot;13dp&quot;android:paddingBottom=&quot;13dp&quot;&gt; &lt;TextViewandroid:id=&quot;@+id/more_id&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;android:text=&quot;查看更多...&quot; android:textSize=&quot;25dp&quot;android:gravity=&quot;center_horizontal&quot;/&gt; &lt;LinearLayoutandroid:id=&quot;@+id/load_id&quot;android:orientation=&quot;horizontal&quot; android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; android:visibility=&quot;gone&quot;&gt; &lt;ProgressBarandroid:layout_width=&quot;match_parent&quot;android:layout_height=&quot;wrap_content&quot; android:layout_gravity=&quot;center_horizontal&quot;/&gt; &lt;TextViewandroid:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;fill_parent&quot; android:gravity=&quot;center_vertical&quot;android:layout_marginLeft=&quot;10dp&quot; android:text=&quot;正在加载...&quot;android:textSize=&quot;20dp&quot;/&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt;</pre><p>7. 还有此小xml加有列出,比较简单,此处省略,</p>
返回顶部 留言