一、开发背景
实现类似“膜拜”app里面的行车日志的功能
二、看实现后的效果
三、分析:
刚刚看到这个需求的时候,是不是也感觉跟我一样,不知道怎么入手。我们把分析图先画出来。如上图,实现的方法是listView里面嵌套我们需要的一个布局。这样我们就能够搞定高度随着我们内容的不同而自适应高度了。
四、代码部分
1. 先写两个布局
(1)对应Activity的布局activity_log_list.xml;
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_log_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/me_bg" tools:context="cn.bql.vehiclemounted.vehiclemounteds.activity.LogListActivity"> //这个是title,可不管 <include android:id="@+id/include" layout="@layout/public_title" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/include"> <ListView android:id="@+id/listViewTime" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/textView5" android:divider="@null" android:padding="5dp" /> <ProgressBar android:id="@+id/probarLog" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerHorizontal="true" android:layout_marginTop="350dp" /> <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" /> </RelativeLayout> //下边的按钮 <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:background="#000" android:gravity="center" android:visibility="gone"> <TextView android:id="@+id/addLog" android:layout_width="80dp" android:layout_height="35dp" android:layout_gravity="center" android:background="@drawable/selector_btn_lvse" android:gravity="center" android:text="添加日志" android:textSize="16sp" /> </LinearLayout> </RelativeLayout>2. 需要填充在listView 适配器adapter的布局
<?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:orientation="horizontal" android:weightSum="5"> //日期 <TextView android:id="@+id/tv_timelog" android:layout_width="45dp" android:layout_height="40dp" android:layout_marginTop="15dp" android:gravity="center" android:textColor="#686A6F" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent"> //竖的线条 <View android:id="@+id/shiguangzhou1" android:layout_width="1dp" android:layout_height="15dp" android:layout_marginLeft="20dp" android:background="#2E96BB" /> //光轴的圆形图标 <ImageView android:layout_below="@id/shiguangzhou1" android:id="@+id/imageView1" android:layout_width="35dp" android:layout_height="35dp" android:background="@mipmap/ic_baoyang_icon" /> //竖的线条 <View android:id="@+id/shiguangzhou" android:layout_below="@id/imageView1" android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:background="#2E96BB" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginTop="15dp" android:background="@drawable/log_bg" android:orientation="vertical" android:padding="5dp"> //title数据 <TextView android:textColor="@color/et_clolr" android:id="@+id/tv_title_project" android:layout_width="match_parent" android:layout_height="match_parent" android:text="养护了很多项" android:textSize="14sp" /> //数据内容 <TextView android:layout_marginTop="5dp" android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="@string/app_name" android:textSize="13sp" android:textColor="#686A6F" /> </LinearLayout> </LinearLayout>1.LogListActivity方法
/** * 日志列表显示 */ public class LogListActivity extends BaseActivity { /** * 自定义适配器 */ public class MyAdapter extends BaseAdapter { private LayoutInflater layoutInflater; public final class ViewHolder { //自定义控件集合 public TextView tv_timelog; public ImageView imageView; public TextView tv_content; public TextView tv_title; } public MyAdapter(Context context) { this.layoutInflater = LayoutInflater.from(context); } @Override public int getCount() { return carLogBeaninfo == null ? 0 : carLogBeaninfo.getCount(); } @Override public Object getItem(int position) { return carLogBeaninfo.getDay_data().get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { //自定义视图 ViewHolder holder = null; if (view == null) { holder = new ViewHolder(); //获取list_item布局文件的视图 view = layoutInflater.inflate(R.layout.group_status, null); //获取控件对象 holder.tv_timelog = (TextView) view.findViewById(R.id.tv_timelog); holder.imageView = (ImageView) view.findViewById(R.id.imageView1); holder.tv_content = (TextView) view.findViewById(R.id.tv_content); holder.tv_title = (TextView) view.findViewById(R.id.tv_title_project); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } String time = carLogBeaninfo.getDay_data().get(position).getCare_date(); String[] sourceStrArray = time.split("-"); String times = sourceStrArray[0] + "\n" + sourceStrArray[1] + "." + sourceStrArray[2]; holder.tv_timelog.setText(times); Glide.with(mContext).load(R.mipmap.ic_baoyang_icon).into(holder.imageView); String s = ""; for (int i = 0; i < carLogBeaninfo.getDay_data().get(position).getCount(); i++) { s = s + (i+1) + "."+carLogBeaninfo.getDay_data().get(position) .getCare_data().get(i).getContent() + ";" + "\n"; } holder.tv_content.setText(s.trim() + ""); String title = "您做了 " + carLogBeaninfo.getDay_data().get(position) .getCare_data().get(0).getContent() +" 等" + carLogBeaninfo.getDay_data().get(position).getCount() + "个保养项"; holder.tv_title.setText(title); return view; } } }这里面主要看适配器即可,数据请求需要根据自己的项目需求自己去获取即可。 总结:也就是说,时光轴的难点其实就是布局这一块,了解了只要基础不是太差,都能够完成。