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"

未完待续

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

相关推荐
ACP广源盛1392462567312 分钟前
此芯 P1 AI BOX / AI NAS@ACP#IX8008、IX8024 应用场景与市场机会
大数据·人工智能·分布式·单片机·嵌入式硬件
凤山老林17 分钟前
从美团全栈化看 AI 冲击:前端转全栈,是自救还是必然
前端·人工智能·状态模式
大龄码农有梦想21 分钟前
企业如何利用 AI 大模型提升业务效率?企业 AI 应用应该从哪些场景切入
人工智能·ai agent·ai智能体·ai应用·ai工作流·ai赋能·智能体平台
其实防守也摸鱼22 分钟前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·运维·网络·人工智能·学习·安全·macos
摇滚侠5 小时前
AI 编程工具 《TRAE 官方手册》阅读笔记 AI 编程核心 上
人工智能·笔记
一个王同学6 小时前
从零到一 | CV转多模态大模型 | week19 | 基于 FastAPI 和 vLLM 的多模态大模型部署
人工智能·深度学习·计算机视觉·fastapi·改行学it·vllm
m沐沐6 小时前
【深度学习】YOLOv2目标检测算法——改进点、网络结构与聚类先验框解析
人工智能·pytorch·深度学习·算法·yolo·目标检测·transformer
企业老板ai培训7 小时前
破解中小企业AI变现难:2026年企业AI培训与陪跑行业趋势深度报告,为何从‘陪跑’到‘变现’才是关键?
大数据·人工智能
神奇霸王龙8 小时前
国产音乐视频 Prompt 三段式屠夫榜:5 个国产视频模型实测对比
人工智能·ai·prompt·音视频·agent·ai编程·agi
OpenCSG8 小时前
Hugging Face遭遇AI Agent攻击:AI资产管理正在进入新阶段
人工智能·大模型