如何在 Horizontal RecyclerView 中最后显示部分项目以指示滚动?
How to show partial item in Horizontal RecyclerView at last to indicate scrolling?
我试过在这个帖子中搜索答案:RecyclerView LinearLayoutManager set item count
但它并没有完全解决我的问题,因为它显示的是固定项目。
我想RecyclerView
像下图一样
如图所示,显示了 3 个项目,但右侧显示的项目不完整,表明此滚动视图可以滚动。我想在 recyclerView
中为所有屏幕尺寸设置部分(3 + 1/4)项。
我使用的项目布局xml是这样的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="150dp"
android:layout_height="250dp"
app:cardCornerRadius="10dp"
android:layout_marginRight="8dp"
app:cardElevation="3dp">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">
<ImageView
android:layout_width="0dp"
android:layout_height="100dp"
app:srcCompat="@drawable/logo_apps"
android:id="@+id/productImageView_Item"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintDimensionRatio="w,1:1"/>
<TextView
android:text="@string/product_name"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/productName_textView_item"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/productImageView_Item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="4dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="4dp"
android:textAlignment="center"
android:minLines="1"
android:maxLines="2"
app:autoSizeMinTextSize="8sp"
app:autoSizeMaxTextSize="14sp"
android:layout_marginBottom="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
activity 中的回收站视图 xml 是这样的:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.HomeFragment" android:background="@color/colorLightGrayBackground">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="1.0"
android:id="@+id/recyclerView_1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
这里是 java 文件中的代码:
val layoutManager = LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false)
recyclerView1.adapter = productAdapter
recyclerView1.layoutManager = layoutManager
这是我的适配器:
class ProductListAdapter(val context: Context, val products: List<Product>) : RecyclerView.Adapter<ProductListAdapter.ViewHolderProductList>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderProductList {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.item_product_list,parent, false)
return ViewHolderProductList(cellForRow)
}
override fun getItemCount(): Int {
return products.size
}
override fun onBindViewHolder(holder: ViewHolderProductList, position: Int) {
val product = products[position]
holder.productNameTextView.text = product.name
Glide
.with(context)
.load(product.getFormattedImagePath())
.into(holder.productImageView)
}
inner class ViewHolderProductList(itemView: View) : RecyclerView.ViewHolder(itemView) {
val productImageView = itemView.findViewById<ImageView>(R.id.productImageView_Item)
val productNameTextView = itemView.findViewById<TextView>(R.id.productName_textView_item)
}
}
我最近在我的一个列表中这样做了。你想知道设备的屏幕宽度:
val displayMetrics = DisplayMetrics()
(context as NavigationActivity).windowManager.getDefaultDisplay().getMetrics(displayMetrics)
width = displayMetrics.widthPixels
然后将设备的宽度除以 3 又 1/3 块覆盖它,所以将屏幕宽度除以大约 3.33:
itemWidth = width / 3.33
现在,在列表适配器中的 onBind
方法中,您想将项目的容器布局参数宽度更改为 itemWidth:
val lp = itemView.container.layoutParams
lp.height = originalHeight
lp.width = itemWidth
itemView.container.layoutParams = lp
您可能需要考虑项目之间的填充,而不是:
itemWidth = width / 3.33
类似于:
itemWidth = (width - totalPadding) / 3.33
编辑
像这样在您的代码中添加它:
class ProductListAdapter(val context: Context, val products: List<Product>) : RecyclerView.Adapter<ProductListAdapter.ViewHolderProductList>() {
// holds this device's screen width,
private var screenWidth = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderProductList {
// get screen width
val displayMetrics = DisplayMetrics()
(context as YourActivity).windowManager.getDefaultDisplay().getMetrics(displayMetrics)
screenWidth = displayMetrics.widthPixels
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.item_product_list,parent, false)
return ViewHolderProductList(cellForRow)
}
override fun getItemCount(): Int {
return products.size
}
override fun onBindViewHolder(holder: ViewHolderProductList, position: Int) {
val product = products[position]
val itemWidth = screenWidth / 3.33
val lp = holder.cardView.layoutParams
lp.height = lp.height
lp.width = itemWidth
itemView.container.layoutParams = lp
holder.productNameTextView.text = product.name
Glide
.with(context)
.load(product.getFormattedImagePath())
.into(holder.productImageView)
}
inner class ViewHolderProductList(itemView: View) : RecyclerView.ViewHolder(itemView) {
val productImageView = itemView.findViewById<ImageView>(R.id.productImageView_Item)
val productNameTextView = itemView.findViewById<TextView>(R.id.productName_textView_item)
}
}
我试过在这个帖子中搜索答案:RecyclerView LinearLayoutManager set item count
但它并没有完全解决我的问题,因为它显示的是固定项目。
我想RecyclerView
像下图一样
如图所示,显示了 3 个项目,但右侧显示的项目不完整,表明此滚动视图可以滚动。我想在 recyclerView
中为所有屏幕尺寸设置部分(3 + 1/4)项。
我使用的项目布局xml是这样的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="150dp"
android:layout_height="250dp"
app:cardCornerRadius="10dp"
android:layout_marginRight="8dp"
app:cardElevation="3dp">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">
<ImageView
android:layout_width="0dp"
android:layout_height="100dp"
app:srcCompat="@drawable/logo_apps"
android:id="@+id/productImageView_Item"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintDimensionRatio="w,1:1"/>
<TextView
android:text="@string/product_name"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/productName_textView_item"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/productImageView_Item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="4dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="4dp"
android:textAlignment="center"
android:minLines="1"
android:maxLines="2"
app:autoSizeMinTextSize="8sp"
app:autoSizeMaxTextSize="14sp"
android:layout_marginBottom="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
activity 中的回收站视图 xml 是这样的:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.HomeFragment" android:background="@color/colorLightGrayBackground">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="1.0"
android:id="@+id/recyclerView_1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
这里是 java 文件中的代码:
val layoutManager = LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false)
recyclerView1.adapter = productAdapter
recyclerView1.layoutManager = layoutManager
这是我的适配器:
class ProductListAdapter(val context: Context, val products: List<Product>) : RecyclerView.Adapter<ProductListAdapter.ViewHolderProductList>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderProductList {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.item_product_list,parent, false)
return ViewHolderProductList(cellForRow)
}
override fun getItemCount(): Int {
return products.size
}
override fun onBindViewHolder(holder: ViewHolderProductList, position: Int) {
val product = products[position]
holder.productNameTextView.text = product.name
Glide
.with(context)
.load(product.getFormattedImagePath())
.into(holder.productImageView)
}
inner class ViewHolderProductList(itemView: View) : RecyclerView.ViewHolder(itemView) {
val productImageView = itemView.findViewById<ImageView>(R.id.productImageView_Item)
val productNameTextView = itemView.findViewById<TextView>(R.id.productName_textView_item)
}
}
我最近在我的一个列表中这样做了。你想知道设备的屏幕宽度:
val displayMetrics = DisplayMetrics()
(context as NavigationActivity).windowManager.getDefaultDisplay().getMetrics(displayMetrics)
width = displayMetrics.widthPixels
然后将设备的宽度除以 3 又 1/3 块覆盖它,所以将屏幕宽度除以大约 3.33:
itemWidth = width / 3.33
现在,在列表适配器中的 onBind
方法中,您想将项目的容器布局参数宽度更改为 itemWidth:
val lp = itemView.container.layoutParams
lp.height = originalHeight
lp.width = itemWidth
itemView.container.layoutParams = lp
您可能需要考虑项目之间的填充,而不是:
itemWidth = width / 3.33
类似于:
itemWidth = (width - totalPadding) / 3.33
编辑
像这样在您的代码中添加它:
class ProductListAdapter(val context: Context, val products: List<Product>) : RecyclerView.Adapter<ProductListAdapter.ViewHolderProductList>() {
// holds this device's screen width,
private var screenWidth = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderProductList {
// get screen width
val displayMetrics = DisplayMetrics()
(context as YourActivity).windowManager.getDefaultDisplay().getMetrics(displayMetrics)
screenWidth = displayMetrics.widthPixels
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.item_product_list,parent, false)
return ViewHolderProductList(cellForRow)
}
override fun getItemCount(): Int {
return products.size
}
override fun onBindViewHolder(holder: ViewHolderProductList, position: Int) {
val product = products[position]
val itemWidth = screenWidth / 3.33
val lp = holder.cardView.layoutParams
lp.height = lp.height
lp.width = itemWidth
itemView.container.layoutParams = lp
holder.productNameTextView.text = product.name
Glide
.with(context)
.load(product.getFormattedImagePath())
.into(holder.productImageView)
}
inner class ViewHolderProductList(itemView: View) : RecyclerView.ViewHolder(itemView) {
val productImageView = itemView.findViewById<ImageView>(R.id.productImageView_Item)
val productNameTextView = itemView.findViewById<TextView>(R.id.productName_textView_item)
}
}