ExpandListView展开更多评论收起以及TextView展开全文收起的使用

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

布局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();
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_zxw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值