【android动态布局】之【ListView动态加载数据模板(使用xml布局)】

转自:http://www.apkbus.com/android-19497-1-1.html

笔者想利用xml布局文件实现一下,因为布局文件在xml文件中实现要规范一些,原理和之前那一篇是一样的,直接来代码
主布局文件other_listview.xml,注意ListView定义id的方式
  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.     >
  7.     <ListView  
  8.         android:id="@android:id/list"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         />
  12. </LinearLayout>
复制代码
ListView里面每个Item的布局文件other_listview_item.xml,里面的ImageView使用的图片自己可以随便替换一张
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <ImageView
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:scaleType="fitXY"
  11.         android:src="@drawable/avira_antyvir"/>
  12.     <TextView
  13.         android:id="@+id/tv"
  14.         android:layout_width="fill_parent"
  15.         android:layout_height="20dp"
  16.         android:text="@string/hello"
  17.         />
  18. </LinearLayout>
复制代码
再来个布局文件other_listview_footer_more.xml,这个文件就是ListView最下面那个View,当点击Button的时候显示进度条
  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.     >
  7.     <Button
  8.         android:id="@+id/button"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:text="更多"
  12.         />
  13.     <LinearLayout
  14.         android:orientation="horizontal"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         android:gravity="center"
  18.         android:id="@+id/linearlayout">
  19.         <ProgressBar
  20.             android:layout_width="wrap_content"
  21.             android:layout_height="wrap_content"/>
  22.         <TextView
  23.             android:layout_width="wrap_content"
  24.             android:layout_height="wrap_content"
  25.             android:text="正在获取..."/>   
  26.     </LinearLayout>
  27. </LinearLayout>
复制代码
类文件OtherListView.java
  1. package com.focus.loading;

  2. import android.app.ListActivity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.ViewGroup;
  9. import android.widget.AbsListView;
  10. import android.widget.AbsListView.OnScrollListener;
  11. import android.widget.BaseAdapter;
  12. import android.widget.Button;
  13. import android.widget.LinearLayout;
  14. import android.widget.ListView;
  15. import android.widget.TextView;

  16. public class OtherListView extends ListActivity implements OnScrollListener {

  17.     ListView list;
  18.     int scrollState;
  19.     int count = 40;
  20.     int lastItem;
  21.     int visibleItemCount;

  22.     Button footerButton;
  23.     LinearLayout footerProgressBarLayout;
  24.     View view;
  25.     ListAdapter listAdapter = new ListAdapter();

  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.other_listview);

  30.         LayoutInflater inflater = LayoutInflater.from(this);
  31.         view = inflater.inflate(R.layout.other_listview_footer_more, null);
  32.         footerButton = (Button) view.findViewById(R.id.button);
  33.         footerProgressBarLayout = (LinearLayout) view
  34.                 .findViewById(R.id.linearlayout);
  35.         footerProgressBarLayout.setVisibility(View.GONE);
  36.         footerButton.setOnClickListener(new OnClickListener() {

  37.             @Override
  38.             public void onClick(View v) {
  39.                 if (lastItem == listAdapter.count
  40.                         && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
  41.                     footerButton.setVisibility(View.GONE);
  42.                     footerProgressBarLayout.setVisibility(View.VISIBLE);
  43.                     if (listAdapter.count <= count) {
  44.                         new Handler().postDelayed(new Runnable() {

  45.                             @Override
  46.                             public void run() {
  47.                                 listAdapter.count += 10;
  48.                                 listAdapter.notifyDataSetChanged();
  49.                                 // list.setSelection(lastItem);
  50.                                 footerButton.setVisibility(View.VISIBLE);
  51.                                 footerProgressBarLayout
  52.                                         .setVisibility(View.GONE);
  53.                             }
  54.                         }, 2000);
  55.                     }

  56.                 }
  57.             }
  58.         });

  59.         list = getListView();
  60.         list.addFooterView(view);
  61.         list.setAdapter(listAdapter);
  62.         list.setOnScrollListener(this);
  63.     }

  64.     class ListAdapter extends BaseAdapter {

  65.         int count = 10;

  66.         @Override
  67.         public int getCount() {
  68.             return count;
  69.         }

  70.         @Override
  71.         public Object getItem(int position) {
  72.             return position;
  73.         }

  74.         @Override
  75.         public long getItemId(int position) {
  76.             return position;
  77.         }

  78.         @Override
  79.         public View getView(int position, View convertView, ViewGroup parent) {
  80.             LayoutInflater inflater = LayoutInflater.from(OtherListView.this);
  81.             View view = inflater.inflate(R.layout.other_listview_item, null);
  82.             TextView tv = (TextView) view.findViewById(R.id.tv);
  83.             tv.setText("Hello World " + position);
  84.             return view;
  85.         }

  86.     }

  87.     @Override
  88.     public void onScroll(AbsListView view, int firstVisibleItem,
  89.             int visibleItemCount, int totalItemCount) {
  90.         this.visibleItemCount = visibleItemCount;
  91.         lastItem = firstVisibleItem + visibleItemCount - 1;
  92.         System.out.println(listAdapter.count);
  93.         if (listAdapter.count >= count) {
  94.             list.removeFooterView(view);
  95.         }
  96.     }

  97.     @Override
  98.     public void onScrollStateChanged(AbsListView view, int scrollState) {
  99.         this.scrollState = scrollState;

  100.     }
  101. }
复制代码
由于跟之前那篇原理一样的,所以没写注释,不懂的先看前面那篇吧 http://www.cnblogs.com/and_he/archive/2011/05/30/2063230.html
效果图
1.jpg 2.jpg 3.jpg 4.jpg
2012-1-5 01:50 上传
下载附件 (73.67 KB)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值