title: How to load the fragments when the tab is selected or page is swiped in Kotlin
body: <blockquote>
    <p>Activity</p>
  </blockquote>
  
  <pre><code>    class NewPaymentActivity : AppCompatActivity(),SummaryFragment.OnFragmentInteractionListener,ReceiptDetailsFragment.OnFragmentInteractionListener{
  
      internal lateinit var viewpageradapter: ViewPagerAdapter //Declare PagerAdapter
  
      private var tabLayout: TabLayout? = null
      var viewPager: ViewPager? = null
      var toolbar: Toolbar? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
  
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_new_payment)
          viewpageradapter= ViewPagerAdapter(supportFragmentManager)
  
          viewPager = findViewById(R.id.viewPager) as ViewPager
          setupViewPager(viewPager!!)
  
          tabLayout = findViewById(R.id.tab_layout) as TabLayout
          tabLayout!!.setupWithViewPager(viewPager)
  
  //      tabLayout!!.setSelectedTabIndicatorColor(getResources().getColor(R.color.t));
          tabLayout!!.setSelectedTabIndicatorHeight(0)
  
  
  
  
          val headerView = (getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
                  .inflate(R.layout.custom_tabs_for_payments, null, false)
  
          val receiptDetails = headerView.findViewById(R.id.tb_receipt_details) as TextView
          val paymentDetails = headerView.findViewById(R.id.tb_payment_details) as TextView
          val invoiceDetails = headerView.findViewById(R.id.tb_invoice_details) as TextView
          val summary = headerView.findViewById(R.id.tb_summary) as TextView
  
          receiptDetails.setTextColor(resources.getColor(R.color.white))
          val drawable = receiptDetails.getBackground() as GradientDrawable
          drawable.setColor(resources.getColor(R.color.received_payments))
  
          paymentDetails.setTextColor(resources.getColor(R.color.main))
          val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
          drawablepaymentDetails.setColor(resources.getColor(R.color.white))
  
          invoiceDetails.setTextColor(resources.getColor(R.color.main))
          val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
          drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
  
          summary.setTextColor(resources.getColor(R.color.main))
          val summarydrawable = summary.getBackground() as GradientDrawable
          summarydrawable.setColor(resources.getColor(R.color.white))
  
          tabLayout!!.getTabAt(0)!!.setCustomView(receiptDetails)
          tabLayout!!.getTabAt(1)!!.setCustomView(paymentDetails)
          tabLayout!!.getTabAt(2)!!.setCustomView(invoiceDetails)
          tabLayout!!.getTabAt(3)!!.setCustomView(summary)
  
          tabLayout!!.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
  
              override fun onTabSelected(tab: TabLayout.Tab?) {
  
                  if(tab!!.position==0)
                  {
                      receiptDetails.setTextColor(resources.getColor(R.color.white))
                      val drawable = receiptDetails.getBackground() as GradientDrawable
                      drawable.setColor(resources.getColor(R.color.received_payments))
  
                      paymentDetails.setTextColor(resources.getColor(R.color.main))
                      val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
                      drawablepaymentDetails.setColor(resources.getColor(R.color.white))
  
                      invoiceDetails.setTextColor(resources.getColor(R.color.main))
                      val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
                      drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
  
                      summary.setTextColor(resources.getColor(R.color.main))
                      val summarydrawable = summary.getBackground() as GradientDrawable
                      summarydrawable.setColor(resources.getColor(R.color.white))
                  }
                  else if(tab!!.position==1)
                  {
                      paymentDetails.setTextColor(resources.getColor(R.color.white))
                      val drawable = paymentDetails.getBackground() as GradientDrawable
                      drawable.setColor(resources.getColor(R.color.received_payments))
  
                      receiptDetails.setTextColor(resources.getColor(R.color.main))
                      val drawablereceiptDetails= receiptDetails.getBackground() as GradientDrawable
                      drawablereceiptDetails.setColor(resources.getColor(R.color.white))
  
                      invoiceDetails.setTextColor(resources.getColor(R.color.main))
                      val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
                      drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
  
                      summary.setTextColor(resources.getColor(R.color.main))
                      val summarydrawable = summary.getBackground() as GradientDrawable
                      summarydrawable.setColor(resources.getColor(R.color.white))
  
  
                  }
                  else if(tab!!.position==2)
                  {
                      invoiceDetails.setTextColor(resources.getColor(R.color.white))
                      val drawable = invoiceDetails.getBackground() as GradientDrawable
                      drawable.setColor(resources.getColor(R.color.received_payments))
  
                      receiptDetails.setTextColor(resources.getColor(R.color.main))
                      val drawablereceiptDetails= receiptDetails.getBackground() as GradientDrawable
                      drawablereceiptDetails.setColor(resources.getColor(R.color.white))
  
                      paymentDetails.setTextColor(resources.getColor(R.color.main))
                      val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
                      drawablepaymentDetails.setColor(resources.getColor(R.color.white))
  
                      summary.setTextColor(resources.getColor(R.color.main))
                      val summarydrawable = summary.getBackground() as GradientDrawable
                      summarydrawable.setColor(resources.getColor(R.color.white))
  
  
                  }
                  else if(tab!!.position==3)
                  {
                      summary.setTextColor(resources.getColor(R.color.white))
                      val drawable = summary.getBackground() as GradientDrawable
                      drawable.setColor(resources.getColor(R.color.received_payments))
  
                      receiptDetails.setTextColor(resources.getColor(R.color.main))
                      val drawablereceiptDetails = receiptDetails.getBackground() as GradientDrawable
                      drawablereceiptDetails.setColor(resources.getColor(R.color.white))
  
                      paymentDetails.setTextColor(resources.getColor(R.color.main))
                      val drawablepaymentDetails = paymentDetails.getBackground() as GradientDrawable
                      drawablepaymentDetails.setColor(resources.getColor(R.color.white))
  
                      invoiceDetails.setTextColor(resources.getColor(R.color.main))
                      val drawableinvoiceDetails = invoiceDetails.getBackground() as GradientDrawable
                      drawableinvoiceDetails.setColor(resources.getColor(R.color.white))
                  }
                  else {
  
                  }
  
              }
              override fun onTabUnselected(tab:TabLayout.Tab?){
              }
              override fun onTabReselected(tab:TabLayout.Tab?) {
              }
          })
  
  
      }
  
      private fun setupViewPager(viewPager: ViewPager) {
  
          val adapter = ViewPagerAdapter(supportFragmentManager)
          adapter.addFragment(ReceiptDetailsFragment(), "Receipt Details")
          adapter.addFragment(PaymentDetailsFragment(), "Payment Details")
          adapter.addFragment(InvoiceDetailsFragment(), "Invoice Details")
          adapter.addFragment(SummaryFragment(), "Summary")
          viewPager.adapter = adapter
  
  
  
      }
  
      internal inner class ViewPagerAdapter(manager: FragmentManager) : FragmentStatePagerAdapter(manager) {
  
          private val mFragmentList = ArrayList&lt;Fragment&gt;()
          private val mFragmentTitleList = ArrayList&lt;String&gt;()
  
          override fun getItem(position: Int): Fragment {
              return mFragmentList[position]
          }
  
          override fun getCount(): Int {
              return mFragmentList.size
          }
  
          fun addFragment(fragment: Fragment, title: String) {
              mFragmentList.add(fragment)
              mFragmentTitleList.add(title)
          }
  
          override fun getPageTitle(position: Int): CharSequence {
              return mFragmentTitleList[position]
          }
  
      }
  
      override fun onFragmentInteraction(jsonObject: JSONObject) {
          //session intialization
          val myPreference = Session(this)
          myPreference.setJsonObject(jsonObject.toString())
          viewPager!!.setCurrentItem(1,true)
  
  
      }
  
  
      override fun onFragmentInteraction(uri: Uri) {
  
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>Fragment</p>
  </blockquote>
  
  <pre><code>class PaymentDetailsFragment : Fragment() {
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
      val rootView = inflater.inflate(R.layout.fragment_payment_details, container, false)
  
      val replacingLayout = rootView.findViewById(R.id.replacing_layout) as LinearLayout
      val viewPaymentCheque = activity!!.layoutInflater.inflate(R.layout.payment_details_cheques, null)
      Log.e("test Object","test")
  
      replacingLayout.addView(viewPaymentCheque)
      val myPreference = Session(activity!!)
      Log.e("test Object", myPreference.getJsonObject())
      if(myPreference.getJsonObject()!="") {
  
          var obj = JSONObject(myPreference.getJsonObject());
          Log.e("test Object", obj.toString())
      }
  
  
  
      return rootView
  }
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p>I'm using android ViewPager and use for different fragments to load when I click separate tabs. But the issue is that when I click tabs fragments are not loading. when the activity loading all the fragments are loaded .then fragments are not changing. How can I solve this issue?I need to load fragment when I select tab in view pager.</p>
  </blockquote>