首页 > 代码库 > 赵雅智_AndroidUI_Android中listview可折叠伸缩仿手风琴效果(动态)
赵雅智_AndroidUI_Android中listview可折叠伸缩仿手风琴效果(动态)
之前写了一篇静态数据listview显示手风琴效果,今天写的博客是动态实现listview的手风琴效果
静态链接:listview静态手风琴效果
数据都是存储在数据库中的,如果想要获取数据就要查询数据库
并且实现了:点击查看想要看的名片的时候,上一个打开的就能关闭
核心代码如下:
package com.cards.activity; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.cards.R; import com.cards.commom.Common; import com.cards.commom.MyApp; import com.cards.dao.CardDao; import com.cards.dao.CardDaoImp; import com.cards.entity.Card; public class CardsListAct extends Activity implements OnItemClickListener { private FlexListAdapter adapter; private List<Card> list; private boolean[] isCurrentItems; private ListView lv_cards; private Button btnRegistered; private TextView tvTitle; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_cardlist); lv_cards = (ListView) findViewById(R.id.list_cards); lv_cards.setSelector(R.color.transparent); // 初始化数据 init(); } /** * * @explaination 初始化数据 * @author yazhizhao * @time 2014-7-23上午11:30:12 */ private void init() { // 查找数据库数据 CardDao cardDao = new CardDaoImp(this); list = cardDao.findAll(); // 是否点开的list集合 isCurrentItems = new boolean[list.size()]; // 刚进入的时候全部条目显示闭合状态 for (int i = 0; i < isCurrentItems.length; i++) { isCurrentItems[i] = false; } // 设置adapter adapter = new FlexListAdapter(); lv_cards.setAdapter(adapter); lv_cards.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /* * 只打开一个 */ for (int i = 0; i < isCurrentItems.length; i++) { if (i != position) { isCurrentItems[i] = false; } } // 打开或者合上 isCurrentItems[position] = !isCurrentItems[position]; // 即时刷新 adapter.notifyDataSetChanged(); } /** * * @explaination 自定义adapter * @author yazhizhao * @time 2014-7-23上午11:33:39 */ class FlexListAdapter extends BaseAdapter { public int getCount() { return list.size(); } 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, list.get(pos), pos, false); } else { view = (FlexLinearLayout) v; view.setWorkTitleLayout(list.get(pos), pos, isCurrentItems[pos]); } return view; } } /** * * @explaination 自定义线性布局 * @author yazhizhao * @time 2014-7-23上午11:25:07 */ public class FlexLinearLayout extends LinearLayout { public static final int BULE = 0xFF3D8CB8; private LinearLayout layout; private RelativeLayout rlName; private RelativeLayout rlCards; private TextView tvCardsPhoneNumInfo; private TextView tvCardsAddressInfo; private TextView tvCardsMailInfo; private TextView tvCardsOtherInfo; private TextView tvCardsName; private TextView tvCardName; private ImageView imgCardsDel; private LayoutInflater mInflater; /** * 创建一个带有伸缩效果的LinearLayout * * @param context * 上下文对象 * @param card * 内容详细 * @param position * 该列所在列表的位置 * @param isCurrentItem * 是否为伸展 */ public FlexLinearLayout(final Context context, final Card card, final int position, boolean isCurrentItem) { super(context); mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); layout = (LinearLayout) mInflater.inflate(R.layout.act_cards_list, null); rlName = (RelativeLayout) layout.findViewById(R.id.rl_name); rlCards = (RelativeLayout) layout.findViewById(R.id.rl_cards); tvCardsPhoneNumInfo = (TextView) layout .findViewById(R.id.tv_mc_phoneNum); 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_mc_addressInfo); tvCardsMailInfo = (TextView) layout.findViewById(R.id.tv_mc_mail); tvCardsOtherInfo = (TextView) layout .findViewById(R.id.tv_mc_otherInfo); imgCardsDel = (ImageView) layout.findViewById(R.id.iv_cards_del); tvCardsPhoneNumInfo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phone_Num = tvCardsPhoneNumInfo.getText().toString() .trim(); Intent intent = new Intent(Intent.ACTION_CALL, Uri .parse("tel:" + phone_Num)); startActivity(intent); } }); imgCardsDel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Builder builder = new AlertDialog.Builder(CardsListAct.this); builder.setMessage("确定要删除此名片?") .setCancelable(false) .setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int id) { CardDao cardDao = new CardDaoImp( context); cardDao.delete(card.getId()); init(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int id) { dialog.cancel(); } }); builder.create().show(); } }); this.addView(layout); setWorkTitleLayout(card, position, isCurrentItem); } /** * 设置该列的状态及样式 * * @param card * 内容详细 * @param position * 该列所在列表的位置 * @param isCurrentItem * 是否为伸展 */ public void setWorkTitleLayout(final Card card, final int position, boolean isCurrentItem) { rlName.setBackgroundResource(R.drawable.title_2); tvCardsName.setText(card.getTruename()); tvCardsName.setTextColor(BULE); if (isCurrentItem) { tvCardName.setText(card.getTruename()); tvCardsOtherInfo.setText(card.getMeno()); tvCardsPhoneNumInfo.setText("电话:" + card.getMobile1()); tvCardsMailInfo.setText("邮箱:" + card.getEmail()); tvCardsAddressInfo.setText("地址:" + card.getAddress()); } rlCards.setVisibility(isCurrentItem ? VISIBLE : GONE); } } @Override protected void onStart() { super.onStart(); init(); // 给线性布局设置adapter MyApp.getInstance().addActivity(this); } @Override protected void onDestroy() { Common.DestroyLoading(CardsListAct.this); finish(); super.onDestroy(); } }
知识点解析:
设置adapter要放在init中,当删除数据的时候调用init才不会出错,
如果只是放在oncreate中,删除数据再刷新的时候就会报错
// 设置adapter adapter = new FlexListAdapter(); lv_cards.setAdapter(adapter); lv_cards.setOnItemClickListener(this);
这段代码就是控制只打开一个item,当打开其中一个item的时候其他的关闭
for (int i = 0; i < isCurrentItems.length; i++) { if (i != position) { isCurrentItems[i] = false; } }
判断打开还是关闭的优化代码:
if (isCurrentItems[position]) { isCurrentItems[position] = false; } else { isCurrentItems[position] = true; }优化为
isCurrentItems[position] = !isCurrentItems[position];
赵雅智_AndroidUI_Android中listview可折叠伸缩仿手风琴效果(动态)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。