android 时间轴 (可动态添加时间轴的内部内容,没实现删除功能)

一般时间轴效果都可以用ListView来实现,下面实现的是,每个时间轴中的内容都可以动态添加。(也是使用ListView实现)


同样,时间轴每一段就是一个Item,Item代码如下:
   
   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
 
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
android:gravity="center"
android:text="出生时" />
 
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7">
 
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#0f0fff" />
 
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/umeng_socialize_location_ic" />
 
</RelativeLayout>
 
 
<LinearLayout
android:id="@+id/linear_vaccine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="2"
android:background="#56f22f66"
android:orientation="vertical" />
 
</LinearLayout>
右边可以动态添加内容的区域,使用了LinearLayout。(到时候通过代码找出Item的LinearLayout,然后通过addView()方法添加内容)

       《二》
通过上面效果图可以看到,每个Item里面的内容也可以看作为一个Item,把它封装起来成一个自定义View,代码如下:
   
   
<?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">
 
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
 
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/text2"
android:gravity="center"
android:text="卡介苗" />
 
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/checkbox1"
android:text="2015-12-25" />
 
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
    
    
 
import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
 
 
public class MyVaccineView extends LinearLayout {
 
TextView textView1;
TextView textView2;
CheckBox checkBox1;
 
 
public MyVaccineView(Context context) {
this(context, null, 0);
}
 
public MyVaccineView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
 
public MyVaccineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
 
public void init(Context context) {
inflate(context, R.layout.layout_list_item, this);
 
textView1 = (TextView) findViewById(R.id.text1);
textView2 = (TextView) findViewById(R.id.text2);
checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
}
 
 
public void setText1(String text1) {
textView1.setText(text1);
}
 
public void setText2(String text2) {
textView2.setText(text2);
}
 
public void setcheck(boolean is) {
if (is) {
checkBox1.setChecked(true);
} else {
checkBox1.setChecked(false);
}
}
 
 
}

封装起来的bean:
     
     
public class vaccine {
 
String name;
String time;
boolean isTrue;
 
public vaccine(boolean isTrue, String time, String name) {
this.isTrue = isTrue;
this.time = time;
this.name = name;
}
 
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 boolean isTrue() {
return isTrue;
}
 
public void setIsTrue(boolean isTrue) {
this.isTrue = isTrue;
}
 
}
      
      
public class Vaccines {
 
String month;
List<vaccine> vaccines;
 
public Vaccines(String month, List<vaccine> vaccines) {
this.month = month;
this.vaccines = vaccines;
}
 
public List<vaccine> getVaccines() {
return vaccines;
}
 
public void setVaccines(List<vaccine> vaccines) {
this.vaccines = vaccines;
}
 
public String getMonth() {
return month;
}
 
public void setMonth(String month) {
this.month = month;
}
}
模拟数据先放在Appcation上:
     
     
 
import android.app.Application;
import android.util.Log;
 
import java.util.ArrayList;
import java.util.List;
 
 
public class APp extends Application {
 
 
List<Vaccines> mVaccines;
Vaccines mvaccine0;
 
 
List<vaccine> vs;
List<vaccine> vs1;
List<vaccine> vs2;
List<vaccine> vs3;
List<vaccine> vs4;
List<vaccine> vs5;
 
vaccine vaccine;
vaccine vaccine2;
vaccine vaccine3;
vaccine vaccine4;
vaccine vaccine5;
vaccine vaccine6;
vaccine vaccine7;
 
@Override
public void onCreate() {
super.onCreate();
 
vs = new ArrayList<vaccine>();
vs1 = new ArrayList<vaccine>();
vs2 = new ArrayList<vaccine>();
vs3 = new ArrayList<vaccine>();
vs4 = new ArrayList<vaccine>();
vs5 = new ArrayList<vaccine>();
 
mVaccines = new ArrayList<Vaccines>();
 
vaccine = new vaccine(true, "()+", "time+");
vaccine2 = new vaccine(false, "asdas", "12313");
vaccine3 = new vaccine(true, "三赛段", "asda");
vaccine4 = new vaccine(true, "萨达", "xasdas");
vaccine5 = new vaccine(true, "下次", "666");
vaccine6 = new vaccine(true, "23是", "按时");
vaccine7 = new vaccine(true, "个热狗", "5214");
 
vs.add(vaccine);
vs1.add(vaccine);
vs1.add(vaccine4);
vs2.add(vaccine4);
vs2.add(vaccine6);
vs2.add(vaccine2);
vs3.add(vaccine7);
vs4.add(vaccine6);
vs4.add(vaccine3);
vs5.add(vaccine2);
vs5.add(vaccine3);
vs5.add(vaccine4);
 
 
mvaccine0 = new Vaccines("出生时", vs);
mVaccines.add(mvaccine0);
mvaccine0 = new Vaccines("1个月", vs1);
mVaccines.add(mvaccine0);
mvaccine0 = new Vaccines("2个月", vs2);
mVaccines.add(mvaccine0);
mvaccine0 = new Vaccines("3个月", vs3);
mVaccines.add(mvaccine0);
mvaccine0 = new Vaccines("4个月", vs4);
mVaccines.add(mvaccine0);
mvaccine0 = new Vaccines("5个月", vs5);
mVaccines.add(mvaccine0);
 
}
 
public List<Vaccines> getVaccines() {
return mVaccines;
}
 
public void setmVaccines(List<Vaccines> mV) {
mVaccines = mV;
}
 
public void addvs(int position, vaccine vaccine) {
Vaccines mvaccine = mVaccines.get(position);
List<vaccine> vs = mvaccine.getVaccines();
vs.add(vaccine);
// Log.d("com,huangqiyu", "修改后--》" + vs.size());
mvaccine.setVaccines(vs);
mVaccines.set(position, mvaccine);
 
// Log.d("com.huangqiyu", "修改后vs1=" + mVaccines.get(0).getVaccines().size());
// Log.d("com.huangqiyu", "修改后vs2=" + mVaccines.get(1).getVaccines().size());
// Log.d("com.huangqiyu", "修改后vs3=" + mVaccines.get(2).getVaccines().size());
// Log.d("com.huangqiyu", "修改后vs4=" + mVaccines.get(3).getVaccines().size());
// Log.d("com.huangqiyu", "修改后vs5=" + mVaccines.get(4).getVaccines().size());
// Log.d("com.huangqiyu", "修改后vs6=" + mVaccines.get(5).getVaccines().size());
 
}
 
}
注:Appcation上数据比较繁杂,因为每一个时间轴Item上的动态内容不能使用相同的数据对象集合,否则出现在其中一个时间添加内容,其他使用相同数据对象集合也会自动添加。(个人也不是很明白为什么会这样)


《三》
接下来就是实现功能了,布局之前说过了,使用ListView :
    
    
<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
tools:context="com.example.huangqiyu.myapplication.TestActivity">
 
 
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" />
 
</LinearLayout>
     
     
package com.example.huangqiyu.myapplication;
 
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.List;
 
public class TestActivity extends AppCompatActivity {
 
ListView mListView;
MyAdapter adapter;
List<Vaccines> mVaccines;
 
APp app;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mListView = (ListView) findViewById(R.id.listView);
 
app = (APp) getApplication();
 
mVaccines = app.getVaccines();
// Log.d("com.huangqiyu", "mva=" + mVaccines.size());
// Log.d("com.huangqiyu", "vs1=" + mVaccines.get(0).getVaccines().size());
// Log.d("com.huangqiyu", "vs2=" + mVaccines.get(1).getVaccines().size());
// Log.d("com.huangqiyu", "vs3=" + mVaccines.get(2).getVaccines().size());
// Log.d("com.huangqiyu", "vs4=" + mVaccines.get(3).getVaccines().size());
// Log.d("com.huangqiyu", "vs5=" + mVaccines.get(4).getVaccines().size());
// Log.d("com.huangqiyu", "vs6=" + mVaccines.get(5).getVaccines().size());
 
adapter = new MyAdapter(mVaccines);
mListView.setAdapter(adapter);
 
}
 
 
public class MyAdapter extends BaseAdapter {
 
List<Vaccines> mVaccines;
MyVaccineView view1;
ViewHolder holder;
List<ViewHolder> viewHolders = new ArrayList<ViewHolder>();
 
public MyAdapter(List<Vaccines> vaccines) {
this.mVaccines = vaccines;
}
 
@Override
public int getCount() {
return mVaccines.size();
}
 
@Override
public Object getItem(int position) {
return mVaccines.get(position);
}
 
@Override
public long getItemId(int position) {
return position;
}
 
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
List<vaccine> vs = null;
if (convertView == null) {
convertView = LayoutInflater.from(TestActivity.this).inflate(R.layout.layout_list, null);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.text1);
holder.imageView = (ImageView) convertView.findViewById(R.id.image);
holder.linear = (LinearLayout) convertView.findViewById(R.id.linear_vaccine);
 
viewHolders.add(holder);
 
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TestActivity.this, "" + position, 0).show();
 
initLinear(position, new vaccine(false, "大蛇丸", "2015-12-2"), viewHolders.get(position).linear);
}
});
vs = mVaccines.get(position).getVaccines();
 
for (int i = 0; i < vs.size(); i++) {
view1 = new MyVaccineView(TestActivity.this);
view1.setText1(vs.get(i).getName());
view1.setText2(vs.get(i).getTime());
view1.setcheck(vs.get(i).isTrue());
holder.linear.addView(view1);
}
 
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
 
holder.textView1.setText(mVaccines.get(position).getMonth());
 
return convertView;
}
 
 
public void initLinear(int position, vaccine vs, LinearLayout linear) {
 
// Log.d("com.huangqiyu", "调用了--》" + position);
 
app.addvs(position, vs);
 
view1 = new MyVaccineView(TestActivity.this);
view1.setText1(vs.getName());
view1.setText2(vs.getTime());
view1.setcheck(vs.isTrue());
linear.addView(view1);
 
notifyDataSetChanged();
 
}
 
class ViewHolder {
TextView textView1;
ImageView imageView;
LinearLayout linear;
}
}
 
}
在这个Demo中,把Adapter作为一个内部类来实现,像平常ListView一样设置Adapter,Adapter的getView()方法中,下图为初始化时间轴每个Item上的内容:
同时给时间轴Item上的添加图标添加监听事件:

直接通过点击添加来添加内容,同时更新数据:

在这里要注意,要给ViewHolder对象添加一个集合保存起来,到时候通过点击时获取的position得到对应的ViewHolder对象(因为每个Item上的LinearLayout都通过ViewHolder来保存的),这样内容才能准确添加到对应的Item上。

(其实上面那个方法有Bug的,现在新方法是在Adapter中AddView前先removeAllView就行了,这样简单方便)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值