您的位置:首页 > 其它

模拟新浪微博随便看看栏目

2015-05-04 21:58 337 查看

MyBlogger

效果图





开发步骤

01 拖选 ListView textView imageView 等组件 用来接受单一博客消息的标题、 头像、姓名、 时间 和内容

<TextView
android:id="@+id/tvTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:background="#AA7755"
android:text="@string/tvTop" />


<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/userName"
android:textColor="@android:color/holo_red_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/time"
android:layout_marginLeft="120dp"
android:textColor="@android:color/holo_green_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>


<ListView
android:id="@+id/myLIstView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>


02 建立实体类

public class Info {
private Integer image;
private String userName;
private String time;
private String title;
public Integer getImage() {
return image;
}
public void setImage(Integer image) {
this.image = image;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Info(Integer image, String userName, String time, String title) {
super();
this.image = image;
this.userName = userName;
this.time = time;
this.title = title;
}
public Info(){
super();
}

}


03 定义所需要的数据 头像、姓名、发表博客的时间、消息内容

private Integer[] image={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,};
private String[] name={"张三","李四","王五","孙六","王胖胖","成成","可可","龙龙","小张","小王"};
private String[] times={"2013-11-16","2014-08-11","2014-12-30","2012-09-12","2013-05-25","2013-11-16","2014-08-11","2014-12-30","2015-03-23","2015-05-04"};
private  String[] titles={"习近平会见中国国民党主席朱立伦","河南纪委去年‘拍蝇’3800只 近一半是村支书",
"男子银行卡被盗刷5万余元银行拒赔 起诉获判全赔","美国女子天津学艺回国热卖煎饼果子 一个卖36元",
"女子不顾全家反对嫁给潦倒男子 离婚后遭当街割喉","足球题材剧本被指易审批 观众或陷足球神剧漩涡",
"成都警方回应男子逼停殴打女司机:已被刑拘","教育部公布6个学历认证假中介 开通举报电话",
"程维高之子否认是逃犯 港媒再曝三官员藏加拿大","科学家:尼泊尔地震致珠峰高度降低2.5厘米"};


04 定义一个适配器 ,继承BaseAdapter并重写构造方法和getView方法

class MyListAdapter extends BaseAdapter{

private LayoutInflater inflater;
private Context context=null;
private List<Info> list;

MyListAdapter(Context context,List<Info> list){
this.context=context;
this.list=list;
inflater=LayoutInflater.from(this.context);
}


public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView indexImage;
TextView indexNames;
TextView indexTimes;
TextView indexTitles;
if(convertView==null){
convertView=inflater.inflate(R.layout.list_view,null);
}
indexImage=(ImageView)convertView.findViewById(R.id.image);
indexNames=(TextView)convertView.findViewById(R.id.userName);
indexTimes=(TextView)convertView.findViewById(R.id.time);
indexTitles=(TextView)convertView.findViewById(R.id.title);
indexImage.setBackgroundResource(list.get(position).getImage());

indexNames.setText(list.get(position).getUserName());
indexTimes.setText(list.get(position).getTime());
indexTitles.setText(list.get(position).getTitle());

return convertView;
}
}


05 定义arrayList泛型,用来存储和添加数据

list=new ArrayList<Info>();
for(int i=0;i<10;i++){
Info in=new Info();
in.setImage(image[i]);
in.setTime(times[i]);
in.setUserName(name[i]);a
in.setTitle(titles[i]);
list.add(in);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: