Android 端接入AI

最近在做Android 端AI接入,记录下遇到的问题。

1.RecyclerView 滚动问题

1.1 滚动到最后一条消息

进入会话详情页面时,需要滚动到最后一条聊天记录,当item内容太高时,scrollToPosition只能保证 item「进入可见区域」,但不能保证滚到 item 底部。 item 超过一屏时,它只会把 item 顶部对齐到屏幕顶部,底部在屏幕外面。 需要手动滚动到最底部。

注意不能使用smoothScrollToPosition,不然会有滚动动画,效果难以接受。

java 复制代码
    /**
     * 滚到指定 item 的底部(不管 item 多高)
     */
    private void scrollToItemBottom(RecyclerView recyclerView, int position) {
        recyclerView.scrollToPosition(position);
        recyclerView.post(() -> {
            RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
            if (lm == null) return;
            View view = lm.findViewByPosition(position);
            if (view == null) return;
            int viewBottom = view.getBottom();
            int rvHeight = recyclerView.getHeight();
            if (viewBottom > rvHeight) {
                recyclerView.scrollBy(0, viewBottom - rvHeight);
            }
        });
    }

1.2 SSE 流式打字效果(实时追加文字)

此时应使用smoothScrollToPosition,但是当文字过长时,item会抖动。

ini 复制代码
	LinearSmoothScroller scroller = new LinearSmoothScroller(AiChatInfoActivity.this) {
            @Override
            protected int getVerticalSnapPreference() {
                return SNAP_TO_END;
            }
        };
		int position = aiChatAdapter.getItemCount() - 1;
        scroller.setTargetPosition(position);
        mBinding.rvChat.getLayoutManager().startSmoothScroll(scroller);

移除item动画后,效果更加

scss 复制代码
((SimpleItemAnimator) mBinding.rvChat.getItemAnimator()).setSupportsChangeAnimations(false);  
mBinding.rvChat.setItemAnimator(null);

2.Markwon

AI返回的的文字是支持Markwon 格式的,安卓端也应支持

2.1添加依赖

arduino 复制代码
implementation "io.noties.markwon:core:4.6.2"
implementation "io.noties.markwon:ext-strikethrough:4.6.2"
implementation "io.noties.markwon:ext-tables:4.6.2"
implementation "io.noties.markwon:ext-tasklist:4.6.2"
implementation "io.noties.markwon:image-glide:4.6.2"

2.2 初始化

less 复制代码
        markwon = Markwon.builder(this)
                .usePlugin(StrikethroughPlugin.create())
                .usePlugin(TablePlugin.create(this))
                .usePlugin(TaskListPlugin.create())
                .usePlugin(GlideImagesPlugin.create(Glide.with(this)))
                .usePlugin(new AbstractMarkwonPlugin() {
                    @Override
                    public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
                        builder
                                .codeBackgroundColor(Color.parseColor("#1E1E1E"))
                                .codeBlockMargin(16)
                                .blockQuoteColor(Color.parseColor("#4FC3F7"));
                    }
                })
                .build();

2.3 使用

swift 复制代码
markwon.setMarkdown(textView,
        "# Hello\n**加粗文本**\n- 列表项1\n- 列表项2\n`行内代码`");

2.4 使用中遇到的坑

2.4.1 点击事件不能响应

在item的最外层布局添加

ini 复制代码
android:clickable="true"  
android:focusable="true"

在要显示markDown 的textview中添加

ini 复制代码
android:clickable="false"  
android:focusable="false"

未完待续

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

相关推荐
冬奇Lab25 分钟前
一天一个开源项目(第259篇):Modern Web Guidance —— Chrome 团队给 AI 编码 Agent 的「现代 Web 最佳实践」外挂
人工智能·开源
飞哥数智坊2 小时前
打造自己的 Personal Agent,从抄大肥鱼作业开始
人工智能·deepseek
对你无可奈何2 小时前
Magpie 实战:把 WorkBuddy、Qoder 的订阅模型接进一个本地池子
人工智能
Csvn2 小时前
每请求到底多少钱?把 Token 成本算成可观测指标(C01)
人工智能·aigc·agent
吴名氏.2 小时前
Java转AI Agent教程(第十三周)混合检索与重排
java·数据库·人工智能·ai·ai agent
老金带你玩AI4 小时前
惊人的一致性!Seedream 5.5实测!
人工智能
数据技术说5 小时前
在 KES AI 数据库上搭一条 NL2SQL 链路:语义、召回、权限各放哪
人工智能·sql·代码规范
jay神5 小时前
基于 YOLOv8 的手势识别系统(Web 版)
前端·人工智能·python·深度学习·yolo·计算机视觉·计算机毕业设计
奶油话梅糖5 小时前
用 iPhone 扫一间房,AI 帮你在浏览器里 1:1 重建它:Wes Bos 的 scan-to-threejs 工作流
人工智能·ios·iphone