我试图在这个线程搜索答案,如: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>
并且在该活动的回收视图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
方法,要更改项目的容器布局PARAMS宽度是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)
}
}