布局Activity:
public class DiscussAty extends BaseAty implements CommentExpandAdapter.DiscussTextViewOnclick,OnClickListener{
private ImageView image_back;
private TextView editText;
private ExpandableListView commentExpandableListView;
private CommentExpandAdapter commentExpandAdapter;
private List<CommentDetailBean> mList = new ArrayList<>();
private BottomSheetDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discuss_aty);
initView();
}
private void initView() {
image_back = findViewById(R.id.discuss_pinglun_back);
editText = findViewById(R.id.discuss_pinglun_edit);
commentExpandableListView = findViewById(R.id.discuss_commentexpandalelistview);
mList = generateTestData();
initExpandableListView(mList);
image_back.setOnClickListener(this);
editText.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.discuss_pinglun_back) {
finish();
} else if (id == R.id.discuss_pinglun_edit) {
showCommentDialog();
}
}
/**
* 初始化评论和回复列表
*/
private void initExpandableListView(final List<CommentDetailBean> commentList){
commentExpandableListView.setGroupIndicator(null);
commentExpandAdapter = new CommentExpandAdapter(this, commentList,this);
commentExpandableListView.setAdapter(commentExpandAdapter);
//默认展开所有回复
int size = commentList.size();
for(int i = 0; i<size; i++){
commentExpandableListView.expandGroup(i);
}
commentExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
commentExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int
childPosition, long id) {
return false;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* by moos on 2018/04/20
* func:生成测试数据
* @return 评论数据
*/
private List<CommentDetailBean> generateTestData(){
List<CommentDetailBean> commentList = new ArrayList<>();
CommentDetailBean bean1 = new CommentDetailBean("程序员","2021年4月16日15:46:04就是这个点我开始装逼了。","五分钟前");
List<ReplyDetailBean> list_bean1 = new ArrayList<>();
ReplyDetailBean reply_bean1 = new ReplyDetailBean("沐风","时间总是在不经意间溜走了,你啊你啊个腿的。");
ReplyDetailBean reply_bean2 = new ReplyDetailBean("狗子","狗子的时间总是在不经意间溜走了,你啊你啊个腿的。");
ReplyDetailBean reply_bean3 = new ReplyDetailBean("晓明","晓明的时间总是在不经意间溜走了,你啊你啊个腿的。");
ReplyDetailBean reply_bean4 = new ReplyDetailBean("Baby","Baby的时间总是在不经意间溜走了,你啊你啊个腿的。");
ReplyDetailBean reply_bean5 = new ReplyDetailBean("大黄","大黄的时间总是在不经意间溜走了,你啊你啊个腿的。");
list_bean1.add(reply_bean1);
list_bean1.add(reply_bean2);
list_bean1.add(reply_bean3);
list_bean1.add(reply_bean4);
list_bean1.add(reply_bean5);
bean1.setReplyList(list_bean1);
CommentDetailBean bean2 = new CommentDetailBean("产品狗","22021年4月16日15:59:36就是这个点我开始装逼了。","五分钟前");
List<ReplyDetailBean> list_bean2 = new ArrayList<>();
ReplyDetailBean reply_bean6 = new ReplyDetailBean("沐风","时间总是在不经意间溜走了,你啊你啊个腿的。");
ReplyDetailBean reply_bean7 = new ReplyDetailBean("狗子","狗子的时间总是在不经意间溜走了,你啊你啊个腿的。");
list_bean2.add(reply_bean6);
list_bean2.add(reply_bean7);
bean2.setReplyList(list_bean2);
commentList.add(bean1);
commentList.add(bean2);
return commentList;
}
/**
* by moos on 2018/04/20
* func:弹出评论框
*/
private void showCommentDialog(){
dialog = new BottomSheetDialog(this);
View commentView = LayoutInflater.from(this).inflate(R.layout.comment_dialog_layout,null);
final EditText commentText = (EditText) commentView.findViewById(R.id.dialog_comment_et);
final TextView bt_comment = commentView.findViewById(R.id.dialog_comment_bt);
dialog.setContentView(commentView);
/**
* 解决bsd显示不全的情况
*/
View parent = (View) commentView.getParent();
BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
commentView.measure(0,0);
behavior.setPeekHeight(commentView.getMeasuredHeight());
bt_comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String commentContent = commentText.getText().toString().trim();

本文档详细介绍了如何在Android应用中创建一个带有ExpandableListView的Discussion Activity,包括数据初始化、Adapter的使用、评论与回复功能的实现,以及相关事件监听。关键部分展示了CommentExpandAdapter的定制和处理文本展开与收缩的逻辑。
最低0.47元/天 解锁文章
166

被折叠的 条评论
为什么被折叠?



