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"

未完待续

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

相关推荐
特立独行的猫a5 分钟前
一切皆插件:DeepSeek Harness 的架构哲学,以及与主流 Agent 的对比
人工智能·架构·agent·deepseek·harness
电商API_1800790524712 分钟前
京东商品详情API技术文章
大数据·运维·人工智能·网络爬虫
fīɡЙtīиɡ ℡24 分钟前
AI Agent 记忆系统
人工智能
企鹅的企1 小时前
2027北京AI数字健康与智慧医疗展官方:超两成展品亚洲首秀
人工智能·科技·机器人
lvts_cs1 小时前
淄博高新区绿天使数智创新港:高标准产研厂房全维度详解
大数据·人工智能
百胜软件@百胜软件2 小时前
从“人找货”到“货找人”:「胜券商品」如何用AI重构配补调
人工智能·重构
风流 少年2 小时前
Spring AI 2.0:Advisor
android·人工智能·spring
zhangfeng11332 小时前
[帮助聋哑人沟通]Tiger AI 手势识别大升级:从数字到无声世界,双手同时比划也能秒识别
人工智能
chunmiao30322 小时前
Claude 文本水印引退订,AI 内容溯源时代要来了
人工智能