首先,这篇参考了别人的代码。根据自己的项目需求简单改造了一下,效果图如下
xml:代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/lv_list" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:cacheColorHint="@null" 12 android:divider="@null" > 13 </ListView> 14 15 </LinearLayout>
接下来是Activity,准备数据就好了
1 public class TimeLineTextActivity extends Activity{ 2 3 private ListView listView; 4 private TimeLineAdapter adapter; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 // TODO Auto-generated method stub 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 listView=(ListView) findViewById(R.id.lv_list); 13 listView.setDividerHeight(0); 14 adapter = new TimeLineAdapter(this, initData()); 15 listView.setAdapter(adapter); 16 17 } 18 19 private List<Map<String, Object>> initData() { 20 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 21 22 Map<String, Object> map = new HashMap<String, Object>(); 23 map.put("title", "提交已完成......"); 24 map.put("time", "2015-10-22 14:00:00"); 25 list.add(map); 26 27 map = new HashMap<String, Object>(); 28 map.put("title", "正在审核中......"); 29 map.put("time", "2015-10-22 15:00:00"); 30 list.add(map); 31 32 map = new HashMap<String, Object>(); 33 map.put("title", "客服将会给您打电话......"); 34 map.put("time", "2015-10-22 16:00:00"); 35 list.add(map); 36 37 map = new HashMap<String, Object>(); 38 map.put("title", "订单已完成"); 39 map.put("time", "2015-10-22 17:00:00"); 40 list.add(map); 41 42 return list; 43 44 } 45 46 47 }
Adapter:
1 public class TimeLineAdapter extends BaseAdapter { 2 private Context context; 3 private List<Map<String,Object>> list; 4 private LayoutInflater inflater; 5 6 public TimeLineAdapter(Context context, List<Map<String, Object>> list) { 7 super(); 8 this.context = context; 9 this.list = list; 10 } 11 12 @Override 13 public int getCount() { 14 // TODO Auto-generated method stub 15 return list.size(); 16 } 17 18 @Override 19 public Object getItem(int position) { 20 // TODO Auto-generated method stub 21 return position; 22 } 23 24 @Override 25 public long getItemId(int position) { 26 // TODO Auto-generated method stub 27 return position; 28 } 29 30 @Override 31 public View getView(int position, View convertView, ViewGroup parent) { 32 // TODO Auto-generated method stub 33 TimeLineHolder viewHolder = null; 34 if (convertView == null) { 35 inflater = LayoutInflater.from(parent.getContext()); 36 convertView = inflater.inflate(R.layout.itemtimeline2, null); 37 viewHolder = new TimeLineHolder(); 38 39 viewHolder.title = (TextView) convertView.findViewById(R.id.title); 40 viewHolder.time = (TextView) convertView.findViewById(R.id.time); 41 convertView.setTag(viewHolder); 42 } else { 43 viewHolder = (TimeLineHolder) convertView.getTag(); 44 } 45 46 String titleStr = list.get(position).get("title").toString(); 47 48 49 viewHolder.title.setText(titleStr); 50 51 return convertView; 52 53 } 54 55 static class TimeLineHolder{ 56 private TextView title,time; 57 } 58 59 }
每一个item的布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 8 9 <View 10 android:id="@+id/view_0" 11 android:layout_width="1dp" 12 android:layout_height="25dp" 13 android:layout_below="@+id/layout_1" 14 android:layout_marginLeft="40dp" 15 android:background="#A6A6A6" /> 16 <ImageView 17 android:id="@+id/image" 18 android:layout_width="15dp" 19 android:layout_height="15dp" 20 android:layout_below="@+id/view_0" 21 android:layout_marginLeft="33dp" 22 android:src="@drawable/timeline_green" /> 23 <View 24 android:id="@+id/view_2" 25 android:layout_width="1dp" 26 android:layout_height="50dp" 27 android:layout_below="@+id/image" 28 android:layout_marginLeft="40dp" 29 android:background="#A6A6A6" /> 30 31 <View 32 android:id="@+id/view_4" 33 android:layout_width="match_parent" 34 android:layout_height="1dp" 35 android:layout_alignBottom="@+id/view_2" 36 android:layout_marginLeft="55dp" 37 android:layout_marginRight="15dp" 38 android:background="#A6A6A6" /> 39 40 <RelativeLayout 41 android:id="@+id/relative" 42 android:layout_width="fill_parent" 43 android:layout_height="match_parent" 44 android:layout_margin="10dp" 45 android:layout_toRightOf="@+id/view_0" 46 android:layout_alignBottom="@+id/view_4" 47 android:padding="5dp" 48 android:orientation="vertical" > 49 50 51 52 <TextView 53 android:id="@+id/title" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" 56 android:ellipsize="end" 57 android:layout_marginTop="8dp" 58 android:maxEms="7" 59 android:paddingLeft="5dp" 60 android:singleLine="true" 61 android:text="需求提交成功" 62 android:textSize="16sp" /> 63 <TextView 64 android:id="@+id/time" 65 android:layout_width="match_parent" 66 android:layout_height="wrap_content" 67 android:ellipsize="end" 68 android:layout_below="@+id/title" 69 android:layout_marginTop="15dp" 70 android:maxEms="7" 71 android:paddingLeft="5dp" 72 android:singleLine="true" 73 android:text="2015-9-28" 74 android:textSize="14sp" /> 75 76 </RelativeLayout> 77 78 79 80 81 </RelativeLayout>
不看、不学不知道,原来这个东西看起来复杂,实际上挺简单的,就是一个ListView,希望对大家有帮助!
时间: 2024-12-12 16:55:42