title: RecyclerView.addOnScrolledToEnd and SwipeRefreshLayout.setOnRefreshListener is not working correct with Kotlin
body: <p>I had implemented <code>RecyclerView.addOnScrolledToEnd</code> to get next set of data from server using async task. It is working fine when just used RecyclerView.addOnScrolledToEnd. But here the my problem is once I reached end and I did <code>SwipeRefreshLayout.setOnRefreshListener</code> (here resetting the data to fresh records) the <code>RecyclerView.addOnScrolledToEnd</code> is not calling and pagination is not working in this scenario.</p>
  
  <p><strong>Fragment</strong></p>
  
  <pre><code>    class RecentNewsFragment : Fragment(), LoaderManager.LoaderCallbacks&lt;Cursor&gt;, NewsAdapter.OnNewsCallBack, OnRefreshCallback {
      private var rlProgressContainer: View? = null
      private var listContainer: View? = null
      private var pbViewMore: ProgressBar? = null
      private var swipeRefreshLayout: SwipeRefreshLayout? = null
      private var srlNoMessageRefreshLayout: SwipeRefreshLayout? = null
      private var newsAdapter: NewsAdapter? = null
      private var pageLimit: Int = 10
      private var pageOffset: Int = 0
      private var totalNewsCount: Int = 0
      private var isAsyncDone: Boolean = false
  
      private val mRecentNewsBroadCast = object : BroadcastReceiver() {
          override fun onReceive(context: Context, intent: Intent) {
              // Get extra data included in the Intent
              restartLoader()
  
          }
      }
  
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                savedInstanceState: Bundle?): View? {
          // Inflate the layout for this fragment
          val view = inflater.inflate(R.layout.fragment_recent_news, container, false)
          initView(view)
          return view
      }
  
      override fun onResume() {
          super.onResume()
          if(!Helper.isConnected(context!!)) {
              Toast.makeText(context!!, context?.getString(R.string.no_connection), Toast.LENGTH_SHORT).show()
          }
      }
  
      private fun initView(view: View) {
          rlProgressContainer = view.findViewById(R.id.rl_progressContainer)
          listContainer = view.findViewById(R.id.ll_list_container)
          pbViewMore = view.findViewById(R.id.pb_view_more)
          newsAdapter = NewsAdapter(context!!, null, this)
          val recyclerView = view.findViewById&lt;RecyclerView&gt;(R.id.recycler_view)
          recyclerView.layoutManager = LinearLayoutManager(context)
          recyclerView.adapter = newsAdapter
  
          recyclerView.addItemDecoration(DividerItemDecoration(context!!, DividerItemDecoration.VERTICAL))
          recyclerView.setHasFixedSize(true)
          recyclerView.itemAnimator = DefaultItemAnimator()
  
          pbViewMore!!.visibility = View.GONE
          srlNoMessageRefreshLayout = view.findViewById(R.id.srl_activities_no_message)
          srlNoMessageRefreshLayout!!.visibility = View.GONE
          swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_feeds_page)
  
          val pullToRefresh = PullToRefresh()
          swipeRefreshLayout!!.setColorSchemeResources(android.R.color.holo_green_dark, android.R.color.holo_red_dark, android.R.color.holo_orange_dark, android.R.color.holo_purple)
          swipeRefreshLayout!!.setOnRefreshListener(pullToRefresh)
  
          srlNoMessageRefreshLayout!!.setColorSchemeResources(android.R.color.holo_green_dark, android.R.color.holo_red_dark, android.R.color.holo_orange_dark, android.R.color.holo_purple)
          srlNoMessageRefreshLayout!!.setOnRefreshListener(pullToRefresh)
          listContainer!!.visibility = View.GONE
          rlProgressContainer!!.visibility = View.VISIBLE
  
          loaderManager.initLoader(Constants.URL_RECENT_NEWS_LOADER, arguments, this)
  
          startAsync(pageLimit, pageOffset)
  
          fun RecyclerView.addOnScrolledToEnd(onScrolledToEnd: () -&gt; Unit){
  
              this.addOnScrollListener(object: RecyclerView.OnScrollListener(){
  
                  private val visibleThreshold = 5
  
                  private var loading = true
                  private var previousTotal = 0
  
                  override fun onScrollStateChanged(recyclerView: RecyclerView,
                                                    newState: Int) {
  
                      with(layoutManager as LinearLayoutManager){
  
                          val visibleItemCount = childCount
                          val totalItemCount = itemCount
                          val firstVisibleItem = findFirstVisibleItemPosition()
  
                          if (loading &amp;&amp; totalItemCount &gt; previousTotal){
  
                              loading = false
                              previousTotal = totalItemCount
                          }
  
                          if(!loading &amp;&amp; (totalItemCount - visibleItemCount) &lt;= (firstVisibleItem + visibleThreshold)){
  
                              onScrolledToEnd()
                              loading = true
                          }
                      }
                  }
              })
          }
  
          recyclerView.addOnScrolledToEnd {
              //What you want to do once the end is reached
              pbViewMore!!.visibility = View.VISIBLE
              startAsync(pageLimit, pageOffset)
          }
  
      }
  
  
      private inner class PullToRefresh : SwipeRefreshLayout.OnRefreshListener {
          override fun onRefresh() {
              pbViewMore!!.visibility = View.GONE
              pageOffset = 0
              startAsync(pageLimit, pageOffset)
              srlNoMessageRefreshLayout!!.isRefreshing = false
              swipeRefreshLayout!!.isRefreshing = false
          }
      }
  
      private fun startAsync(pageLimit: Int, pageOffset: Int) {
          if(Helper.isConnected(context!!)) {
              val recentAsync = RecentNewsAsync(context = this, pageLimit = pageLimit, pageOffset = pageOffset)
              recentAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
          } else {
              isAsyncDone = true
          }
      }
  
      fun restartLoader() {
          if(isAdded)
              loaderManager.restartLoader(Constants.URL_RECENT_NEWS_LOADER, null, this)
      }
  
      override fun onCreateLoader(id: Int, args: Bundle?): Loader&lt;Cursor&gt; {
          return NewsVo.getCursorLoader(context!!, NewsVo.NewsType.RECENT, 0)
      }
  
      override fun onLoadFinished(loader: Loader&lt;Cursor&gt;, cursor: Cursor?) {
          if (loader.id == Constants.URL_RECENT_NEWS_LOADER) {
              if(isAsyncDone) {
                  listContainer!!.visibility = View.VISIBLE
                  rlProgressContainer!!.visibility = View.GONE
              }
              if (cursor != null) {
                  newsAdapter!!.swapCursor(cursor)
                  newsAdapter!!.notifyDataSetChanged()
                  if (isAsyncDone &amp;&amp; cursor.count &gt; 0) {
                      srlNoMessageRefreshLayout!!.visibility = View.GONE
                      swipeRefreshLayout!!.visibility = View.VISIBLE
                  } else if(isAsyncDone){
                      srlNoMessageRefreshLayout!!.visibility = View.VISIBLE
                      swipeRefreshLayout!!.visibility = View.GONE
                  }
              }
          }
      }
  
      override fun onLoaderReset(loader: Loader&lt;Cursor&gt;) {
          newsAdapter!!.swapCursor(null)
  
      }
  
  
      private class RecentNewsAsync internal constructor(context: RecentNewsFragment, private val pageLimit: Int, private val pageOffset: Int) : AsyncTask&lt;Void, Void, Response&lt;ArrayList&lt;NewsVo&gt;&gt;&gt;() {
  
          private val context: WeakReference&lt;RecentNewsFragment&gt; = WeakReference(context)
  
          override fun doInBackground(vararg p0: Void?): Response&lt;ArrayList&lt;NewsVo&gt;&gt; {
              var response = Response&lt;ArrayList&lt;NewsVo&gt;&gt;()
              try {
                  val lphService = LPHServiceFactory.getCALFService(context.get()?.context!!)
                  response = lphService.recentNews(pageLimit, pageOffset)
              } catch (e: LPHException) {
                  e.printStackTrace()
                  response.setThrowable(e)
              } catch (e: JSONException) {
                  e.printStackTrace()
                  response.setThrowable(e)
              } catch (e: IOException) {
                  e.printStackTrace()
                  response.setThrowable(e)
              }
  
              return response
          }
  
          override fun onPostExecute(response: Response&lt;ArrayList&lt;NewsVo&gt;&gt;) {
              super.onPostExecute(response)
              context.get()?.isAsyncDone = true
              context.get()?.pbViewMore?.visibility = View.GONE
              if (response.isSuccess()) {
                  context.get()?.totalNewsCount = response.getMetaData() as Int
                  if(context.get() != null)
                      context.get()!!.pageOffset += response.getResult()?.size!!
  
                  context.get()?.restartLoader()
              }
          }
      }
  
      override fun onFavoriteClick(newsId: Int, isFavorite: Boolean) {
          if(Helper.isConnected(context!!)) {
              val weakReferenceContext = WeakReference(this.context!!)
              val markFavoriteAsync = Helper.MarkFavoriteAsync(weakReferenceContext, newsId, isFavorite, this, 0)
              markFavoriteAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
          } else {
              Helper.showConfirmationAlertTwoButton(context!!, context!!.getString(R.string.internet_warning), object : ConfirmationAlertCallback {
                  override fun onPositiveButtonClick() {
                      onFavoriteClick(newsId, isFavorite)
                  }
  
                  override fun onNegativeButtonClick() {
  
                  }
  
                  override fun onNeutralButtonClick() {
  
                  }
              })
          }
      }
  
      override fun setRead(newsId: Int, isRead: Boolean) {
          if(Helper.isConnected(context!!)) {
              val weakReferenceContext = WeakReference(context!!)
              val markReadAsync = Helper.MarkReadAsync(weakReferenceContext, newsId, isRead, this, 0)
              markReadAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
          } else {
              Helper.showConfirmationAlertTwoButton(context!!, context!!.getString(R.string.internet_warning), object : ConfirmationAlertCallback {
                  override fun onPositiveButtonClick() {
                      setRead(newsId, isRead)
                  }
  
                  override fun onNegativeButtonClick() {
  
                  }
  
                  override fun onNeutralButtonClick() {
  
                  }
              })
          }
      }
  
      override fun onViewClick(newsId: Int) {
          val newsDetailIntent = Intent(context, NewsDetailActivity::class.java)
          newsDetailIntent.putExtra(Constants.BUNDLE_NEWS_ID, newsId)
          startActivityForResult(newsDetailIntent, Constants.REQUEST_CODE_RECENT_NEWS)
      }
  
      override fun onActivityCreated(savedInstanceState: Bundle?) {
          super.onActivityCreated(savedInstanceState)
          activity!!.registerReceiver(mRecentNewsBroadCast, IntentFilter(Constants.BROADCAST_RECENT_NEWS))
      }
  
      override fun onDetach() {
          super.onDetach()
          activity!!.unregisterReceiver(mRecentNewsBroadCast)
      }
  
      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
          super.onActivityResult(requestCode, resultCode, data)
          if (requestCode == Constants.REQUEST_CODE_RECENT_NEWS &amp;&amp; resultCode == Activity.RESULT_OK) {
              restartLoader()
              val intent1 = Intent(Constants.BROADCAST_FAVORITE_NEWS)
              context!!.sendBroadcast(intent1)
  
              val intent2 = Intent(Constants.BROADCAST_CATEGORIES)
              context!!.sendBroadcast(intent2)
          }
      }
  
      override fun onRefresh() {
          restartLoader()
          val intent1 = Intent(Constants.BROADCAST_FAVORITE_NEWS)
          context!!.sendBroadcast(intent1)
  
          val intent2 = Intent(Constants.BROADCAST_CATEGORIES)
          context!!.sendBroadcast(intent2)
      }
  
      companion object {
  
          /**
           * Use this factory method to create a new instance of
           * this fragment using the provided parameters.
           *
           * @return A new instance of fragment RecentNewsFragment.
           */
          fun newInstance(): RecentNewsFragment {
              return RecentNewsFragment()
          }
      }
  }
  </code></pre>
  
  <p><strong>Layout file:</strong></p>
  
  <pre><code>    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:id="@+id/ll_list_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"&gt;
  
          &lt;android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/srl_activities_no_message"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:visibility="gone"&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="horizontal"&gt;
  
                  &lt;TextView
                      android:id="@+id/tv_no_data_found"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center"
                      android:enabled="false"
                      android:fontFamily="sans-serif-light"
                      android:gravity="center"
                      android:text="@string/no_data"
                      android:textAppearance="?android:attr/textAppearanceMedium"
                      android:textColor="@android:color/black" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/android.support.v4.widget.SwipeRefreshLayout&gt;
  
          &lt;android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/swipe_refresh_feeds_page"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:fitsSystemWindows="false"
              android:visibility="visible"&gt;
  
              &lt;RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"&gt;
  
                  &lt;android.support.v7.widget.RecyclerView
                      android:id="@+id/recycler_view"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent" /&gt;
  
                  &lt;ProgressBar
                      android:id="@+id/pb_view_more"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentBottom="true"
                      android:layout_centerInParent="true"
                      android:layout_gravity="center_horizontal"
                      android:theme="@style/ProgressBarTheme"
                      android:visibility="gone" /&gt;
  
              &lt;/RelativeLayout&gt;
  
  
          &lt;/android.support.v4.widget.SwipeRefreshLayout&gt;
      &lt;/LinearLayout&gt;
  
      &lt;RelativeLayout
          android:id="@+id/rl_progressContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:visibility="gone"&gt;
  
          &lt;ProgressBar
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_centerInParent="true"
              android:layout_centerVertical="true"
              android:theme="@style/ProgressBarTheme" /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>