模拟新浪微博随便看看
来源:互联网 发布:广场舞网络一线牵 编辑:程序博客网 时间:2024/06/06 04:55
1.导语
这几天学会了ListView组件,这个组件真的很有用。希望在今后的开发中能大量用到。
通过ListView的学习,模拟了新浪微博的随便看看页面。
程序效果如下:
2.代码分析
此文件自定义一个adapter,继承自BaseAdapter,实现getView方法,在此方法中定义了每个item的显示样式。
知识点:
- 建立数据源
- 数据源与适配器建立关系
- AdapterView
- 创建一个自定义的适配器
3.xml布局代码
步骤:
我们先添加两个xml文件:
activity_main.xml
代码如下:
01.<span style="font-family:Comic Sans MS;font-size:12px;"><?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. android:id="@+id/liner" 04. android:layout_width="fill_parent" 05. android:layout_height="fill_parent" 06. android:orientation="vertical" > 07. <LinearLayout 08. android:layout_width="match_parent" 09. android:layout_height="wrap_content" 10. android:padding="6dip" 11. android:background="#CD4F39"> 12. <TextView 13. android:layout_width="wrap_content" 14. android:layout_height="wrap_content" 15. android:text="新浪微博-随便看看" 16. android:textSize="15sp" 17. 18. android:textColor="@android:color/white" 19. /> 20. </LinearLayout> 21. 22. <com.bzu.edu.gxs.view.MyListView 23. android:id="@+id/myListView1" 24. android:layout_width="match_parent" 25. android:layout_height="wrap_content" > 26. </com.bzu.edu.gxs.view.MyListView> 27.</LinearLayout></span>
layout_view.xml
代码如下:
01.<span style="font-family:Comic Sans MS;font-size:12px;"><?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. android:layout_width="match_parent" 04. android:layout_height="match_parent" 05. android:padding="10dip" 06. android:orientation="vertical" > 07. <RelativeLayout 08. android:layout_width="match_parent" 09. android:layout_height="wrap_content" 10. > 11. <ImageView 12. android:id="@+id/head_img" 13. android:src="@drawable/p1" 14. android:layout_width="wrap_content" 15. android:layout_height="wrap_content" 16. android:layout_marginTop="5dip" 17. android:layout_alignParentLeft="true" 18. /> 19. <LinearLayout 20. android:layout_width="match_parent" 21. android:layout_height="wrap_content" 22. android:layout_toRightOf="@+id/head_img" 23. android:layout_marginLeft="7dip" 24. android:orientation="vertical"> 25. <RelativeLayout 26. android:layout_width="match_parent" 27. android:layout_height="wrap_content"> 28. <TextView 29. android:id="@+id/name" 30. android:layout_width="wrap_content" 31. android:layout_height="wrap_content" 32. android:layout_alignParentLeft="true" 33. android:textColor="#242424" 34. android:textSize="14sp" 35. android:typeface="sans" 36. android:textStyle="bold" 37. android:text="@string/gxs"/> 38. <TextView 39. android:id="@+id/time" 40. android:layout_width="wrap_content" 41. android:layout_height="wrap_content" 42. android:layout_toRightOf="@id/name" 43. android:layout_alignBottom="@id/name" 44. android:layout_marginLeft="5dip" 45. android:textColor="#FFFFFF" 46. android:textSize="12sp" 47. android:text=""/> 48. 49. <TextView 50. android:id="@+id/rq" 51. android:layout_width="wrap_content" 52. android:layout_height="wrap_content" 53. android:layout_alignParentRight="true" 54. android:layout_centerVertical="true" 55. android:textSize="13sp" 56. android:text="1分钟前"/> 57. </RelativeLayout> 58. <LinearLayout 59. android:layout_width="match_parent" 60. android:layout_height="wrap_content" 61. android:layout_marginTop="5dip"> 62. <TextView 63. android:id="@+id/article" 64. android:layout_width="match_parent" 65. android:layout_height="wrap_content" 66. android:minHeight="50px" 67. android:textSize="12sp" 68. android:text="gxs"/> 69. </LinearLayout> 70. </LinearLayout> 71. </RelativeLayout> 72. 73. 74.</LinearLayout> 75.</span>
MainActivity.java
01.<span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.gxs; 02. 03.import java.text.SimpleDateFormat; 04.import java.util.ArrayList; 05.import java.util.Date; 06.import java.util.List; 07.import java.util.Random; 08.import com.bzu.edu.gxs.adapter.ArticleAdapter; 09.import com.bzu.edu.gxs.adapter.Messages; 10.import com.bzu.edu.gxs.view.MyListView; 11.import com.bzu.gxs.*; 12.import android.R.integer; 13.import android.os.Bundle; 14.import android.os.Handler; 15.import android.os.Message; 16. 17. 18.import android.app.Activity; 19.import android.content.res.TypedArray; 20.import android.view.Menu; 21.import android.view.Window; 22.import android.widget.ArrayAdapter; 23.import android.widget.BaseAdapter; 24.import android.widget.LinearLayout; 25. 26.public class MainActivity extends Activity { 27. 28. private MyListView mylistview=null; 29. private List<Messages> list_msg=null; 30. private BaseAdapter adapter=null; 31. @Override 32. protected void onCreate(Bundle savedInstanceState) { 33. super.onCreate(savedInstanceState); 34. requestWindowFeature(Window.FEATURE_NO_TITLE); 35. setContentView(R.layout.activity_main); 36. 37. init(); 38. if(list_msg==null){ 39. getData(); 40. } 41. 42. adapter=new ArticleAdapter(this,list_msg); 43. //设置监听器 44. mylistview.setOnRershListener(new OnRershListener()); 45. mylistview.setAdapter(adapter); 46. } 47. 48. private void getData() { 49. // TODO Auto-generated method stub 50. //获得xml的资源 51. String names[]; 52. String article[]; 53. TypedArray img; 54. int i; 55. names=getResources().getStringArray(R.array.name); 56. article=getResources().getStringArray(R.array.article); 57. img=getResources().obtainTypedArray(R.array.head_photo); 58. 59. list_msg=new ArrayList<Messages>(); 60. for(i=0;i<names.length;i++){ 61. Messages message=new Messages(); 62. message.setImg(img.getDrawable(i)); 63. message.setName(names[i]); 64. message.setArticel(article[i]); 65. message.setRq(String.valueOf(new Random().nextInt(120)+"分钟前")); 66. Date date=new Date(); 67. SimpleDateFormat simple=new SimpleDateFormat("MM-dd"); 68. message.setTime(simple.format(date)); 69. list_msg.add(message); 70. } 71. } 72. private void init() { 73. // TODO Auto-generated method stub 74. mylistview=(MyListView)findViewById(R.id.myListView1); 75. } 76. 77. @Override 78. public boolean onCreateOptionsMenu(Menu menu) { 79. 80. getMenuInflater().inflate(R.menu.main, menu); 81. return true; 82. } 83. 84. 85. Handler handler=new Handler(new Handler.Callback() { 86. @Override 87. public boolean handleMessage(Message arg0) { 88. // TODO Auto-generated method stub 89. if(arg0.what==0){ 90. //结束刷新 91. mylistview.endOnRersh(); 92. } 93. return false; 94. } 95. }); 96. 97. //刷新接口实现类 98. class OnRershListener implements OnRersh{ 99. //实现刷新接口的 方法,,在MyList中会回调该方法。 100. //模拟 101. @Override 102. public void OnRershListener() { 103. // TODO Auto-generated method stub 104. new Thread(new Run()).start();//启动一个线程 105. } 106. } 107. 108. //线程接口实现类 109. class Run implements Runnable{ 110. @Override 111. public void run() { 112. // TODO Auto-generated method stub 113. try { 114. Thread.sleep(2000); 115. } catch (Exception e) { 116. // TODO: handle exception 117. e.printStackTrace(); 118. } 119. handler.sendEmptyMessage(0); 120. } 121. } 122. 123.} 124. 125.</span>
ArticleAdapter.java
01.<span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.edu.gxs.adapter; 02. 03.import java.util.List; 04.import com.bzu.gxs.*; 05.import android.content.Context; 06.import android.view.LayoutInflater; 07.import android.view.View; 08.import android.view.ViewGroup; 09.import android.widget.BaseAdapter; 10.import android.widget.ImageView; 11.import android.widget.LinearLayout; 12.import android.widget.TextView; 13. 14.public class ArticleAdapter extends BaseAdapter { 15. private List<Messages> list_mes=null; 16. private Context context; 17. 18. public ArticleAdapter(Context context,List<Messages> list_mes) { 19. // TODO Auto-generated constructor stub 20. this.list_mes=list_mes; 21. this.context=context; 22. } 23. 24. @Override 25. public int getCount() { 26. // TODO Auto-generated method stub 27. return list_mes.size(); 28. } 29. 30. @Override 31. public Object getItem(int position) { 32. // TODO Auto-generated method stub 33. return list_mes.get(position); 34. } 35. 36. @Override 37. public long getItemId(int position) { 38. // TODO Auto-generated method stub 39. return position; 40. } 41. 42. @Override 43. public View getView(int position, View convertView, ViewGroup parent) { 44. // TODO Auto-generated method stub 45. ListItem listitem; 46. if(convertView==null) 47. { 48. convertView=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.layout_view, null); 49. 50. listitem=new ListItem(); 51. listitem.img=(ImageView)convertView.findViewById(R.id.head_img); 52. listitem.name=(TextView)convertView.findViewById(R.id.name); 53. //显示当前日期 54. listitem.time=(TextView)convertView.findViewById(R.id.time); 55. listitem.rq=(TextView)convertView.findViewById(R.id.rq); 56. listitem.article=(TextView)convertView.findViewById(R.id.article); 57. convertView.setTag(listitem); 58. }else { 59. listitem=(ListItem)convertView.getTag(); 60. } 61. 62. listitem.img.setImageDrawable(list_mes.get(position).getImg()); 63. listitem.name.setText(list_mes.get(position).getName()); 64. listitem.time.setText(list_mes.get(position).getTime()); 65. listitem.rq.setText(list_mes.get(position).getRq()); 66. listitem.article.setText(list_mes.get(position).getArticel()); 67. return convertView; 68. } 69. 70. public class ListItem{ 71. ImageView img; 72. TextView name; 73. TextView time; 74. TextView rq; 75. TextView article; 76. } 77.} 78.</span> Messages.java[html] view plaincopyprint?01.<span style="font-family:Comic Sans MS;font-size:12px;">package com.bzu.edu.gxs.adapter; 02. 03.import android.graphics.drawable.Drawable; 04. 05.public class Messages { 06. private Drawable img; 07. private String name; 08. private String time; 09. private String rq; 10. private String articel; 11. 12. public Drawable getImg() { 13. return img; 14. } 15. public void setImg(Drawable img) { 16. this.img = img; 17. } 18. public String getName() { 19. return name; 20. } 21. public void setName(String name) { 22. this.name = name; 23. } 24. public String getTime() { 25. return time; 26. } 27. public void setTime(String time) { 28. this.time = time; 29. } 30. public String getRq() { 31. return rq; 32. } 33. public void setRq(String rq) { 34. this.rq = rq; 35. } 36. public String getArticel() { 37. return articel; 38. } 39. public void setArticel(String articel) { 40. this.articel = articel; 41. } 42. 43. 44.}</span><span style="font-family:Comic Sans MS;"> 45.</span>
总结:通过ListView的学习,了解了adapter适配器的使用。
0 0
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看
- 模拟新浪微博-随便看看
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看栏目
- 模拟新浪微博随便看看
- 模拟新浪微博随便看看栏目
- 用 union 将 单链表 模块化
- 安装dlib python接口
- 数据对齐
- Python函数式编程
- CF 340A The Wall
- 模拟新浪微博随便看看
- JSP——EL表达式
- 算法六:floyd-warshall和dijkstra
- 完美解决Android SDK Manager无法更新
- php调用webservice的几种方法
- Enea公司----OSE/OSEck RTOS
- 云端开源⾼性能技术架构调研分析报告
- 关于zabbix agent安装配置的一些心得
- RequestAnimationFrame更好的实现Javascript动画