************模拟新浪微博动态*************************
1.项目效果图
主要代码:
代码如下:
<?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:padding="10dip"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/head_img"
android:src="@drawable/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_alignParentLeft="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/head_img"
android:layout_marginLeft="7dip"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="#FF6666"
android:textSize="16sp"
android:typeface="sans"
android:textStyle="bold"
android:text="赵迪忠"/>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/name"
android:layout_alignBottom="@id/name"
android:layout_marginLeft="5dip"
android:textColor="#696969"
android:textSize="12sp"
android:text="12:11"/>
<TextView
android:id="@+id/rq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="9sp"
android:text="123人气"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<TextView
android:id="@+id/article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50px"
android:textSize="12sp"
android:text=" 我是西伯利亚狼,坑蒙拐骗偷,吃喝嫖赌抽样样不精通。我和比尔盖茨是同行。人生自古谁无死,留取丹心照汗青 "/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
2、获取xml资源:在android开发工具中,包bzu、edu中的MainActivity中的getData。
private
void
getData
()
{
// TODO Auto-generated method stub
//获得xml的资源
String
names
[];
String
article
[];
TypedArray
img
;
int
i
;
names
=
getResources
().
getStringArray
(
R
.
array
.
name
);
article
=
getResources
().
getStringArray
(
R
.
array
.
article
);
img
=
getResources
().
obtainTypedArray
(
R
.
array
.
head_photo
);
list_msg
=
new
ArrayList
<
Messages
>();
for
(
i
=
0
;
i
<
names
.
length
;
i
++){
Messages
message
=
new
Messages
();
message
.
setImg
(
img
.
getDrawable
(
i
));
message
.
setName
(
names
[
i
]);
message
.
setArticel
(
article
[
i
]);
message
.
setRq
(
"人气:"
+
String
.
valueOf
(
new
Random
().
nextInt
(
1000
)));
Date
date
=
new
Date
();
SimpleDateFormat
simple
=
new
SimpleDateFormat
(
"MM-dd"
);
message
.
setTime
(
simple
.
format
(
date
));
list_msg
.
add
(
message
);
}
}
private
void
init
()
{
// TODO Auto-generated method stub
mylistview
=(
MyListView
)
findViewById
(
R
.
id
.
myListView1
);
// mylistview=new MyListView(this);
// LinearLayout linear=(LinearLayout)MainActivity.this.findViewById(R.id.liner);
// linear.addView(mylistview);
}
3,编写ArticleAdapter类。它继承BaseAdapter类,重写其方法,在getView方法中是加载了list_layout.xml布局,并将List中的每一条数据都对应布局中的控件,这样就显示出来随便看看的数据了
private List<Messages> list_mes=null;
private Context context;
public ArticleAdapter(Context context,List<Messages> list_mes) {
// TODO Auto-generated constructor stub
this.list_mes=list_mes;
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list_mes.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_mes.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) {
// TODO Auto-generated method stub
ListItem listitem;
if(convertView==null)
{
convertView=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.list_layout, null);
listitem=new ListItem();
listitem.img=(ImageView)convertView.findViewById(R.id.head_img);
listitem.name=(TextView)convertView.findViewById(R.id.name);
listitem.time=(TextView)convertView.findViewById(R.id.time);
listitem.rq=(TextView)convertView.findViewById(R.id.rq);
listitem.article=(TextView)convertView.findViewById(R.id.article);
convertView.setTag(listitem);
}else {
listitem=(ListItem)convertView.getTag();
}
listitem.img.setImageDrawable(list_mes.get(position).getImg());
listitem.name.setText(list_mes.get(position).getName());
listitem.time.setText(list_mes.get(position).getTime());
listitem.rq.setText(list_mes.get(position).getRq());
listitem.article.setText(list_mes.get(position).getArticel());
return convertView;
}
public class ListItem{
ImageView img;
TextView name;
TextView time;
TextView rq;
TextView article;
}
}
private Drawable img;//图片
private String name;//姓名
private String time;//时间
private String rq;//riqi
private String articel;//文字
public Drawable getImg() {
return img;
}
public void setImg(Drawable img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRq() {
return rq;
}
public void setRq(String rq) {
this.rq = rq;
}
public String getArticel() {
return articel;
}
public void setArticel(String articel) {
this.articel = articel;
}
}