Guest User

Untitled

a guest
Jun 12th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. **************** MainActivity (onCreate) ***************
  2. storiesContainerAdapter = StoriesContainerAdapter(applicationContext)
  3. postsContainerAdapter = PostsContainerAdapter(applicationContext)
  4. recyclerView.apply {
  5. layoutManager = LinearLayoutManager(applicationContext)
  6. adapter = MergeAdapter(storiesContainerAdapter, postsContainerAdapter)
  7. }
  8.  
  9.  
  10.  
  11. *************** PostsContainerAdapter *****************
  12. class PostsContainerAdapter(private var context: Context): RecyclerView.Adapter<PostsContainerAdapter.PostsContainerHolder>() {
  13.  
  14. lateinit var recyclerView: RecyclerView
  15. private var config: PagedList.Config
  16. private var pagedList: PagedList<Post>
  17. var postAdapter: PostAdapter = PostAdapter(context)
  18.  
  19. init {
  20. val dataSource = MyPositionalDataSource(MainRepository())
  21.  
  22. config = PagedList.Config.Builder()
  23. .setEnablePlaceholders(false)
  24. .setPageSize(2)
  25. .build()
  26.  
  27. pagedList = PagedList.Builder(dataSource, config)
  28. .setFetchExecutor(Executors.newSingleThreadExecutor())
  29. .setNotifyExecutor(MainActivity.MainThreadExecutor())
  30. .build()
  31.  
  32. postAdapter.submitList(pagedList)
  33. }
  34.  
  35. override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
  36. this.recyclerView = recyclerView
  37. }
  38.  
  39. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostsContainerHolder {
  40. val view = LayoutInflater.from(parent.context).inflate(R.layout.posts_container, parent, false)
  41. return PostsContainerHolder(view)
  42. }
  43.  
  44. override fun getItemCount(): Int {
  45. return 1
  46. }
  47.  
  48. override fun onBindViewHolder(holder: PostsContainerHolder, position: Int) {
  49. holder.bind()
  50. }
  51.  
  52. inner class PostsContainerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  53. private val recyclerViewPosts: RecyclerView = itemView.recyclerViewPosts
  54. fun bind(){
  55. recyclerViewPosts.layoutManager = LinearLayoutManager(context)
  56. recyclerViewPosts.adapter = postAdapter
  57. }
  58. }
  59.  
  60. }
  61.  
  62.  
  63. *********************** posts_container.xml **************************
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <androidx.recyclerview.widget.RecyclerView
  66. android:id="@+id/recyclerViewPosts"
  67. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  68. android:layout_height="wrap_content">
  69.  
  70. </androidx.recyclerview.widget.RecyclerView>
Add Comment
Please, Sign In to add comment