Pre-Conversation as a RecyclerView item

Configure the Pre-Conversation in your RecyclerView Adapter.

We supports implementing the Conversation as the last item in your RecyclerView Adapter.

If your article is not implemented in a RecyclerView, you can use the regular implementation of the Conversation as a Fragment in your Activity or Fragment.


Implementation

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

  1. In onCreate() of the application class, initialize the OpenWeb SDK with your Spot_ID.
SpotIm.init(
    this,
    "SPOT_ID"
)
SpotIm.init(
    context,
    "SPOT_ID"
);

  1. In your article RecyclerView.Adapter, create a method to set the SpotIm Fragment and update the data set. (you also need to define an instance var to hold the fragment)
private var spotImFragment: Fragment? = null
private var fragmentManger: FragmentManager? = null
  
fun setSpotImFragment(spotImFragment: Fragment, fragmentManger: FragmentManager = null) {
    this.spotImFragment = spotImFragment
    this.fragmentManger = fragmentManger
    notifyDataSetChanged()
}
private Fragment spotImFragment = null;
private FragmentManager fragmentManager = null;


public void setSpotImFragment(Fragment spotImFragment) {
        setSpotImFragment(spotImFragment, null)
}

public void setSpotImFragment(Fragment spotImFragment, FragmentManager fragmentManager) {
    this.spotImFragment = spotImFragment;
        this.fragmentManager = fragmentManager;
    this.notifyDataSetChanged();
}

  1. In onCreate() of an article activity, get the pre-conversation fragment and call your adapter setSpotImFragment() method (see step #1)

📘

RecycleView embedded within a fragment

In case RecycleView is embedded within a fragment it is recommended to 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 ...
          }
})
SpotIm.getPreConversationFragment(CONVERSATION_ID,new SpotCallback<Fragment>() {
    @Override 
    public void onSuccess(Fragment fragment) { 
        yourAdapter.setSpotImFragment(fragment);
       //within a fragment 
       //yourAdapter.setSpotImFragment(fragment, childFragmentManager)
    } 
    
    @Override 
    public void onFailure(SpotException exception) {    
        //doSomething...
    } 
});

  1. Add item_spotim_fragment_container XML file 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 your RecyclerView.Adapter getItemCount() method.
override fun getItemCount(): Int {
        return if (this.spotImFragment != null) {
            originalRecyclerViewSize + 1
    } else {
            originalRecyclerViewSize
    }
}
@Override
public int getItemCount() {
        return ORIGINAL_RECYCLE_VIEW_SIZE + (spotImFragment == null ? 0 : 1);
}

  1. In your RecyclerView.Adapter getItemViewType() method. Return a unique item type for your last RecyclerView item.
override fun getItemViewType(position: Int): Int {
        return if (position < originalRecyclerViewSize) {
            YOUR_ITEM_TYPE
    } else { // Last item
            SPOT_IM_ITEM_TYPE
    }
}
@Override
public int getItemViewType(int position) {
        if (position < originalRecyclerViewSize) {
      return YOUR_ITEM_TYPE;
    } else { // Last item
      return SPOT_IM_ITEM_TYPE;
    }
}

  1. In your RecyclerView.Adapter 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) {}
    }
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    
    // your implementation ...
  
    if (viewType == SPOT_IM_ITEM_TYPE) {
        View v = inflater.inflate(R.layout.item_spotim_fragment_container, parent, false);
        SPFragmentConstraintLayout fragmentContainerLayout = (SPFragmentConstraintLayout) v;
        fragmentContainerLayout.fragmentManager = (this.fragmentManager != null) ? this.fragmentManager : ((AppCompatActivity) parent.getContext()).getSupportFragmentManager();
        fragmentContainerLayout.spotImFragment = this.spotFragment;
        return new RecyclerView.ViewHolder(v) {};
    }
}

  1. In your RecyclerView.Adapter onViewAttachedToWindow() method.
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
    if (holder.itemViewType == SPOT_IM_ITEM_TYPE){
        val fragmentContainerView = holder.itemView as SPFragmentConstraintLayout
        fragmentContainerView.onViewAttachedToWindow()
    }
    super.onViewAttachedToWindow(holder)
}
@Override
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
    if (holder.getItemViewType() == SPOT_IM_ITEM_TYPE) {
        SPFragmentConstraintLayout fragmentContainerLayout = (SPFragmentConstraintLayout) holder.itemView;
        fragmentContainerLayout.onViewAttachedToWindow();
    }
    super.onViewAttachedToWindow(holder);
}

Did this page help you?