<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_select_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_search_member"
android:hint="搜索联系人"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:paddingLeft="@dimen/space_tiny"
android:textColor="@color/font_black"
android:layout_height="match_parent" />
</LinearLayout>
在添加或删除数据后,比如
data.add()
adapter.notifyItemInsert()
执行下面的变更布局
// 获取layoutParams
val lp = rv_select_member.layoutParams
// 获取当前数据量
val itemCount = mSelectMemberAdapter.data.size
// 数量大于4的时候,固定宽度
if (mSelectMemberAdapter.itemCount > 4) {
lp.width = SizeUtils.dp2px(40F)*4
}else {
lp.width = SizeUtils.dp2px(40F)*itemCount
}
// 设置好layoutParams后,将recyclerview滚动到最后一个元素位置
rv_select_member.layoutParams = lp
rv_select_member.smoothScrollToPosition(mSelectMemberAdapter.data.lastIndex)