最后如何在Horizo​​ntal RecyclerView中显示部分项目以指示滚动?

时间:2019-02-05 09:23:31

标签: android kotlin android-recyclerview recyclerview-layout

我已经尝试在此线程中搜索答案:RecyclerView LinearLayoutManager set item count

但是它不能完全解决我的问题,因为它显示固定的项目。

我想使RecyclerView像下面的图片

enter image description here

如您在图片中看到的,显示了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)


    }



}

1 个答案:

答案 0 :(得分:1)

我最近在我的一个列表中做了此操作。您想找出设备的屏幕宽度:

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)


    }
}