融云 会话列表以及会话界面的集成

 

参考资料:https://www.rongcloud.cn/docs/android.html#integration_conversation_list_config

一.静态注册
1.在需要显示会话列表的Activity布局文件中,直接引用:

注意 android:name 固定为融云的 ConversationListFragment。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/conversationlist"
        android:name="io.rong.imkit.fragment.ConversationListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2.配置 intent-filter:

融云 SDK 是通过隐式调用的方式来实现界面跳转的。因此您需要在 AndroidManifest.xml 中,您的会话列表 Activity 下面配置 intent-filter,其中,android:host 是您应用的包名,需要手动修改,其他请保持不变。

<!--会话列表-->
<activity
    android:name="io.rong.fast.activity.ConversationListActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden|adjustResize">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <data
            android:host="io.rong.fast"
            android:pathPrefix="/conversationlist"
            android:scheme="rong" />
    </intent-filter>
</activity>

3.启动方式:

HashMap<String, Boolean> hashMap = new HashMap<>();
        //会话类型 以及是否聚合显示
        hashMap.put(Conversation.ConversationType.PRIVATE.getName(),false);
        hashMap.put(Conversation.ConversationType.PUSH_SERVICE.getName(),true);
        hashMap.put(Conversation.ConversationType.SYSTEM.getName(),true);
        RongIM.getInstance().startConversationList(this,hashMap);

二、动态注册:

布局文件:

    <!--会话列表-->
    <FrameLayout
        android:id="@+id/rong_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/line1"
        />
//会话列表
        ConversationListFragment conversationListFragment = new ConversationListFragment();
        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
.appendPath("conversationlist")       .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话,该会话聚合显示
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//设置系统会话,该会话非聚合显示
                .build();
        conversationListFragment.setUri(uri);

        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.rong_container,conversationListFragment);
        transaction.commit();

三、会话界面配置
1. 会话 Fragment 跟会话列表是完全一致的,您可以用同样的方式快速的配置好。

配置布局文件

这是您的会话 Activity 对应的布局文件 conversation.xml,注意 android:name 固定为融云的 ConversationFragment。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/conversation"
        android:name="io.rong.imkit.fragment.ConversationFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

2.配置 intent-filter

在 AndroidManifest.xml 中,会话 Activity 下面配置 intent-filter。 注意请修改 android:host 为您应用的包名,其他保持不变。

<!--会话界面-->
 <activity
     android:name="io.rong.fast.activity.ConversationActivity"
     android:screenOrientation="portrait"
     android:windowSoftInputMode="stateHidden|adjustResize">

     <intent-filter>
         <action android:name="android.intent.action.VIEW" />

         <category android:name="android.intent.category.DEFAULT" />

         <data
             android:host="io.rong.fast"
             android:pathPrefix="/conversation/"
             android:scheme="rong" />
     </intent-filter>
 </activity>

3.会话界面设置顶部显示好友昵称:

/**
 * crate by longShun on 2017.2.18
 * 每一个会话界面
 */
public class ConversationActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_messages);

        //会话界面 对方id
        //String targetId = getIntent().getData().getQueryParameter("targetId");
        //对方 昵称
        String title = getIntent().getData().getQueryParameter("title");
        if (!TextUtils.isEmpty(title)){
            //todo 设置标题为对方昵称
        }
    }
}

4.启动方式

1. 自动启动:当我们点击会话列表中某个会话时,融云会触发自己实现的点击事件,隐式启动会话界面,从而进入某一个会话界面,所以我们可以不用去处理会话列表item的点击事件;
2.参考下面手动启动的方式

四、聚合会话列表
1.配置布局文件

这是您的聚合会话列表 Activity 对应的布局文件:subconversationlist.xml。 注意 android:name 固定为融云的 SubConversationListFragment。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/subconversationlist"
        android:name="io.rong.imkit.fragment.SubConversationListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

2.配置 intent-filter

在 AndroidManifest.xml 中, 聚合会话 Activity 下面配置 intent-filter。 注意请修改 android:host 为您应用的包名,其他保持不变。

<!--聚合会话列表-->
 <activity
     android:name="io.rong.fast.activity.SubConversationListActivtiy"
     android:screenOrientation="portrait"
     android:windowSoftInputMode="stateHidden|adjustResize">

     <intent-filter>
         <action android:name="android.intent.action.VIEW" />

         <category android:name="android.intent.category.DEFAULT" />

         <data
             android:host="io.rong.fast"
             android:pathPrefix="/subconversationlist"
             android:scheme="rong" />
     </intent-filter>
 </activity>

五、手动启动各种界面

/**
 * <p>启动会话界面。</p>
 * <p>使用时,可以传入多种会话类型 {@link io.rong.imlib.model.Conversation.ConversationType} 对应不同的会话类型,开启不同的会话界面。
 * 如果传入的是 {@link io.rong.imlib.model.Conversation.ConversationType#CHATROOM},sdk 会默认调用
 * {@link RongIMClient#joinChatRoom(String, int, RongIMClient.OperationCallback)} 加入聊天室。
 * 如果你的逻辑是,只允许加入已存在的聊天室,请使用接口 {@link #startChatRoomChat(Context, String, boolean)} 并且第三个参数为 true</p>
 *
 * @param context          应用上下文。
 * @param conversationType 会话类型。
 * @param targetId         根据不同的 conversationType,可能是用户 Id、讨论组 Id、群组 Id 或聊天室 Id。
 * @param title            聊天的标题,开发者可以在聊天界面通过 intent.getData().getQueryParameter("title") 获取该值, 再手动设置为标题。
 */
public void startConversation(Context context, Conversation.ConversationType conversationType, String targetId, String title)



/**
 * 启动会话列表界面。
 *
 * @param context               应用上下文。
 * @param supportedConversation 定义会话列表支持显示的会话类型,及对应的会话类型是否聚合显示。
 *                              例如:supportedConversation.put(Conversation.ConversationType.PRIVATE.getName(), false) 非聚合式显示 private 类型的会话。
 */
public void startConversationList(Context context, Map<String, Boolean> supportedConversation)



/**
 * 启动聚合后的某类型的会话列表。<br> 例如:如果设置了单聊会话为聚合,则通过该方法可以打开包含所有的单聊会话的列表。
 *
 * @param context          应用上下文。
 * @param conversationType 会话类型。
 */
public void startSubConversationList(Context context, Conversation.ConversationType conversationType)

完 

发布了54 篇原创文章 · 获赞 20 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/csdn15002274757/article/details/104171183