Android 原生listview item伸展收缩效果

时间:2021-08-24 06:19:58
Android原生listview做的一个item的伸缩效果。
*永远不要让你老大有机会改需求 package com.example.yunkanglast; import java.io.Serializable; /**
* 状态类
*/
public class PurchasedVipInfo implements Serializable { public enum Type {
CHECK_BOX,
TEXT_VIEW,
NO_CONTENT,
NO_CONTENT_JUMP,
TEXT_VIEW_HEAD
} private String name;
private String desc;
private Type type;
private boolean isChecked;
private boolean isExpend;
private String price; public PurchasedVipInfo(String name, String desc, Type type, boolean isExpend) {
this.name = name;
this.desc = desc;
this.type = type;
this.isExpend = isExpend;
} public PurchasedVipInfo(String name, String desc, Type type, boolean isExpend,String price) {
this.name = name;
this.desc = desc;
this.type = type;
this.isExpend = isExpend;
this.price = price;
} public PurchasedVipInfo(String name, String desc, boolean isExpend) {
this.name = name;
this.desc = desc;
this.isExpend = isExpend;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} public Type getType() {
return type;
} public void setType(Type type) {
this.type = type;
} public boolean isChecked() {
return isChecked;
} public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
} public boolean isExpend() {
return isExpend;
} public void setIsExpend(boolean isExpend) {
this.isExpend = isExpend;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
}
}
package com.example.yunkanglast;

import android.app.Activity;
import android.content.Context; import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; public class RegistrationPurchasedVipActivity extends Activity { private ListView purchasedVipInfolistView;
private List<PurchasedVipInfo> purchasedVipInfolist;
private RegistrationPurchasedVipAdapter pVipAdapter; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration_purchased_vip);
purchasedVipInfolistView = (ListView) findViewById(R.id.lv_register_healthvip_list);
purchasedVipInfolistView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
});
initData();
} private void initData() {
purchasedVipInfolist = new ArrayList<PurchasedVipInfo>();
for (int i = 1; i < 5; i++) {
PurchasedVipInfo.Type type = PurchasedVipInfo.Type.TEXT_VIEW;
if (i == 0) {
type = PurchasedVipInfo.Type.NO_CONTENT;
} else if (i > 0 && i < 3) {
type = PurchasedVipInfo.Type.CHECK_BOX;
} else if (i == 3) {
type = PurchasedVipInfo.Type.NO_CONTENT_JUMP;
} else if (i == 4) {
type = PurchasedVipInfo.Type.TEXT_VIEW_HEAD;
}
}
purchasedVipInfolist
.add(new PurchasedVipInfo(
"预约挂号服务 ",
"客户可通过400热线、网站、手机客户端等多个渠道提供全国500多家医院的免费预约挂号服务。服务不限次数,部分医院限制同一就诊人每月不超过6次,以医院要求为准。 ",
PurchasedVipInfo.Type.TEXT_VIEW, false));
purchasedVipInfolist
.add(new PurchasedVipInfo(
"会员专享服务",
"客户可通过400热线、网站、手机客户端等多个渠道提供全国500多家医院的免费预约挂号服务。服务不限次数,部分医院限制同一就诊人每月不超过6次,以医院要求为准。 ",
PurchasedVipInfo.Type.TEXT_VIEW, false));
purchasedVipInfolist
.add(new PurchasedVipInfo(
"医生质询/导诊 ",
"客户可通过400热线、网站、手机客户端等多个渠道提供全国500多家医院的免费预约挂号服务。服务不限次数,部分医院限制同一就诊人每月不超过6次,以医院要求为准。 ",
PurchasedVipInfo.Type.TEXT_VIEW, false));
purchasedVipInfolist
.add(new PurchasedVipInfo(
"放号提醒 ",
"客户可通过400热线、网站、手机客户端等多个渠道提供全国500多家医院的免费预约挂号服务。服务不限次数,部分医院限制同一就诊人每月不超过6次,以医院要求为准。 ",
PurchasedVipInfo.Type.TEXT_VIEW, false));
pVipAdapter = new RegistrationPurchasedVipAdapter(RegistrationPurchasedVipActivity.this,
purchasedVipInfolist);
purchasedVipInfolistView.setAdapter(pVipAdapter); }
}
package com.example.yunkanglast;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class RegistrationPurchasedVipAdapter extends BaseAdapter { private Context context;
private List<PurchasedVipInfo> purchasedVipInfolist;
private LayoutInflater inflater; public RegistrationPurchasedVipAdapter(Context context,
List<PurchasedVipInfo> purchasedVipInfolist) {
this.context = context;
this.purchasedVipInfolist = purchasedVipInfolist;
this.inflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return purchasedVipInfolist.size();
} @Override
public Object getItem(int position) {
return purchasedVipInfolist.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final PurchasedVipInfo node = purchasedVipInfolist.get(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.registration_purchased_vip_listview, null);
holder.mTvDesc = (TextView) convertView
.findViewById(R.id.tv_register_healthvip_lists);
holder.mIvIcon = (ImageView) convertView
.findViewById(R.id.iv_register_healthvip_icon);
holder.mTvName = (TextView) convertView
.findViewById(R.id.tv_register_healthvip_listhead);
holder.mHead = convertView
.findViewById(R.id.linear_register_healthvip_list);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (node != null) {
PurchasedVipInfo.Type type = node.getType();
if (type == PurchasedVipInfo.Type.NO_CONTENT) {
holder.mHead.setVisibility(View.GONE);
holder.mTvDesc.setVisibility(View.GONE);
} else if (type == PurchasedVipInfo.Type.TEXT_VIEW) { holder.mTvName.setText(node.getName());
holder.mTvDesc.setText(node.getDesc()); holder.mHead.setVisibility(View.VISIBLE);
holder.mIvIcon.setBackgroundColor(node.isExpend() ? Color.GREEN
: Color.RED);
holder.mTvDesc.setVisibility(node.isExpend() ? View.VISIBLE
: View.GONE);
holder.mHead.setOnClickListener(new OnpVipClickListener(holder,
position)); } else if (type == PurchasedVipInfo.Type.CHECK_BOX) { holder.mTvName.setText(node.getName());
holder.mTvDesc.setText(node.getDesc()); holder.mHead.setVisibility(View.GONE);
holder.mIvIcon.setBackgroundColor(node.isExpend() ? Color.GREEN
: Color.RED);
holder.mTvDesc.setVisibility(node.isExpend() ? View.VISIBLE
: View.GONE); } else if (type == PurchasedVipInfo.Type.NO_CONTENT_JUMP) {
holder.mTvName.setText(node.getName());
holder.mTvDesc.setText(node.getDesc()); holder.mHead.setVisibility(View.VISIBLE);
holder.mIvIcon.setBackgroundColor(node.isExpend() ? Color.GREEN
: Color.RED);
holder.mTvDesc.setVisibility(node.isExpend() ? View.VISIBLE
: View.GONE);
holder.mHead.setOnClickListener(new OnpVipClickListener(holder,
position));
} else if (type == PurchasedVipInfo.Type.TEXT_VIEW_HEAD) {
holder.mHead.setVisibility(View.GONE);
}
}
return convertView;
} public class ViewHolder {
View mHead; // listview item部分的布局
TextView mTvName; // item 文字
TextView mTvDesc; // item 被隐藏的文字
ImageView mIvIcon; // item上面的指向图标
} public class OnpVipClickListener implements View.OnClickListener { private ViewHolder holder;
private int position; public OnpVipClickListener(ViewHolder holder, int position) {
this.holder = holder;
this.position = position;
} @Override
public void onClick(View v) {
PurchasedVipInfo node = purchasedVipInfolist.get(position);
if (node.isExpend()) {
holder.mTvDesc.setVisibility(View.GONE);
holder.mIvIcon.setImageResource(R.drawable.registration_icon_toright);
} else {
holder.mTvDesc.setVisibility(View.VISIBLE);
holder.mIvIcon.setImageResource(R.drawable.registration_icon_totop);
}
node.setIsExpend(!node.isExpend());
}
}
}

xml

<!--activity_registration_purchased_vip.xml-->
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="#ececec"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingBottom="25dp"
android:paddingLeft="28dp"
android:paddingRight="20dp"
android:paddingTop="34dp" > <TextView
android:id="@+id/tv_register_healthvip_utitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="尊敬的用户 "
android:textColor="#333333"
android:textSize="14sp" /> <TextView
android:id="@+id/tv_register_healthvip_uname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_register_healthvip_utitle"
android:layout_toRightOf="@+id/tv_register_healthvip_utitle"
android:text="Superman"
android:textColor="#ff5000"
android:textSize="14sp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_register_healthvip_utitle"
android:layout_toRightOf="@+id/tv_register_healthvip_uname"
android:text=":"
android:textColor="#333333"
android:textSize="14sp" /> <TextView
android:id="@+id/tv_register_healthvip_explain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_register_healthvip_utitle"
android:layout_below="@+id/tv_register_healthvip_utitle"
android:layout_marginTop="16dp"
android:lineSpacingExtra="11dp"
android:text="您已经是小白的健康月费会员,您可以享受小百健康为您提供的覆盖全国的预约挂号服务和专业医生咨询服务,小百努力为您提供高效便捷的就医体验"
android:textColor="#333333"
android:textSize="12sp" /> <TextView
android:id="@+id/tv_register_healthvip_viptimeprompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_register_healthvip_utitle"
android:layout_below="@+id/tv_register_healthvip_explain"
android:layout_marginTop="10dp"
android:text="您的会员使用时间:"
android:textColor="#b1b1b1"
android:textSize="12sp" /> <TextView
android:id="@+id/tv_register_healthvip_viptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_register_healthvip_viptimeprompt"
android:layout_toRightOf="@+id/tv_register_healthvip_viptimeprompt"
android:text="20151028-20151128"
android:textColor="#b1b1b1"
android:textSize="12sp" />
</RelativeLayout> <View
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="#eeeeee" /> <ListView
android:id="@+id/lv_register_healthvip_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="#000000"
android:scrollbars="none" />
</LinearLayout> </LinearLayout>
<!--registration_purchased_vip_listview.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" > <LinearLayout
android:id="@+id/linear_register_healthvip_list"
android:layout_width="fill_parent"
android:layout_height="47dp"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_register_healthvip_listhead"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:layout_marginLeft="27dp"
android:gravity="center_vertical"
android:textColor="#333333"
android:textSize="13sp" /> <View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" /> <ImageView
android:id="@+id/iv_register_healthvip_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="16dp"
android:src="@drawable/registration_icon_toright" />
</LinearLayout>
<!-- listview --> <View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="#ededed" /> <TextView
android:id="@+id/tv_register_healthvip_lists"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:lineSpacingExtra="10dp"
android:paddingBottom="15dp"
android:paddingLeft="28dp"
android:paddingRight="15dp"
android:textColor="#999999"
android:textSize="12sp"
android:visibility="gone" /> </LinearLayout>