Created
March 13, 2021 11:48
-
-
Save rohitjakhar/446018cf43f087adff3f8ceeb7f10c36 to your computer and use it in GitHub Desktop.
Load Data when reach at bottom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rohitjakhar.blogx.ui.home | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.AbsListView | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.ViewModelProvider | |
import androidx.lifecycle.lifecycleScope | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import com.rohitjakhar.blogx.adapters.HomePostAdapter | |
import com.rohitjakhar.blogx.databinding.FragmentHomeBinding | |
import com.rohitjakhar.blogx.repository.remote.RemoteDataSource | |
import kotlinx.coroutines.* | |
class HomeFragment : Fragment() { | |
private var _binding: FragmentHomeBinding? = null | |
private val binding get() = _binding!! | |
private lateinit var homeViewModel: HomeViewModel | |
private var pageNo = 1 | |
private val mAdapter by lazy { HomePostAdapter() } | |
private val linearLayoutManager by lazy { LinearLayoutManager(requireContext()) } | |
private var isLoading = true | |
private var userScroll = true | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View { | |
homeViewModel = | |
ViewModelProvider(this).get(HomeViewModel::class.java) | |
_binding = FragmentHomeBinding.inflate(inflater, container, false) | |
binding.rvPost.apply { | |
layoutManager = linearLayoutManager | |
adapter = mAdapter | |
setHasFixedSize(true) | |
addOnScrollListener(object : RecyclerView.OnScrollListener() { | |
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { | |
super.onScrollStateChanged(recyclerView, newState) | |
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { | |
userScroll = true | |
} | |
} | |
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | |
super.onScrolled(recyclerView, dx, dy) | |
val totalItem = linearLayoutManager.itemCount | |
val visibleItemCount = linearLayoutManager.childCount | |
val pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition() | |
val lastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition() | |
Log.d("testRecyc", "Loding: $isLoading") | |
if (userScroll && (visibleItemCount + pastVisibleItems) == totalItem && dy > 0) { | |
userScroll = false | |
updateRecyclerView() | |
} | |
} | |
}) | |
} | |
loadData(pageNo) | |
return binding.root | |
} | |
private fun updateRecyclerView() { | |
binding.progressBar.visibility = View.VISIBLE | |
lifecycleScope.launch { | |
delay(2000) | |
isLoading = false | |
pageNo++ | |
binding.progressBar.visibility = View.VISIBLE | |
loadData(pageNo) | |
userScroll = false | |
} | |
Log.d("testRecyc", "page: $pageNo") | |
} | |
private fun loadData(pageNo: Int) { | |
val dataCall = RemoteDataSource() | |
val getPost = runBlocking { | |
withContext(Dispatchers.IO) { | |
dataCall.getPost( | |
pageNo | |
) | |
} | |
} | |
if (getPost.isSuccessful) { | |
val data = getPost.body() | |
Log.d("postData", data.toString()) | |
mAdapter.setDate(data!!) | |
mAdapter.notifyDataSetChanged() | |
binding.progressBar.visibility = View.GONE | |
} | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
_binding = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment