One, foreword

Many times will encounter the use of RecyclerView, the requirements of more than a certain number of items, fixed RecyclerView height, not more than this number on the adaptive height. This is more likely to occur in the dialog box, too many can not let the dialog box take up the entire screen, while the number of items displayed can be controlled, the following for different cases can use different methods.

2. Given the height of Item

When the height of Item layout is known, the maximum height can be set to control the number of items displayed. Since the height of Item is known, the purpose can be achieved by calculating the height well. The following is the specific implementation:

Look at the effect, display a maximum of 3 items, more than scrolling display:

    1. Layout interface, relatively simple, on a RecyclerView, as long as a maximum height can be set (note: maximum height to use 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 high fixed known - > < 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>
Copy the code
    1. Activity interface, simple Settings and data Settings, any normal available adapter can be
class RecyclerItemFixedActivity: AppCompatActivity(a){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_item_fixed)

        val list = arrayListOf<String>("Test Data 1"."Test Data 2"."Test Data 3"."Test data 4") 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)
    }
}
Copy the code
    1. Very general Item layout, mainly fixed height
<? 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>
Copy the code

3. The height of Item cannot be determined

For this case, I checked a lot of information, only dynamic setting RecyclerView method is more effective, after the Adapter is set, get the height of a single Item, and then you can use the code to set the height of RecyclerView. If there’s a better way to do this, feel free to mention it in the comments. The renderings are basically the same as above. The implementation is as follows:

  • 1. Layout file
<? 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>
Copy the code
    1. A simple Item file, unlike the Item above, is not fixed in height
<? 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>
Copy the code
  • 3. Simple Adapter, just set the display data
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)
    }
}
Copy the code
    1. Activity set data, the most important is to set the RecyclerView height, code with specific comments
class RecyclerWrapActivity: AppCompatActivity(a){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview_wrap)

        val list = arrayListOf("Test Data 1"."Test Data 2"."Test Data 3"."Test data 4"."Test number five"."Test Data 6") 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)

        / / reference https://www.cnblogs.com/guanxinjing/p/13037156.html
        // Set the recyclerView height dynamicallyrv_recycler_wrap_list? .post {// If 4 are fixed, you need to set more than 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
            }
        }
    }
}
Copy the code

Four, horizontal scrolling

In horizontal scrolling, it is generally required to display a fixed number of items, and if the number exceeds, it will be displayed in scroll. At this time, it is necessary to dynamically set the width of the Item layout to achieve this. The figure below shows 4.5 items beyond scroll:



  • 1. Layout interface, same as above
<? 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>
Copy the code
    1. Activity, change the orientation to scroll horizontally
class RecyclerViewWrapActivity: AppCompatActivity(a){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview_wrap)

        val list = arrayListOf("Test Data 1"."Test Data 2"."Test Data 3"."Test data 4"."Test number five"."Test Data 6") 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)
    }
}
Copy the code
    1. Adapter, where you set the width of the 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)

        // Get the width of the screen and allocate it. 4.5 means you can put 4.5 items at any height. In this case, match_parent
        // If margin is set, subtract margin
        / / val width = (context) resources. The displayMetrics. WidthPixels - Utils. Dip2px (context, margin)) / 4.5 f
        / / reference https://blog.csdn.net/qq_32518491/article/details/81033275?utm_medium=distribute.pc_relevant.none-task-blog-BlogComm endFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineL earnPai2-1.channel_param
        val width = (context.resources.displayMetrics.widthPixels) / 4.5 f
        val params = ViewGroup.LayoutParams(
            width.toInt(),
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        holder.itemView.layoutParams = params

    }
    
}
Copy the code
    1. Item layout, height can be fixed or not fixed, width 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>
Copy the code

V. Address and Reference:

Github Android development fixed RecyclerView height after a specified number of items Android RecyclerView item width maintain four and a half