一炸庞、前言
很多時候會遇到使用 RecyclerView 時沛婴,要求超過一定數(shù)目的 Item 后,固定 RecyclerView 的高度姑丑,沒有超過這個數(shù)目就自適應高度。這種情況更多會出現(xiàn)在對話框中栅哀,數(shù)量過多時不能讓對話框占據(jù)整個屏幕称龙,同時又能控制顯示的 Item 個數(shù),下面針對不同的情況可以使用不同的方法鲫尊。
二痴柔、已知 Item 高度的情況下
在已知 Item 布局的高度的情況時疫向,可以通過設置最大高度來控制顯示的 Item 數(shù)目,因為 Item 高度已知搔驼,所以計算好高度就可以達到目的谈火,下面是具體的實現(xiàn):<br />看下效果堆巧,最大展示 3 個 Item泼菌,超過滾動展示:<br />
iShot2020-08-0121.56.41.gif
- 布局界面哗伯,比較簡單篷角,就一個 RecyclerView恳蹲,只要設置一個最大高度就可以了(注意:最大高度要使用layout_constraintHeight_max)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<!--item高度固定可知時-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recycler_fixed_one"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_max="240dp"
android:background="@color/yellow_FF9B52"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
- Activity 界面嘉蕾,簡單的設置和數(shù)據(jù)設置,任意正忱苈剩可用的 adapter 就可以
class RecyclerItemFixedActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler_item_fixed)
val list = arrayListOf<String>("測試數(shù)據(jù)一","測試數(shù)據(jù)二","測試數(shù)據(jù)三","測試數(shù)據(jù)四")
val adapter by lazy { RecyclerItemFixedAdapter() }
rv_recycler_fixed_one?.let {
it.layoutManager = LinearLayoutManager(this)
it.adapter = adapter
it.addItemDecoration(DividerItemDecoration(this,LinearLayoutManager.VERTICAL))
}
adapter.setList(list)
}
}
- 非常普通的 Item 布局儿普,主要是高度固定
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/green_07C0C2">
<TextView
android:id="@+id/tv_item_recycler_fixed_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/white"
android:layout_centerInParent="true"/>
</RelativeLayout>
<a name="jQDp4"></a>
三、Item 高度無法確定時
對于這種情況个绍,我查了很多資料浪汪,只有動態(tài)設置 RecyclerView 方法比較有效吟宦,在設置好 Adapter 后,獲取單個 Item 的高度殃姓,然后就可以用代碼設置 RecyclerView 的高度袁波。如果有更好的方法實現(xiàn)的話,歡迎在評論中提出蜗侈。效果圖和上圖基本一樣篷牌。實現(xiàn)如下:
- 1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/red_F7E6ED">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recycler_wrap_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
- 簡單的 Item文件踏幻,和上面的 Item 不同的地方就是高度不固定
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green_07C0C2">
<TextView
android:id="@+id/tv_item_recycler_wrap2_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/white"
android:layout_centerInParent="true"
android:paddingVertical="@dimen/m10"/>
</RelativeLayout>
- 3.簡單的 adapter该面,僅僅設置一下展示數(shù)據(jù)
class RecyclerWrapAdapter(resId: Int = R.layout.item_recycelr_wrap2) :
BaseQuickAdapter<String, BaseViewHolder>(resId) {
override fun convert(holder: BaseViewHolder, item: String) {
holder.setText(R.id.tv_item_recycler_wrap2_title, item)
}
}
- activity 設置數(shù)據(jù)隔缀,最重要的是設置 RecyclerView 的高度,代碼中有具體的注釋
class RecyclerWrapActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recyclerview_wrap)
val list = arrayListOf("測試數(shù)據(jù)一","測試數(shù)據(jù)二","測試數(shù)據(jù)三","測試數(shù)據(jù)四","測試數(shù)據(jù)五","測試數(shù)據(jù)六")
val adapter by lazy { RecyclerWrapAdapter() }
rv_recycler_wrap_list?.let {
it.layoutManager = LinearLayoutManager(this)
it.adapter = adapter
it.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))
}
adapter.setList(list)
//參考https://www.cnblogs.com/guanxinjing/p/13037156.html
//動態(tài)設置recyclerview高度
rv_recycler_wrap_list?.post {
//假如固定4個,超過4個才需要設置
if (list.size <= 4){
return@post
}
val itemView = rv_recycler_wrap_list.getChildAt(0)
itemView?.let {
val height = it.height * 4
val layoutParems = rv_recycler_wrap_list.layoutParams
layoutParems.height = height
rv_recycler_wrap_list.layoutParams = layoutParems
}
}
}
}
<a name="MBual"></a>
四、橫向滾動時
橫向滾動時淮悼,一般是要求展示固定數(shù)目的 Item揽思,超過數(shù)目則滾動展示钉汗,這個時候就要去動態(tài)設置 Item 布局的寬度來實現(xiàn)锡宋。如下圖执俩,展示 4.5個 Item癌刽,超過滾動:<br />
image.png

image.png
- 1.布局界面衡奥,和上面都一樣
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/red_F7E6ED">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recycler_wrap_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
- activity矮固,把方向改成橫向滾動就可以了
class RecyclerViewWrapActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recyclerview_wrap)
val list = arrayListOf("測試數(shù)據(jù)一","測試數(shù)據(jù)二","測試數(shù)據(jù)三","測試數(shù)據(jù)四","測試數(shù)據(jù)五","測試數(shù)據(jù)六")
val adapter by lazy { RecyclerItemWrapAdapter() }
rv_recycler_wrap_list?.let {
it.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
it.adapter = adapter
it.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL))
}
adapter.setList(list)
}
}
- adapter档址,在里面設置 Item 的寬度
class RecyclerItemWrapAdapter(resId: Int = R.layout.item_recycelr_wrap)
: BaseQuickAdapter<String,BaseViewHolder>(resId){
override fun convert(holder: BaseViewHolder, item: String) {
holder.setText(R.id.tv_item_recycler_wrap_title,item)
//獲取屏幕的寬度,進行分配,4.5代表可以放4.5個item,高度任意,這里是match_parent
//如果設置了margin,需要減去margin
// val width = (context.resources.displayMetrics.widthPixels - Utils.dip2px(context,margin)) / 4.5f
//參考https://blog.csdn.net/qq_32518491/article/details/81033275?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
val width = (context.resources.displayMetrics.widthPixels) / 4.5f
val params = ViewGroup.LayoutParams(
width.toInt(),
ViewGroup.LayoutParams.MATCH_PARENT
)
holder.itemView.layoutParams = params
}
}
- Item 布局守伸,高度可以固定尼摹,可以不固定剂娄,寬度 wrap_content
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/green_07C0C2">
<TextView
android:id="@+id/tv_item_recycler_wrap_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/white"
android:layout_centerInParent="true"
android:paddingVertical="@dimen/m10"
/>
</RelativeLayout>
<a name="R77jF"></a>
五阅懦、地址和參考:
Github
Android開發(fā) 在有指定數(shù)量item后固定RecyclerView高度
Android RecyclerView的item寬度保持四個半