RecyclerViewの上下にViewを挟んだ場合のRecyclerViewの縦方向のサイズ
RecyclerViewが真ん中にあります。その上下にView(例えばTextView)を配置して、そのViewのvisibilityをView.GONEにした場合、RecyclerViewの縦サイズが自動拡大するかどうかをやってみました。
例えば、こんな画面を作ります。
真ん中がRecyclerView。
RecyclerViewの上にTextViewを2個配置。
RecyclerViewの下にTextViewを2個配置。
レイアウトエディタで見ると、こうなっています。
この時、RectclerViewはlayout_height="0dp"で作るところがミソです。
(前略)
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
(後略)
上側のTextView1、上側のTextView2がgroup1に入っています。
下側のTextView1、下側のTextView2がgroup2に入っています。
上側のTextView1の水平バリア(Horizontal Barrier)が設定されています。
下側のTextView1の水平バリア(Horizontal Barrier)が設定されています。
「上GRP」のボタンをクリックすると、group1のvisibilityをView.VISIBLE / View.GONE をトルグします。
「下GRP」のボタンをクリックすると、group2のvisibilityをView.VISIBLE / View.GONE をトルグします。
「上TEXT1」のボタンをクリックすると、上側のTextView1のvisibilityをView.VISIBLE / View.GONE をトルグします。
「下TEXT1」のボタンをクリックすると、下側のTextView1のvisibilityをView.VISIBLE / View.GONE をトルグします。
「上GRP」のボタンをクリック(group1のvisibilityをView.GONE)

「下GRP」のボタンをクリック(group2のvisibilityをView.GONE)

「上TEXT1」のボタンをクリック(上側のTextView1のvisibilityをView.GONE)

「下TEXT1」のボタンをクリック(下側のTextView1のvisibilityをView.GONE)

と、行った具合にRecyclerViewの縦方向が自動的に拡大しているのがわかるかと思います。
ソースコード
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Group
android:id="@+id/group1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="textUp1,textUp2" />
<TextView
android:id="@+id/textUp1"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#BBDEFB"
android:gravity="center"
android:text="上側のTextView1"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textUp2"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#B2EBF2"
android:gravity="center"
android:text="上側のTextView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textUp1" />
<androidx.constraintlayout.widget.Barrier
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="textUp1"
tools:layout_editor_absoluteY="731dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFCDD2"
app:layout_constraintBottom_toTopOf="@+id/textDown1"
app:layout_constraintTop_toBottomOf="@+id/textUp2"
tools:layout_editor_absoluteX="1dp">
</androidx.recyclerview.widget.RecyclerView>
<androidx.constraintlayout.widget.Group
android:id="@+id/group2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="textDown1,textView4"
tools:layout_editor_absoluteY="110dp" />
<TextView
android:id="@+id/textDown1"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#FFF9C4"
android:gravity="center"
android:text="下側のTextView1"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#FFECB3"
android:gravity="center"
android:text="下側のTextView12"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.Barrier
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="textDown1"
tools:layout_editor_absoluteY="731dp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="上Grp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="下Grp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="上Text1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="下Text1" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/list1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#D1C4E9"
android:text="TextView" />
<TextView
android:id="@+id/list2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#D1C4E9"
android:text="TextView" />
<TextView
android:id="@+id/list3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#D1C4E9"
android:text="TextView" />
</LinearLayout>
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val data:List<List<String>> = listOf(
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("ああああ", "いいいい", "うううう"),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("うううう", "ああああ", "いいいい" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
listOf("いいいい", "うううう", "ああああ" ),
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater).apply {
setContentView(this.root)
}
val recyclerView = binding.recyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this)
val myAdapter = MyAdapter(data)
recyclerView.adapter = myAdapter
val itemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
recyclerView.addItemDecoration(itemDecoration) // 区切り線
binding.button.setOnClickListener {
if (binding.group1.visibility == View.GONE) {
binding.group1.visibility = View.VISIBLE
} else {
binding.group1.visibility = View.GONE
}
}
binding.button2.setOnClickListener {
if (binding.group2.visibility == View.GONE) {
binding.group2.visibility = View.VISIBLE
} else {
binding.group2.visibility = View.GONE
}
}
binding.button3.setOnClickListener {
if (binding.textUp1.visibility == View.GONE) {
binding.textUp1.visibility = View.VISIBLE
} else {
binding.textUp1.visibility = View.GONE
}
}
binding.button4.setOnClickListener {
if (binding.textDown1.visibility == View.GONE) {
binding.textDown1.visibility = View.VISIBLE
} else {
binding.textDown1.visibility = View.GONE
}
}
}
}
class MyAdapter(private val data: List<List<String>>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
private var binding: MyTextViewBinding
var isInTheMidele = false
get() = field
set(value) { field = value}
var list1: TextView
var list2: TextView
var list3: TextView
init {
binding = MyTextViewBinding.bind(view)
list1 = binding.list1
list2 = binding.list2
list3 = binding.list3
}
fun bindTo(data: List<String>) {
list1.text = data.get(0)
list2.text = data.get(1)
list3.text = data.get(2)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = MyTextViewBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.list1.text = data[position][0]
holder.list2.text = data[position][1]
holder.list3.text = data[position][2]
if (position == data.size/2) {
holder.isInTheMidele = true
} else {
holder.isInTheMidele = false
}
}
override fun getItemCount(): Int {
return data.size
}
}
最後に
完成版はgitHubに置きました