android - 使 recyclerview 项目占用屏幕宽度并将其定位在点击
问题描述
正在使用谷歌地图,我在地图顶部有一个水平回收视图,以显示一些关于地图中某些位置的重要信息,当用户选择某个项目以使其占据整个空间(匹配父项)并且能够这通过更改 with 以匹配 onClickListner 中的父级,但我有两个问题
1 - when select the first item everything is fine but when select the second one the width change but its half way off screen I want to position it correctly
2- when any item selected the rest of items are being pulled up I guess because they are sticking to the top of the parent view , I have tried to set the gravity to bottom but it didn't work
地图.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".maps.HospitalsSearchActivity" />
<include
layout="@layout/hospital_map_overley"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="16dp"
/>
回收站视图
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mapHospitals_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
/>
膨胀到 recycerview 的卡片视图
<androidx.cardview.widget.CardView
android:id="@+id/hospital_logo"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
app:cardCornerRadius="10dp"
android:layout_marginHorizontal="8dp"
android:layout_gravity="center|bottom"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/hospital_logo"
android:layout_marginHorizontal="60dp"
android:layout_marginTop="8dp"
/>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hospital_name"
style="@style/AppText"
android:layout_marginVertical="8dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_star_border" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_star_full" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_star_full" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_star_full" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_star_full" />
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/reserveNowBtn"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/PrimaryButton"
android:text="@string/reserve_now"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
解决方案
1 - when select the first item everything is fine but when select the second one the width change but its half way off screen I want to position it correctly
选项:
- 隐藏 Recycler 中的所有其他视图,例如:
private fun hideAllBut(itemId: String) {
itemList.iterator().apply {
while (hasNext()) next().apply {
if (id != itemId) itemList.indexOf(this).also {
layout.recycler.getChildAt(it).visibility = View.GONE
layout.recycler.adapter?.notifyItemChanged(it)
} } } }
- 将 Recycler 滚动到项目的位置,例如(source):
// Scroll to item 2 with 20 pixels offset
linearLayoutManager.scrollToPositionWithOffset(2, 20)
2- when any item selected the rest of items are being pulled up I guess because they are sticking to the top of the parent view , I have tried to set the gravity to bottom but it didn't work
确保您使用了正确的gravity
和layout_gravity
,例如您想android:gravity="bottom"
在 RecyclerView 或android:layout_gravity="bottom"
CardView 中使用。更多信息在这里。
推荐阅读
- python - 我的 return 语句没有返回值有问题
- c++ - NVAPI获取gpu输出物理索引
- c# - 实体框架 FirstOrDefault 谓词未返回预期结果
- python - DeprecationWarning:不推荐使用 AppURLopener 调用请求的样式。使用更新的 urlopen 函数/方法
- arduino-c++ - 如何解决循环数字
- c# - 在单行 C# 和 ASP.NET MVC 中获取不同行的 db 值
- c# - DistributedCacheExtensions 函数
- r - 看不到使用 Docker 和 {golem} 构建的暴露的 Shiny 应用程序
- groovy - 将 shell 输出传递给 groovy 变量
- android - imageview中的Recryler如何使不可见