【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
效果图

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇