关闭

ExpandableListView长按监听实现

401人阅读 评论(0) 收藏 举报
分类:

多级列表(ExpandableListView)长按监听实现


再适配器中加代码:


     1.在getChildView()中设置视图

       view.setTag(参数1(id1),父控件索引(groupPosition));参数1:必须是本类中的具体id:例R.id.控件,

       view.setTag(参数1(id2),子控件索引(childPosition));参数1:必须是本类中的具体id:例 R.id.控件。

<pre name="code" class="java">public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view = convertView;

		if (view == null) {
			//填充视图
			view = LayoutInflater.from(context).inflate(R.layout.music_child, null);
			vcHolder = new ViewChildHolder();
			/*子布局中的控件初始化*/
			view.setTag(vcHolder);
		}else{
			vcHolder = (ViewChildHolder) view.getTag();
		}
		
	     view.setTag(R.id.about_back_ImageView,groupPosition);//参数1:必须是本类中的具体id:例R.id.控件,
	     view.setTag(R.id.about_Email,childPosition);
		

		return view;
	}



     2.在getGroupView()中设置视图       view.setTag(参数1(id1),父控件索引(groupPosition));参数1:同上

       view.setTag(参数1(id2),-1);参数1:同上

代码如下:

public View getGroupView(final int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.music_group, null);
	                /*
                         初始化group中的控件*/
			view.setTag(vGHolder);
		}else{
			vGHolder = (ViewGroupHolder) view.getTag();
		}
		 view.setTag(R.id.about_back_ImageView,groupPosition);//参数1:必须是本类中的具体id:例R.id.控件,
	         view.setTag(R.id.about_Email,-1);
		
		
		return view;
	}



     3.在实例化ExpandableListView对象 .setOnItemLongClickListener()中
    {
int groupPos = view.getTag(id1);
int childPos = view.getTag(id2);

if(childPos == -1){

  //长按父控件

         }else{

                  //长按子控件

         }

      }

代码如下:

ev_listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				int groupPos = (Integer) view.getTag(R.id.about_back_ImageView);
				int childPos = (Integer) view.getTag(R.id.about_Email);
				if(childPos == -1){//长按父控件
					Toast.makeText(getContext(), "groupPos"+groupPos, Toast.LENGTH_SHORT).show();
		         }else{//长按子控件
		        	 Toast.makeText(getContext(), "groupPos"+groupPos+"childPos"+childPos, Toast.LENGTH_SHORT).show();
		         }
				return true;
			}
		});


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3776次
    • 积分:173
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条