Skip to content

Instantly share code, notes, and snippets.

@davidliu
Created March 13, 2019 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidliu/afb5bb736d9dc52c37ae1f9b67cbfb7c to your computer and use it in GitHub Desktop.
Save davidliu/afb5bb736d9dc52c37ae1f9b67cbfb7c to your computer and use it in GitHub Desktop.
RecyclerView in a ScrollView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view4"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view6"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view7"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view8"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view9"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
package com.test.scrollviewrecyclerview
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recycler_view.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view.adapter = Adapter();
recycler_view1.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view1.adapter = Adapter();
recycler_view2.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view2.adapter = Adapter();
recycler_view3.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view3.adapter = Adapter();
recycler_view4.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view4.adapter = Adapter();
recycler_view5.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view5.adapter = Adapter();
recycler_view6.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view6.adapter = Adapter();
recycler_view7.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view7.adapter = Adapter();
recycler_view8.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view8.adapter = Adapter();
recycler_view9.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycler_view9.adapter = Adapter();
}
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
class Adapter : RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = View(parent.context)
view.layoutParams = ViewGroup.LayoutParams(300, 300)
view.setBackgroundColor(Random.nextInt())
return ViewHolder(view)
}
override fun getItemCount(): Int = 10
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment