ExpandableListView Group 里放了一个button,但是button监听器如何获取getGroupView里的参数groupPosition呢?如果在button的监听器里直接使用groupPosition,会报错,因为groupPosition不是final类型、、纠结了许久,在getGroupView里把参数改成final就行啦、原来我还以为不能改Override的方法,不改不知道、、、
需求:因为group是一条评论、而且里面还有个评论的button(二级评论),点击按钮后,把评论更新到该评论的child里面作为二级评论
// 获取一级列表View对象 @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { Log.v(TAG, "ExpandableList GroupView.."); CommentLevel1 comment = mGroup.get(groupPosition); LayoutInflater layoutInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout layout = (RelativeLayout) layoutInflater.inflate( R.layout.list_item_comment1, null); ImageView headPic; Button reply; TextView nickName, commentContent, postTime, commentNum, commentItem; headPic = (ImageView) layout .findViewById(R.id.list_item_talkCommentLevel1_iv_headPic); reply = (Button) layout .findViewById(R.id.list_item_talkCommentLevel1_btn_reply); nickName = (TextView) layout .findViewById(R.id.list_item_talkCommentLevel1_tv_nickName); commentContent = (TextView) layout .findViewById(R.id.list_item_talkCommentLevel1_tv_comment); postTime = (TextView) layout .findViewById(R.id.list_item_talkCommentLevel1_tv_postTime); commentNum = (TextView) layout .findViewById(R.id.list_item_talkCommentLevel1_tv_commentNum); commentItem = (TextView) layout.findViewById(R.id.comment); User user = comment.getUser(); if (null != user) { headPic.setImageBitmap(user.getHeadPicture()); nickName.setText(user.getNickName()); } commentContent.setText(comment.getContent()); postTime.setText(comment.getPostTime().toLocaleString()); if (comment.getCommentNum() != 0) { commentNum.setText(String.valueOf(comment.getCommentNum())); } else { commentNum.setVisibility(View.INVISIBLE); commentItem.setVisibility(View.INVISIBLE); } // If the button get the focus, the list won't expand any more. reply.setFocusable(false); reply.setClickable(true); reply.setTag(groupPosition); reply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LayoutInflater factory = LayoutInflater .from(TalkListCommentsActivity.this); final View replyView = factory.inflate( R.layout.list_item_reply, null); new AlertDialog.Builder(TalkListCommentsActivity.this) .setTitle(R.string.talk_postAComment) .setView(replyView) .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int whichButton) { // User clicked OK so do some stuff CommentLevel2 level2; User user; Bitmap headPic; BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources() .getDrawable( R.drawable.myheadpic); headPic = bitmapDrawable .getBitmap(); EditText commentText = (EditText) replyView .findViewById(R.id.talkList_et_reply); user = new User(); level2 = new CommentLevel2(); user.setNickName("用户 x"); user.setHeadPicture(headPic); level2.setUser(user); String strComment = commentText .getText().toString(); if (null != strComment) { level2.setContent(strComment); } level2.setPostTime(new Date()); mChild.get(groupPosition).add( level2); mAdapter.notifyDataSetChanged(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int whichButton) { // User clicked cancel so do some // stuff dialog.dismiss(); } }).create().show(); } }); return layout; }