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

    OpenWeb Android SDK



Implementation

Use the following steps to initialize the SDK and configure the Conversation:

  1. 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>

  1. In the article RecyclerView.Adapter, define an instance variable to hold the fragment.
private var spotImFragment: Fragment? = null
private var fragmentManger: FragmentManager? = null

  1. 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()
}

  1. Add getItemCount().
override fun getItemCount(): Int {
        return if (this.spotImFragment != null) {
            originalRecyclerViewSize + 1
    } else {
            originalRecyclerViewSize
    }
}

  1. Add getItemViewType(). This method returns a unique item type for the last RecyclerView item.
override fun getItemViewType(position: Int): Int {
        return if (position < originalRecyclerViewSize) {
            YOUR_ITEM_TYPE
    } else { // Last item
            SPOT_IM_ITEM_TYPE
    }
}

  1. 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) {}
    }
}

  1. 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)
}

  1. In onCreate() of an article activity, get the Pre-Conversation fragment and call setSpotImFragment().

📘

If RecycleView is embedded within a fragment, set childFragmentManager to avoid using the Activity FragmentManager.

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 ...
          }
})

  1. (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)
}


Did this page help you?