前段时间做的新浪微博项目一直想实现listview分页加载数据,今天终于实现了,哈哈!感觉挺好的,今天又写了个demo给大家分享下。

              首先说下listview的优化方案,这也是面试中常考的题目。优化方案有三种:1,如果自定义适配器,那么在getView方法中判断contentView是否为空,如果为空创建contentView并返回,如果不为空直接返回contentView。这样能尽可能少创建view。2.给contentView设置tag,传入一个viewHoder对象,用于缓存要实现的数据。3,如果listview中显示的item太多,就要考虑分页加载了。

               下面就注意介绍一下分页加载数据。首先在layout下创建listview.xml:

           

[html] view plain copy

 
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     android:orientation=“vertical” >  
  6.   
  7.     <ListView  
  8.         android:id=“@+id/listView1”  
  9.         android:layout_width=“match_parent”  
  10.         android:layout_height=“wrap_content” >  
  11.     </ListView>  
  12.   
  13. </LinearLayout>  

          然后创建listview_item.xml:

 

          

[html] view plain copy

 
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”    
  3.     android:orientation=“vertical”    
  4.     android:layout_width=“fill_parent”    
  5.     android:layout_height=“fill_parent”>    
  6.     <TextView    
  7.         android:id=“@+id/list_item_text”    
  8.         android:layout_width=“fill_parent”    
  9.         android:layout_height=“fill_parent”    
  10.         android:gravity=“center”    
  11.         android:textSize=“20sp”    
  12.         android:paddingTop=“10dp”    
  13.         android:paddingBottom=“10dp”/>    
  14. </LinearLayout>    

 

           再为跟多按钮添加一个xml:

      

[html] view plain copy

 
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“wrap_content”  
  5.     android:gravity=“center”  
  6.     android:orientation=“vertical” >  
  7.   
  8.     <Button  
  9.         android:id=“@+id/loadMoreButton”  
  10.         android:layout_width=“fill_parent”  
  11.         android:layout_height=“wrap_content”  
  12.         android:onClick=“loadMore”  
  13.         android:text=“加载更多” />  
  14.   
  15. </LinearLayout>  

      

 

           代码部分:

       

[html] view plain copy

 
  1. public class ListViewAdapter extends BaseAdapter {  
  2.   
  3.     private static Map<Integer,Viewm=new HashMap<Integer,View>();  
  4.       
  5.     private List<String> items;  
  6.     private LayoutInflater inflater;  
  7.       
  8.     public ListViewAdapter(List<String> items, Context context) {  
  9.         super();  
  10.         this.items = items;  
  11.         this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  12.     }  
  13.   
  14.     @Override  
  15.     public int getCount() {  
  16.         // TODO Auto-generated method stub  
  17.         return items.size();  
  18.     }  
  19.   
  20.     @Override  
  21.     public Object getItem(int position) {  
  22.         // TODO Auto-generated method stub  
  23.         return items.get(position);  
  24.     }  
  25.   
  26.     @Override  
  27.     public long getItemId(int position) {  
  28.         // TODO Auto-generated method stub  
  29.         return position;  
  30.     }  
  31.   
  32.     @Override  
  33.     public View getView(int position, View contentView, ViewGroup arg2) {  
  34.         // TODO Auto-generated method stub  
  35.         contentView=m.get(position);  
  36.         if(contentView==null){  
  37.             contentView=inflater.inflate(R.layout.listview_item, null);  
  38.             TextView text=(TextView) contentView.findViewById(R.id.list_item_text);  
  39.             text.setText(items.get(position));  
  40.         }  
  41.         m.put(position, contentView);  
  42.         return contentView;  
  43.     }  
  44.       
  45.     public void addItem(String item) {    
  46.         items.add(item);    
  47.     }    
  48.   
  49. }  


         

[html] view plain copy

 
  1. public class ListViewActivity extends Activity implements OnScrollListener  {  
  2.       List<Stringitems = new ArrayList<String>();    
  3.      private ListView listView;    
  4.         private int visibleLastIndex = 0;   //最后的可视项索引    
  5.         private int visibleItemCount;       // 当前窗口可见项总数    
  6.         private ListViewAdapter adapter;    
  7.         private View loadMoreView;    
  8.         private Button loadMoreButton;    
  9.         private Handler handler = new Handler();    
  10.         
  11.         @Override    
  12.         public void onCreate(Bundle savedInstanceState) {    
  13.             super.onCreate(savedInstanceState);    
  14.             setContentView(R.layout.listview);    
  15.                 
  16.             loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);    
  17.             loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);    
  18.             loadMoreButton.setOnClickListener(new OnClickListener() {  
  19.                   
  20.                 @Override  
  21.                 public void onClick(View v) {  
  22.                     // TODO Auto-generated method stub  
  23.                     loadMoreButton.setText(“正在加载…”);   //设置按钮文字loading    
  24.                     handler.postDelayed(new Runnable() {    
  25.                         @Override    
  26.                         public void run() {    
  27.                                 
  28.                             loadData();    
  29.                                 
  30.                             adapter.notifyDataSetChanged(); //数据集变化后,通知adapter    
  31.                             listView.setSelection(visibleLastIndex – visibleItemCount + 1); //设置选中项    
  32.                                 
  33.                             loadMoreButton.setText(“加载更多”);    //恢复按钮文字    
  34.                         }    
  35.                     }, 1000);    
  36.                 }  
  37.             });  
  38.             listView = (ListView) this.findViewById(R.id.listView1);  
  39.                 
  40.             listView.addFooterView(loadMoreView);   //设置列表底部视图    
  41.            // listView.addHeaderView(v)    //设置列表顶部视图  
  42.         
  43.             initAdapter();    
  44.                 
  45.             listView.setAdapter(adapter);                //自动为id是list的ListView设置适配器    
  46.                 
  47.             listView.setOnScrollListener(this);     //添加滑动监听    
  48.             listView.setOnItemClickListener(new OnItemClickListener() {  
  49.   
  50.                 @Override  
  51.                 public void onItemClick(AdapterView<?> arg0, View view,  
  52.                         int position, long arg3) {  
  53.                     // TODO Auto-generated method stub  
  54.                     Toast.makeText(getApplicationContext(), items.get(position),Toast.LENGTH_SHORT).show();  
  55.                 }  
  56.             });  
  57.         }    
  58.             
  59.         /**   
  60.          * 初始化适配器   
  61.          */    
  62.         private void initAdapter() {    
  63.            
  64.             for (int i = 0; i 20; i++) {    
  65.                 items.add(String.valueOf(i + 1));    
  66.             }    
  67.             adapter = new ListViewAdapter(items,this);    
  68.         }    
  69.         
  70.         /**   
  71.          * 滑动时被调用   
  72.          */    
  73.         @Override    
  74.         public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {    
  75.             this.visibleItemCount = visibleItemCount;    
  76.             visibleLastIndex = firstVisibleItem + visibleItemCount – 1;    
  77.         }    
  78.         
  79.         /**   
  80.          * 滑动状态改变时被调用   
  81.          */    
  82.         @Override    
  83.         public void onScrollStateChanged(AbsListView view, int scrollState) {    
  84.             int itemsLastIndex = adapter.getCount() – 1;    //数据集最后一项的索引    
  85.             int lastIndex = itemsLastIndex + 1;             //加上底部的loadMoreView项    
  86.             if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {    
  87.                 //如果是自动加载,可以在这里放置异步加载数据的代码    
  88.                 Log.i(“LOADMORE”, “loading…”);    
  89.             }    
  90.         }    
  91.             
  92.   
  93.         /**   
  94.          * 模拟加载数据   
  95.          */    
  96.         private void loadData() {    
  97.             int count = adapter.getCount();    
  98.             for (int i = count; i count + 20; i++) {    
  99.                 adapter.addItem(String.valueOf(i + 1));    
  100.             }    
  101.         }  
  102.   
  103.           
  104. }  


           最后看看效果:

 

         

       

        

        

        

 

版权声明:本文为aademeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/aademeng/articles/6123721.html