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"

未完待续

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

相关推荐
酱学编程1 小时前
【从零到一实现一个 AI Agent 框架 · 第四篇】04. 任务规划:拆解复杂目标 -
服务器·网络·数据库·人工智能
机器之心1 小时前
世界模型评测的最大盲区,被这个新基准捅破了
人工智能·openai
机器之心2 小时前
Claude、GLM、GPT谁才是真正的AI软件工程师?首个持续更新Visual Spec-to-App Benchmark发布
人工智能·openai
dreamread2 小时前
2026子平格局分析排盘工具怎么选:看规则线索、复盘记录和AI边界
人工智能·软件工具·传统文化
mooyuan天天2 小时前
大模型全自动攻防渗透:HexStrike-AI 与 MCP 联动部署实战教程(从零搭建完整避坑手册)
人工智能·ai安全·hexstrike
东风破_2 小时前
LLM 为什么记不住你?无状态、messages 和上下文管理
javascript·人工智能
如此这般英俊2 小时前
手搓Claude Code-第六章 subagent
数据结构·人工智能·python·语言模型·自然语言处理
EAI-Robotics2 小时前
机器人操作鲁棒性:当机械手遇上真实世界的不确定性
人工智能·算法·机器人
shushangyun_3 小时前
2026智能采购商城系统选型指南:如何引领企业数字化采购升级
java·大数据·数据库·人工智能·机器学习