聊天界面的左右分布局

for (Message message : vector) {
                    String from = message.getFrom();
                    String body = message.getBody();
                    LogUtil.i("ShowGroupChatMessage", from + ":" + body);
                    // 判断是我说的,还是别人说的
                    View view = null;
                    if (from.equals(TApplication.currentUser.getUser())) {
                        // 我说的
                        view = View.inflate(context, R.layout.right, null);
                    } else {
                        // 好友说的
                        view = View.inflate(context, R.layout.left, null);
                    }

                    if (!from.equals(TApplication.currentUser.getUser())) {
                        // 好友说的
                        TextView tvFriendName = (TextView) view
                                .findViewById(R.id.tv_chat_friendName);
                        // from:allrun@confrence.tarena.com/friendName
                        String friendName = from.substring(from
                                .lastIndexOf("/") + 1);
                        tvFriendName.setText(friendName);
                    }
                    // 显示,判断数据的类型,有文本,有face
                    // 不同类型显示不一样
                    int type = ChatUtil.getType(body);
                    if (type == ChatUtil.TYPE_AUDIO) {//是语言
                        ImageView ivPlay = (ImageView) view
                                .findViewById(R.id.iv_playAudio);
                        ivPlay.setVisibility(View.VISIBLE);
                        ivPlay.setTag(body);
                        ivPlay.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                try {
                                    String body = (String) v.getTag();
                                    ChatUtil.getAudio(body);
                                    MediaPlayer mediaPlayer = new MediaPlayer();
                                    mediaPlayer.setDataSource(ChatUtil
                                            .getAudioFile().getAbsolutePath());
                                    mediaPlayer.prepare();
                                    mediaPlayer.start();
                                } catch (Exception e) {
                                    ExceptionUtil.handleException(e);
                                }
                            }
                        });

                    }
                    if (type == ChatUtil.TYPE_IMAGE) {//图片
                        ImageView iv = (ImageView) view
                                .findViewById(R.id.iv_chat);
                        iv.setVisibility(View.VISIBLE);

                        iv.setImageBitmap(ChatUtil.getImage(body));
                    }
                    if (type == ChatUtil.TYPE_TEXT) {//文本
                        TextView tv = (TextView) view
                                .findViewById(R.id.tv_chat_text);
                        tv.setVisibility(View.VISIBLE);
                        tv.setText(body);
                    }
                    if (type == ChatUtil.TYPE_FACE) {//表情
                        GifImageView gifImageView = (GifImageView) view
                                .findViewById(R.id.gifImageView);
                        gifImageView.setVisibility(View.VISIBLE);
                        String fileName = ChatUtil.getFaceFileName(body);
                        GifDrawable gifDrawable = new GifDrawable(getAssets(),
                                fileName);
                        gifImageView.setBackgroundDrawable(gifDrawable);
                    }

                    llChatContent.addView(view);

                    vector.remove(message);
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                // 向上移
                                // int linearLayoutHeight = llChatContent
                                // .getHeight();
                                // int scrollViewHeight =
                                // scrollView.getHeight();
                                // LogUtil.i("向上移", "linearLayoutHeight="
                                // + linearLayoutHeight
                                // + " scrollViewHeight="
                                // + scrollViewHeight);
                                //
                                // if (linearLayoutHeight > scrollViewHeight) {
                                // int moveY = linearLayoutHeight
                                // - scrollViewHeight;
                                // scrollView.scrollTo(0, moveY);
                                // }

                                scrollView.fullScroll(ScrollView.FOCUS_DOWN);

                            } catch (Exception e) {
                                // TODO: handle exception
                            }

                        }
                    }, 1);// 有的手机samsung s4时间要长一点
                }
            } catch (Exception e) {
                ExceptionUtil.handleException(e);
            }

        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值