码迷,mamicode.com
首页 > 其他好文 > 详细

ListView的Item中带按钮以及按钮布局的显示与隐藏

时间:2015-04-08 21:38:19      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:android

一、上图

                          技术分享


二、图片功能解说

       1、点击ListView中其中的一项,Item中处于隐藏状态的操作按钮部分布局显示,其他项显示的布局同时被隐藏,再次点击该项,操作按钮部分布局再次隐藏。

       2、点击各个按钮,分别弹出哪个按钮被点击(也就是点击每一个按钮分别处理不同按钮的业务功能)。

三、实现代码主要处于适配器的处理。见如下代码:

package com.test.listviewbtn.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.test.listviewbtn.R;
import com.test.listviewbtn.beans.LvItemBean;

public class LvAdapter extends BaseAdapter {

	private ArrayList<LvItemBean> lvItemBeanList;
	// 布局加载器
	private LayoutInflater mInflater;
	// 上下文
	private Context context;
	// 布局缓存对象
	private ViewHolder holder;
	//记录当前展开项的索引
	private int expandPosition = -1;

	public LvAdapter(ArrayList<LvItemBean> lvItemBeanList, Context context) {
		super();
		this.lvItemBeanList = lvItemBeanList;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return null == lvItemBeanList ? 0 : lvItemBeanList.size();
	}

	@Override
	public Object getItem(int position) {
		return lvItemBeanList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.activity_main_listview_item, null);
			holder = new ViewHolder();
			holder.tv1 = (TextView) convertView.findViewById(R.id.tv_1);
			holder.tv2 = (TextView) convertView.findViewById(R.id.tv_2);
			holder.tv3 = (TextView) convertView.findViewById(R.id.tv_3);
			holder.llItemTop = (LinearLayout) convertView.findViewById(R.id.ll_item_top);
			holder.llItemBottom = (LinearLayout) convertView.findViewById(R.id.ll_item_bottom);
			holder.bt1 = (Button) convertView.findViewById(R.id.bt_1);
			holder.bt2 = (Button) convertView.findViewById(R.id.bt_2);
			holder.bt3 = (Button) convertView.findViewById(R.id.bt_3);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		LvItemBean lvItemBean = lvItemBeanList.get(position);
		if (null != lvItemBean) {
			holder.llItemTop.setOnClickListener(new OnLvItemClickListener(position));
			holder.bt1.setOnClickListener(new OnBtnClickListener());
			holder.bt2.setOnClickListener(new OnBtnClickListener());
			holder.bt3.setOnClickListener(new OnBtnClickListener());
			//如果点击的是当前项,则将其展开,否则将其隐藏
			if(expandPosition == position){
				holder.llItemBottom.setVisibility(View.VISIBLE);
			}else{
				holder.llItemBottom.setVisibility(View.GONE);
			}
		}

		return convertView;
	}

	class OnLvItemClickListener implements OnClickListener {
		private int position;

		public OnLvItemClickListener(int position) {
			super();
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			//如果当前项为展开,则将其置为-1,目的是为了让其隐藏,如果当前项为隐藏,则将当前位置设置给全局变量,让其展开,这也就是借助于中间变量实现布局的展开与隐藏
			if(expandPosition == position){
				expandPosition = -1;
			}else{
				expandPosition = position;
			}
			notifyDataSetChanged();
		}

	}
	
	class OnBtnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.bt_1:
				Toast.makeText(context, "新增按钮被点击", 1).show();
				break;
			case R.id.bt_2:
				Toast.makeText(context, "修改按钮被点击", 1).show();
				break;
			case R.id.bt_3:
				Toast.makeText(context, "删除按钮被点击", 1).show();
				break;
			}
		}
		
	}

	class ViewHolder {
		TextView tv1;
		TextView tv2;
		TextView tv3;
		LinearLayout llItemTop;
		LinearLayout llItemBottom;
		Button bt1;
		Button bt2;
		Button bt3;
		ImageView ivUpDownArrow;
	}

}


资源代码:

    http://download.csdn.net/detail/xiogjie_67/8575521



ListView的Item中带按钮以及按钮布局的显示与隐藏

标签:android

原文地址:http://blog.csdn.net/qinyunying/article/details/44945987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!