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"

未完待续

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

相关推荐
AI风控技术指南4 分钟前
Agent安全工程实践:从指令到执行,拆解五类风险与关键控制点
人工智能·大模型安全围栏·数美科技·ai安全围栏·agent安全评测·agent安全围栏·ai安全评测
一只落魄的蜂鸟6 分钟前
【2026年-13期】 Becoming an Upgradable Professional in the Era of Intelligence
人工智能
岁月刘声机13 分钟前
国标实施|《法律法规电子文件》三项标准正式施行,电子文档可信闭环成关键
大数据·人工智能·microsoft
把我格子衫拿来16 分钟前
本周科技热点:Meta 新模型、微软 Maia 300 与 AI 智能体新动向(10.12—10.15)
人工智能·科技·microsoft
峥嵘life25 分钟前
Android16 Camera2录像中拍照后预览模糊问题分析与解决总结
人工智能·数码相机·wireshark
漫步人生走在路上29 分钟前
用小语种SEO突破外贸B2B天花板:法语、德语市场的实测数据
开发语言·前端·javascript·人工智能·搜索引擎·ecmascript
二川bro36 分钟前
签到自动化
人工智能
白日放歌须纵久41 分钟前
Agent 工具契约测试工程:适配层隔离与 Mock 回放,让接口变更不再静默击穿你的工作流
人工智能
火山引擎开发者社区42 分钟前
一句话,做出一个应用:豆包背后的数据库底座是什么?
人工智能
humors22143 分钟前
如何识别网络假信息
网络·人工智能·ai·互联网·识别·谣言·假消息