FastComments.com

在您的 Android 应用中添加评论


这是 FastComments 的官方 Android 库。

用于 Android 的 FastComments 评论小部件

仓库

在 GitHub 上查看


功能 Internal Link

  • 🔄 实时评论与即时更新
  • 📱 原生 Android UI 组件
  • 🧵 支持回复的线程式讨论
  • 👤 安全的 SSO 身份验证
  • 👍 带可定制样式的投票系统
  • 🔔 用户通知与在线状态
  • 🔍 评论审核功能
  • 📱 社交动态集成
  • ♾️ 无限滚动分页
  • 🎨 全面主题定制

要求 Internal Link

  • Android SDK 26+ (Android 8.0 Oreo 或更高版本)
  • Java 8+

安装 Internal Link

将 FastComments SDK 添加到您应用的 build.gradle.kts 文件:

dependencies {
    implementation("com.fastcomments:sdk:0.0.1")
}

确保您的项目的 settings.gradle.kts 中包含 Repsy 仓库:

dependencyResolutionManagement {
    repositories {
        maven {
            url = uri("https://repo.repsy.io/mvn/winrid/fastcomments")
        }
        // other repositories...
    }
}

基本用法 Internal Link

1. 将 FastCommentsView 添加到您的布局

<com.fastcomments.sdk.FastCommentsView
    android:id="@+id/commentsView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2. 初始化并配置 SDK

// 配置 SDK
val config = CommentWidgetConfig(
    "your-tenant-id", 
    "page-url-id", 
    "Page Title", 
    "yourdomain.com", 
    "Site Name"
)

// 其他配置选项
config.voteStyle = VoteStyle.UpDown // 或 VoteStyle.Heart
config.enableInfiniteScrolling = true
config.hasDarkBackground = true // 用于暗色模式支持

// 初始化 SDK
val sdk = FastCommentsSDK(config)

// 在布局中查找评论视图
val commentsView = findViewById<FastCommentsView>(R.id.commentsView)

// 为视图设置 SDK 实例
commentsView.setSDK(sdk)

// 加载评论
commentsView.load()

安全单点登录(SSO)认证 Internal Link

为您的用户实现安全身份验证:

// 创建用户数据(理想情况下在您的服务器上)
val userData = SecureSSOUserData(
    "user-id",
    "user@example.com",
    "User Name",
    "https://path-to-avatar.jpg"
)

// 生成 SSO 令牌(应在服务器端完成!)
val sso = FastCommentsSSO.createSecure("YOUR_API_KEY", userData)
val token = sso.prepareToSend()

// 添加到配置
config.sso = token

信息流集成 Internal Link

显示带评论的社交媒体风格信息流:

// 配置 SDK
CommentWidgetConfig config = new CommentWidgetConfig();
config.tenantId = "your-tenant-id";
config.urlId = "page-url-id";

// 初始化 Feed SDK
FastCommentsFeedSDK feedSDK = new FastCommentsFeedSDK(config);

// 设置信息流视图
FastCommentsFeedView feedView = findViewById(R.id.feedView);
feedView.setSDK(feedSDK);

// 设置交互监听器
feedView.setFeedViewInteractionListener(new FastCommentsFeedView.OnFeedViewInteractionListener() {
    @Override
    public void onFeedLoaded(List<FeedPost> posts) {
        // 信息流加载成功
    }

    @Override
    public void onFeedError(String errorMessage) {
        // 处理错误
    }

    @Override
    public void onPostSelected(FeedPost post) {
        // 用户选择了帖子
    }

    @Override
    public void onCommentsRequested(FeedPost post) {
        // 显示该帖子的评论
        CommentsDialog dialog = new CommentsDialog(context, post, feedSDK);
        dialog.show();
    }
});

// 加载信息流
feedView.load();

实时聊天集成 Internal Link

向您的应用添加实时聊天界面:

// 将 LiveChatView 添加到您的布局 XML
// <com.fastcomments.sdk.LiveChatView
//     android:id="@+id/liveChatView"
//     android:layout_width="match_parent"
//     android:layout_height="match_parent" />

// Create a configuration for the SDK
val config = CommentWidgetConfig().apply {
    tenantId = "your-tenant-id"
    urlId = "chat-room-identifier" 
    pageTitle = "Chat Room Name"
}
LiveChatView.setupLiveChatConfig(config)

// Optional: Add user authentication
val userData = SimpleSSOUserData(
    "User Name",
    "user@example.com",
    "https://path-to-avatar.jpg"
)
val sso = FastCommentsSSO(userData)
config.sso = sso.prepareToSend()

// Initialize the SDK
val sdk = FastCommentsSDK().configure(config)

// Set up the live chat view
val liveChatView = findViewById<LiveChatView>(R.id.liveChatView)
liveChatView.setSDK(sdk)
liveChatView.load()

// 别忘记生命周期处理
override fun onResume() {
    super.onResume()
    sdk.refreshLiveEvents()
}

override fun onDestroy() {
    super.onDestroy()
    sdk.cleanup()
}

示例项目 Internal Link

查看这些演示实现:

配置选项 Internal Link

该 SDK 通过 CommentWidgetConfig 类提供许多配置选项:

OptionDescription
tenantId您的 FastComments 帐户 ID
urlId表示当前页面的 ID
sso用于身份验证的 SSO 令牌
allowAnon允许匿名评论
voteStyleUpDown 或 Heart 的投票样式
hideAvatars隐藏用户头像
hasDarkBackground指示深色模式
customCSS自定义 CSS 样式
enableInfiniteScrolling启用无限滚动分页
readonly禁用发表评论但显示评论
disableVoting禁用投票功能
disableLiveCommenting禁用实时更新

全面的主题自定义 Internal Link

所有 FastComments SDK 中的按钮和 UI 元素都支持主题化。使用 FastCommentsTheme.Builder 完全控制您应用的品牌样式。

编程式主题(推荐)

val theme = FastCommentsTheme.Builder()
    // 操作按钮:发送、投票、菜单、点赞/分享 按钮
    .setActionButtonColor(Color.parseColor("#FF1976D2"))
    
    // 回复按钮:评论回复按钮  
    .setReplyButtonColor(Color.parseColor("#FF4CAF50"))
    
    // 切换按钮:显示/隐藏回复 按钮
    .setToggleRepliesButtonColor(Color.parseColor("#FFFF5722"))
    
    // 加载更多按钮:分页按钮
    .setLoadMoreButtonTextColor(Color.parseColor("#FF9C27B0"))
    
    .setPrimaryColor(Color.parseColor("#FF6200EE"))
    .setLinkColor(Color.parseColor("#FF1976D2"))
    .setDialogHeaderBackgroundColor(Color.parseColor("#FF333333"))
    .build()

// 应用主题
sdk.setTheme(theme)

快速颜色覆盖

在您的 colors.xml 中覆盖颜色资源以实现简单的品牌定制:

<!-- 在您的应用的 res/values/colors.xml 中 -->
<resources>
    <!-- 更改所有主要 UI 元素 -->
    <color name="primary">#FF1976D2</color>
    
    <!-- 或自定义特定的按钮类型 -->
    <color name="fastcomments_action_button_color">#FF1976D2</color>
    <color name="fastcomments_reply_button_color">#FF4CAF50</color>
    <color name="fastcomments_toggle_replies_button_color">#FFFF5722</color>
    <color name="fastcomments_load_more_button_text_color">#FF9C27B0</color>
</resources>

主题按钮覆盖范围

SDK 中的每个按钮都支持主题化:

  • 发送按钮、投票按钮、菜单按钮、回复按钮
  • 显示/隐藏回复按钮、加载更多按钮
  • Feed 操作按钮(点赞、评论、分享)
  • 对话框按钮(提交、取消、保存)
  • 信息流帖子中的动态任务按钮

有关详细的主题化文档,请参见 THEMING.md.

内存管理 Internal Link

防止内存泄漏

在 Activity 或 Fragment 中使用 FastComments 视图时,为防止内存泄漏,当视图不再需要时务必调用 cleanup()

在 Activity 中:

@Override
protected void onDestroy() {
    super.onDestroy();
    // 清理 FastComments 视图以防止内存泄漏
    if (feedView != null) {
        feedView.cleanup();
    }
    if (commentsView != null) {
        commentsView.cleanup();
    }
}

在 Fragment 中:

@Override
public void onDestroyView() {
    super.onDestroyView();
    // 在 fragment 视图被销毁时清理 FastComments 视图
    if (feedView != null) {
        feedView.cleanup();
        feedView = null;
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    // 在 fragment 被销毁时进行额外清理
    if (feedSDK != null) {
        feedSDK.cleanup();
        feedSDK = null;
    }
}

切换 Fragment 时:

// 在替换或移除包含 FastComments 视图的 fragment 之前
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (currentFragment instanceof YourFragmentWithFeedView) {
    ((YourFragmentWithFeedView) currentFragment).cleanupFeedView();
}

// 然后继续进行 fragment 事务
getSupportFragmentManager().beginTransaction()
    .replace(R.id.container, newFragment)
    .commit();

重要:务必调用 cleanup() 方法以防止内存泄漏,尤其是在以下情况下:

  • Activity 被销毁时
  • Fragment 视图被销毁时
  • 在 Fragment 之间切换时
  • 从包含 FastComments 组件的屏幕导航离开时

需要帮助?

如果您在使用 Android 库时遇到任何问题或有任何疑问,请:

贡献

欢迎贡献!请访问 GitHub 仓库 查看贡献指南。