Giới thiệu
Ví dụ này minh họa cách đặt ListView bên trong ScrollView mà không bị thu gọn (collapsed) bằng cách tính toán chiều cao tổng của ListView và thiết lập lại LayoutParams.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin.
Bước 2: Giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
Bước 3: MainActivity.kt
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private val listViewArray = arrayOf(
"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE",
"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val listView = findViewById<ListView>(R.id.listView)
listView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, listViewArray)
ListHelper.setListViewHeightBasedOnItems(listView)
}
}
Bước 4: Helper class (ListHelper.kt)
import android.util.Log
import android.widget.ListAdapter
import android.widget.ListView
object ListHelper {
fun setListViewHeightBasedOnItems(listView: ListView) {
val adapter = listView.adapter ?: return
var totalHeight = 0
val desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.width, View.MeasureSpec.AT_MOST)
for (i in 0 until adapter.count) {
val listItem = adapter.getView(i, null, listView)
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED)
totalHeight += listItem.measuredHeight
}
val params = listView.layoutParams
params.height = totalHeight + (listView.dividerHeight * (adapter.count - 1))
listView.layoutParams = params
listView.requestLayout()
Log.i("ListHelper", "Total height: $totalHeight")
}
}
Bước 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. ListView sẽ hiển thị đầy đủ các item bên trong ScrollView.
⚠️ Cảnh báo: Việc đặt ListView/RecyclerView bên trong ScrollView không được khuyến khích vì gây xung đột cuộn và ảnh hưởng hiệu năng. Nên dùng RecyclerView với NestedScrollView hoặc gộp header/footer vào RecyclerView bằng ConcatAdapter / multiple view types.