Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **************** MainActivity (onCreate) ***************
- storiesContainerAdapter = StoriesContainerAdapter(applicationContext)
- postsContainerAdapter = PostsContainerAdapter(applicationContext)
- recyclerView.apply {
- layoutManager = LinearLayoutManager(applicationContext)
- adapter = MergeAdapter(storiesContainerAdapter, postsContainerAdapter)
- }
- *************** PostsContainerAdapter *****************
- class PostsContainerAdapter(private var context: Context): RecyclerView.Adapter<PostsContainerAdapter.PostsContainerHolder>() {
- lateinit var recyclerView: RecyclerView
- private var config: PagedList.Config
- private var pagedList: PagedList<Post>
- var postAdapter: PostAdapter = PostAdapter(context)
- init {
- val dataSource = MyPositionalDataSource(MainRepository())
- config = PagedList.Config.Builder()
- .setEnablePlaceholders(false)
- .setPageSize(2)
- .build()
- pagedList = PagedList.Builder(dataSource, config)
- .setFetchExecutor(Executors.newSingleThreadExecutor())
- .setNotifyExecutor(MainActivity.MainThreadExecutor())
- .build()
- postAdapter.submitList(pagedList)
- }
- override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
- this.recyclerView = recyclerView
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostsContainerHolder {
- val view = LayoutInflater.from(parent.context).inflate(R.layout.posts_container, parent, false)
- return PostsContainerHolder(view)
- }
- override fun getItemCount(): Int {
- return 1
- }
- override fun onBindViewHolder(holder: PostsContainerHolder, position: Int) {
- holder.bind()
- }
- inner class PostsContainerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- private val recyclerViewPosts: RecyclerView = itemView.recyclerViewPosts
- fun bind(){
- recyclerViewPosts.layoutManager = LinearLayoutManager(context)
- recyclerViewPosts.adapter = postAdapter
- }
- }
- }
- *********************** posts_container.xml **************************
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.recyclerview.widget.RecyclerView
- android:id="@+id/recyclerViewPosts"
- xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </androidx.recyclerview.widget.RecyclerView>
Add Comment
Please, Sign In to add comment