一、前言
很多時候會遇到使用 RecyclerView 時,要求超過一定數(shù)目的 Item 后,固定 RecyclerView 的高度,沒有超過這個數(shù)目就自適應(yīng)高度。這種情況更多會出現(xiàn)在對話框中,數(shù)量過多時不能讓對話框占據(jù)整個屏幕,同時又能控制顯示的 Item 個數(shù),下面針對不同的情況可以使用不同的方法。
二、已知 Item 高度的情況下
在已知 Item 布局的高度的情況時,可以通過設(shè)置最大高度來控制顯示的 Item 數(shù)目,因為 Item 高度已知,所以計算好高度就可以達(dá)到目的,下面是具體的實現(xiàn):<br />看下效果,最大展示 3 個 Item,超過滾動展示:<br />
iShot2020-08-0121.56.41.gif
- 布局界面,比較簡單,就一個 RecyclerView,只要設(shè)置一個最大高度就可以了(注意:最大高度要使用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è)置和數(shù)據(jù)設(shè)置,任意正常可用的 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)設(shè)置 RecyclerView 方法比較有效,在設(shè)置好 Adapter 后,獲取單個 Item 的高度,然后就可以用代碼設(shè)置 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è)置一下展示數(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è)置數(shù)據(jù),最重要的是設(shè)置 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)設(shè)置recyclerview高度
rv_recycler_wrap_list?.post {
//假如固定4個,超過4個才需要設(shè)置
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)設(shè)置 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,在里面設(shè)置 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)
//獲取屏幕的寬度,進(jìn)行分配,4.5代表可以放4.5個item,高度任意,這里是match_parent
//如果設(shè)置了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寬度保持四個半