有的时候,ListView的数据是从网上传来的,用户不需要那么多的数据,
那么就先让它显示其中一小部分,然后让用户自己去点击加载剩余数据。
代码实现:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lv_list" ></ListView> </LinearLayout> |
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_item" /> </LinearLayout> |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/fl_more" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/ll_wait" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" android:visibility="invisible" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="正在加载请稍等..." android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/ll_more" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:text="查看更多" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </FrameLayout> |
package com.hwh.list; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.ListView; public class ListDemoActivity extends Activity { private ListView lv_list; private List<String> data;//总数据集合 private List<String> show_data;//要显示的数据集合 private LinearLayout ll_wait;//等待中的界面 private LinearLayout ll_more;//显示跟多的界面 private ListAdapter adapter;//数据适配器 private View view;//listview的底部view private Handler handler=new Handler() { public void handleMessage(Message msg) { if(msg.what==10) { ll_wait.setVisibility(LinearLayout.INVISIBLE); ll_more.setVisibility(LinearLayout.VISIBLE); setShowData(5); adapter.setData(show_data); adapter.notifyDataSetChanged(); //如果要显示的数据和总数据的大小一样则不显示更多条目 if (show_data.size() == data.size()) { lv_list.removeFooterView(view); } } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv_list=(ListView) findViewById(R.id.lv_list); data=new ArrayList<String>(); show_data=new ArrayList<String>(); //使用假数据模拟从网上请求 for(int i=1;i<=20;i++) { data.add(i+"条数据,分页显示"); } setShowData(5); view=initFooterView(); lv_list.addFooterView(view);//添加底部的view adapter=new ListAdapter(show_data, this); lv_list.setAdapter(adapter); } /** * 设置每页显示几条 * @param pagesize */ public void setShowData(int pagesize) { int index=show_data.size(); for(int i=0;i<pagesize;i++) { show_data.add(data.get(index+i)); } } /** * 初始化底部的view并返回 * @return */ public View initFooterView() { FrameLayout view=(FrameLayout) View.inflate(this, R.layout.more_item, null); ll_wait=(LinearLayout) view.findViewById(R.id.ll_wait); ll_more=(LinearLayout) view.findViewById(R.id.ll_more); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ll_wait.setVisibility(LinearLayout.VISIBLE); ll_more.setVisibility(LinearLayout.INVISIBLE); new Thread(){ public void run() { try { //模拟联网的延迟 Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } Message msg=handler.obtainMessage(10); handler.sendMessage(msg); }; }.start(); } }); return view; } } |
package com.hwh.list; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ListAdapter extends BaseAdapter { private List<String> data; private Context context; private static TextView tv; public void setData(List<String> data) { this.data = data; } public ListAdapter(List<String> data, Context context) { this.data = data; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return data.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.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) { String str=data.get(position); View view; if(convertView!=null) { view=convertView; } else { view=View.inflate(context, R.layout.list_items, null); } tv=(TextView) view.findViewById(R.id.tv_item); tv.setText(str); return view; } } |
这里只是ListView分页的简单实现,具体的功能还得看项目中的业务需求。
------不足之处请大家指出,谢谢。