标签:
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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/Iv"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="50sp"
android:layout_height="50sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 权重分配位置,建议使用Relativelayout -->
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:layout_weight="5"
android:textSize="20sp"/>
<TextView
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1分钟前"/>
</LinearLayout>
<TextView
android:id="@+id/Contents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试内容区域"
android:layout_marginTop="5dp"/>
</LinearLayout>
</LinearLayout>
2.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffa500">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:text="@string/title"
android:textSize="15sp"/>
</LinearLayout>
<ListView
android:id="@+id/Lv"
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView=mInflater.from(mContext).inflate(R.layout.listview_item,null);
holder=new ViewHolder();
//实例化控件
holder.Image=(ImageView)convertView.findViewById(R.id.Iv);
holder.Name=(TextView)convertView.findViewById(R.id.Name);
holder.Time=(TextView)convertView.findViewById(R.id.Time);
holder.Contents=(TextView)convertView.findViewById(R.id.Contents);
convertView.setTag(holder); //设置setTag使得程序得以优化
}else{
holder=(ViewHolder) convertView.getTag();
}
//给控件赋值
holder.Image.setBackgroundResource(list.get(position).getImgId());
holder.Name.setText(list.get(position).getName());
holder.Time.setText(list.get(position).getDate());
holder.Contents.setText(list.get(position).getContent());
return convertView;
}
class ViewHolder{
ImageView Image;
TextView Name;
TextView Time;
TextView Contents;
}
}
package cn.edu.bzu.weibo_looking;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bzu.adapter.MyAdapter;
import cn.edu.bzu.entity.Information;
import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity;
public class MainActivity extends Activity {
private ListView listView;
private MyAdapter adapter;
private List<Information> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData();//数据源
initView();
}
private void initView() {
// 初始化控件
listView=(ListView) findViewById(R.id.Lv);
listView.setAdapter(adapter); //绑定适配器
}
private void setData() {
// 设置数据源
list=new ArrayList<Information>();
list.add(new Information(R.drawable.c,"xiaohong","2015 4 28","天然卷的家伙都不坏。"));
list.add(new Information(R.drawable.a,"hehe","2015 4 28","曾经沧海难为水,除却巫山不是云。"));
list.add(new Information(R.drawable.img1,"zhiqian","2015 4 28","在学校和生活中,工作的最重要的动力是工作中的乐趣,是工作获得结果时的乐趣以及对这个结果的社会价值的认识"));
list.add(new Information(R.drawable.img4,"xiaolu","2015 4 28","要永远觉得祖国的土地是稳固地在你脚下,要与集体一起生活,要记住,是集体教育了你。那一天你若和集体脱离,那便是末路的开始。"));
list.add(new Information(R.drawable.img2,"xiaoxiao","2015 4 28","寂寞空庭春欲晚,梨花满地不开门"));
adapter=new MyAdapter(list, MainActivity.this);
}
}
标签:
原文地址:http://www.cnblogs.com/aleale/p/4496546.html