Add a Pre-Conversation as a RecyclerView item
Initialize the OpenWeb SDK and configure the Conversation in your Android app.
This approach allows you to implement a Conversation as the last item in a RecyclerView.Adapter
. When implemented, a user sees the following displayed below the article in the article Activity
or Fragment
:
- Preview of comments from the Conversation in a Pre-Conversation fragment
- Textbox to enter comments
- Button to show all comments
Requirement
Implementation
Use the following steps to initialize the SDK and configure the Conversation:
- Create and add item_spotim_fragment_container.xml to your project.
<?xml version="1.0" encoding="utf-8"?>
<spotIm.common.helpers.SPFragmentConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</spotIm.common.helpers.SPFragmentConstraintLayout>
- In the article
RecyclerView.Adapter
, define an instance variable to hold the fragment.
private var spotImFragment: Fragment? = null
private var fragmentManger: FragmentManager? = null
- Create a method to set the
spotImFragment
and update the data set.
fun setSpotImFragment(spotImFragment: Fragment, fragmentManger: FragmentManager = null) {
this.spotImFragment = spotImFragment
this.fragmentManger = fragmentManger
notifyDataSetChanged()
}
- Add
getItemCount()
.
override fun getItemCount(): Int {
return if (this.spotImFragment != null) {
originalRecyclerViewSize + 1
} else {
originalRecyclerViewSize
}
}
- Add
getItemViewType()
. This method returns a unique item type for the lastRecyclerView
item.
override fun getItemViewType(position: Int): Int {
return if (position < originalRecyclerViewSize) {
YOUR_ITEM_TYPE
} else { // Last item
SPOT_IM_ITEM_TYPE
}
}
- Add
onCreateViewHolder()
method.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// your implementation ...
if (viewType == SPOT_IM_ITEM_TYPE) {
val fragmentContainerView = inflater.inflate(R.layout.item_spotim_fragment_container, parent, false) as SPFragmentConstraintLayout
val fragmentManager = this.fragmentManger ?: (parent.context as AppCompatActivity).supportFragmentManager
fragmentContainerView.fragmentManager = fragmentManager
fragmentContainerView.spotImFragment = this.spotImFragment!!
return object : RecyclerView.ViewHolder(fragmentContainerView) {}
}
}
- Add
onViewAttachedToWindow()
.
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
if (holder.itemViewType == SPOT_IM_ITEM_TYPE){
val fragmentContainerView = holder.itemView as SPFragmentConstraintLayout
fragmentContainerView.onViewAttachedToWindow()
}
super.onViewAttachedToWindow(holder)
}
- In
onCreate()
of an article activity, get the Pre-Conversation fragment and callsetSpotImFragment()
.
If
RecycleView
is embedded within a fragment, setchildFragmentManager
to avoid using the ActivityFragmentManager
.
SpotIm.getPreConversationFragment(CONVERSATION_ID, object :
SpotCallback<Fragment> {
override fun onSuccess(fragment: Fragment) {
yourAdapter.setSpotImFragment(fragment)
//within a fragment
//yourAdapter.setSpotImFragment(fragment, childFragmentManager)
}
override fun onFailure(exception: SpotException) {
//do Something ...
}
})
- (Optional) Customize the Pre-Conversation fragment and Conversation.
Full RecyclerView.Adapter code
The following code sample shows steps 2-7 together.
private var spotImFragment: Fragment? = null
private var fragmentManger: FragmentManager? = null
fun setSpotImFragment(spotImFragment: Fragment, fragmentManger: FragmentManager = null) {
this.spotImFragment = spotImFragment
this.fragmentManger = fragmentManger
notifyDataSetChanged()
}
override fun getItemCount(): Int {
return if (this.spotImFragment != null) {
originalRecyclerViewSize + 1
} else {
originalRecyclerViewSize
}
}
override fun getItemViewType(position: Int): Int {
return if (position < originalRecyclerViewSize) {
YOUR_ITEM_TYPE
} else { // Last item
SPOT_IM_ITEM_TYPE
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// your implementation ...
if (viewType == SPOT_IM_ITEM_TYPE) {
val fragmentContainerView = inflater.inflate(R.layout.item_spotim_fragment_container, parent, false) as SPFragmentConstraintLayout
val fragmentManager = this.fragmentManger ?: (parent.context as AppCompatActivity).supportFragmentManager
fragmentContainerView.fragmentManager = fragmentManager
fragmentContainerView.spotImFragment = this.spotImFragment!!
return object : RecyclerView.ViewHolder(fragmentContainerView) {}
}
}
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
if (holder.itemViewType == SPOT_IM_ITEM_TYPE){
val fragmentContainerView = holder.itemView as SPFragmentConstraintLayout
fragmentContainerView.onViewAttachedToWindow()
}
super.onViewAttachedToWindow(holder)
}
Updated about 1 month ago
Did this page help you?