赵雅智_AndroidUI_Android中listview可折叠伸缩仿手风琴效果(静态)

最近些一个名片夹得app。其中名片夹客户要求做成可折叠伸缩的效果,真是费了好大劲啊~~~啥都不说了看效果图吧~~



package com.cards.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cards.R;

public class CardsListAct extends ListActivity implements OnScrollListener,
		OnItemClickListener {
	private static String LAYOUT_INFLATER_SERVICE = Context.LAYOUT_INFLATER_SERVICE;

	public static final String TV_CARDS_NAME = "tv_cards_name";
	public static final String TV_CARDS_PHONENUMINFO = "tv_cards_phoneNumInfo";
	public static final String TV_CARDS_ADDRESSINFO = "tv_cards_addressInfo";
	public static final String TV_CARDS_MAILINFO = "tv_cards_mailInfo";
	public static final String TV_CARDS_OTHERINFO = "tv_cards_otherInfo";
	public static final String TV_CARD_NAME = "tv_card_name";

	private LayoutInflater mInflater;

	private FlexListAdapter adapter;

	private List<Map<String, String>> contentDefileList;
	private boolean[] isCurrentItems;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		init();
		setListAdapter(adapter);
		getListView().setBackgroundColor(Color.WHITE);
		getListView().setCacheColorHint(Color.TRANSPARENT);
		getListView().setDivider(
				getResources().getDrawable(R.color.transparent));
		getListView().setSelector(R.drawable.work_detail_click_bg);
		getListView().setOnScrollListener(this);
		getListView().setOnItemClickListener(this);
	}

	/**
	 * 初始化数据
	 */
	private void init() {
		contentDefileList = new ArrayList<Map<String, String>>();

		Map<String, String> contentDefile = new HashMap<String, String>();
		contentDefile.put(TV_CARDS_NAME, "张三");
		contentDefile.put(TV_CARD_NAME, "张三");
		contentDefile.put(TV_CARDS_PHONENUMINFO, "13933333333");
		contentDefile.put(TV_CARDS_ADDRESSINFO, "保定市高开区朝阳北大街866号(保百购物广场南侧)");
		contentDefile.put(TV_CARDS_MAILINFO, "[email protected]");
		contentDefile.put(TV_CARDS_OTHERINFO, "部门经理");

		Map<String, String> contentDefile_1 = new HashMap<String, String>();
		contentDefile_1.put(TV_CARDS_NAME, "李四");
		contentDefile_1.put(TV_CARD_NAME, "李四");
		contentDefile_1.put(TV_CARDS_PHONENUMINFO, "13933333333");
		contentDefile_1.put(TV_CARDS_ADDRESSINFO, "保定市高开区朝阳北大街866号(保百购物广场南侧)");
		contentDefile_1.put(TV_CARDS_MAILINFO, "[email protected]");
		contentDefile_1.put(TV_CARDS_OTHERINFO, "经理");

		Map<String, String> contentDefile_2 = new HashMap<String, String>();
		contentDefile_2.put(TV_CARDS_NAME, "王五");
		contentDefile_2.put(TV_CARD_NAME, "王五");
		contentDefile_2.put(TV_CARDS_PHONENUMINFO, "13933333333");
		contentDefile_2.put(TV_CARDS_ADDRESSINFO, "hfdkajshfkadsjflkajsdlkf");
		contentDefile_2.put(TV_CARDS_MAILINFO, "[email protected]");
		contentDefile_2.put(TV_CARDS_OTHERINFO, "经理");

		Map<String, String> contentDefile_3 = new HashMap<String, String>();
		contentDefile_3.put(TV_CARDS_NAME, "vvv");
		contentDefile_3.put(TV_CARD_NAME, "vvv");
		contentDefile_3.put(TV_CARDS_PHONENUMINFO, "13933333333");
		contentDefile_3.put(TV_CARDS_ADDRESSINFO, "hfdkajshfkadsjflkajsdlkf");
		contentDefile_3.put(TV_CARDS_MAILINFO, "[email protected]");
		contentDefile_3.put(TV_CARDS_OTHERINFO, "设计师");
		Map<String, String> contentDefile_5 = new HashMap<String, String>();
		contentDefile_5.put(TV_CARDS_NAME, "bbb");

		Map<String, String> contentDefile_6 = new HashMap<String, String>();
		contentDefile_6.put(TV_CARDS_NAME, "ccc");
		contentDefile_6.put(TV_CARD_NAME, "ccc");

		Map<String, String> contentDefile_7 = new HashMap<String, String>();
		contentDefile_7.put(TV_CARDS_NAME, "ddd");
		contentDefile_7.put(TV_CARD_NAME, "ddd");

		Map<String, String> contentDefile_4 = new HashMap<String, String>();
		contentDefile_4.put(TV_CARDS_NAME, "aaa");
		contentDefile_4.put(TV_CARD_NAME, "aaa");

		contentDefileList.add(contentDefile);
		contentDefileList.add(contentDefile_1);
		contentDefileList.add(contentDefile_2);
		contentDefileList.add(contentDefile_3);
		contentDefileList.add(contentDefile_4);
		contentDefileList.add(contentDefile_6);
		contentDefileList.add(contentDefile_7);

		isCurrentItems = new boolean[contentDefileList.size()];

		for (int i = 0; i < isCurrentItems.length; i++) {

			isCurrentItems[i] = false;
		}

		Log.w("TAG", "AutoLoadActivity init() =========>>>>>> come in.");
		adapter = new FlexListAdapter();
	}

	public void onScroll(AbsListView v, int i, int j, int k) {
	}

	public void onScrollStateChanged(AbsListView v, int state) {
		if (state == OnScrollListener.SCROLL_STATE_IDLE) {
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (isCurrentItems[position]) {
			isCurrentItems[position] = false;
		} else {
			isCurrentItems[position] = true;
		}
		// 即时刷新
		adapter.notifyDataSetChanged();
	}

	class FlexListAdapter extends BaseAdapter {
		int count = contentDefileList.size();

		public int getCount() {
			return count;
		}

		public Object getItem(int pos) {
			return pos;
		}

		public long getItemId(int pos) {
			return pos;
		}

		public View getView(int pos, View v, ViewGroup p) {
			FlexLinearLayout view = null;
			if (null == v) {
				view = new FlexLinearLayout(CardsListAct.this,
						contentDefileList.get(pos), pos, false);
			} else {
				view = (FlexLinearLayout) v;
				view.setWorkTitleLayout(contentDefileList.get(pos), pos,
						isCurrentItems[pos]);
			}
			return view;
		}
	}

	public class FlexLinearLayout extends LinearLayout {
		public static final int BULE = 0xFF3D8CB8;

		private LinearLayout layout;

		private RelativeLayout rlName;

		private RelativeLayout llCards;

		private TextView tvCardsPhoneNumInfo;

		private TextView tvCardsAddressInfo;

		private TextView tvCardsMailInfo;

		private TextView tvCardsOtherInfo;

		private TextView tvCardsName;
		private TextView tvCardName;

		/**
		 * 创建一个带有伸缩效果的LinearLayout
		 * 
		 * @param context
		 * @param contextDefail
		 *            内容详细
		 * @param position
		 *            该列所在列表的位置
		 * @param isCurrentItem
		 *            是否为伸展
		 */
		public FlexLinearLayout(Context context,
				final Map<String, String> contextDefail, final int position,
				boolean isCurrentItem) {
			super(context);

			layout = (LinearLayout) mInflater.inflate(R.layout.act_cards_list,
					null);
			rlName = (RelativeLayout) layout.findViewById(R.id.rl_name);
			llCards = (RelativeLayout) layout.findViewById(R.id.ll_cards);

			tvCardsPhoneNumInfo = (TextView) layout
					.findViewById(R.id.tv_cards_phoneNumInfo);
			tvCardsName = (TextView) layout.findViewById(R.id.tv_cards_name);
			tvCardName = (TextView) layout.findViewById(R.id.tv_card_name);

			tvCardsAddressInfo = (TextView) layout
					.findViewById(R.id.tv_cards_addressInfo);
			tvCardsMailInfo = (TextView) layout
					.findViewById(R.id.tv_cards_mailInfo);
			tvCardsOtherInfo = (TextView) layout
					.findViewById(R.id.tv_cards_otherInfo);

			this.addView(layout);
			setWorkTitleLayout(contextDefail, position, isCurrentItem);
		}

		/**
		 * 设置该列的状态及样式
		 * 
		 * @param contentDefail
		 *            内容详细
		 * @param position
		 *            该列所在列表的位置
		 * @param isCurrentItem
		 *            是否为伸展
		 */
		public void setWorkTitleLayout(final Map<String, String> contentDefail,
				final int position, boolean isCurrentItem) {

			rlName.setBackgroundResource((position % 2 == 1) ? R.drawable.title_1
					: R.drawable.title_2);

			tvCardsName.setText(contentDefail.get(TV_CARDS_NAME));

			tvCardsName.setTextColor((position % 2 == 0) ? BULE : Color.WHITE);

			if (isCurrentItem) {
				Log.d("TAG", "isCurrentItem ============>>>>>>>>>  " + position);
				tvCardsPhoneNumInfo.setText("电话:"
						+ contentDefail.get(TV_CARDS_PHONENUMINFO));
				tvCardsMailInfo.setText("邮箱:"
						+ contentDefail.get(TV_CARDS_MAILINFO));
				tvCardsAddressInfo.setText("地址:"
						+ contentDefail.get(TV_CARDS_ADDRESSINFO));
				tvCardsOtherInfo.setText(contentDefail.get(TV_CARDS_OTHERINFO));
				tvCardName.setText(contentDefail.get(TV_CARD_NAME));
			}

			llCards.setVisibility(isCurrentItem ? VISIBLE : GONE);
		}
	}

}



act_cards_list布局文件:

<?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="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rl_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent" >

        <TextView
            android:id="@+id/tv_cards_name"
            style="@style/cards_item_title"
            android:text="路人甲" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/ll_cards"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/iv_card_list_bg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/ll_cards_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip" >

            <TextView
                android:id="@+id/tv_card_name"
                style="@style/work_detail_row_style"
                android:text="路人甲"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_mc_otherInfo"
                style="@style/work_detail_row_style"
                android:text="财务总监"
                android:textColor="@color/blue"
                android:textSize="10dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_cards_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ll_cards_name"
            android:orientation="vertical" 
            android:layout_marginTop="20dp">

            <TextView
                android:id="@+id/tv_mc_phoneNum"
                style="@style/work_detail_row_style"
                android:text="18933333333" />

            <TextView
                android:id="@+id/tv_mc_mail"
                style="@style/work_detail_row_style"
                android:text="[email protected]" />

            <TextView
                android:id="@+id/tv_mc_addressInfo"
                style="@style/work_detail_row_style"
                android:layout_width="171dp"
                android:layout_centerInParent="true"
                android:text="保定市高开区朝阳北大街866号(保百购物广场南侧)" />
        </LinearLayout>

</RelativeLayout>

</LinearLayout>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。