如何在Android中创建一个网格回收器视图,将不同的项目大小调整为不同的大小?

wljmcqd8  于 2023-02-06  发布在  Android
关注(0)|答案(1)|浏览(151)

我需要创建这种网格回收器视图:

其中第一个项目比其他项目大。网格可能只有6个项目,大小可能为:
第一个的宽度为320,高度为220。
其他的宽度为100,高度为150。
我的回收商查看项目XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/home_items_space">

<ImageView
    android:id="@+id/lookImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:scaleType="centerCrop"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的回收商查看XML:

<?xml version="1.0" encoding="utf-8"?>
<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="@dimen/looks_grid_height"
android:paddingHorizontal="12dp">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/looksGrid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
    app:layout_constraintTop_toTopOf="parent"
    app:spanCount="2"
    tools:itemCount="6" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的适配器代码:

class LooksAdapter :
ListAdapter<HomeLook, LooksAdapter.LooksViewHolder>(DiffCallback()) {

companion object {
    private val FIRST_ITEM_INDEX = 0
}

var listener: ((HomeLook) -> Unit)? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LooksViewHolder {
    return LooksViewHolder(
        ItemHomeLooksBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    )
}

override fun onBindViewHolder(holder: LooksViewHolder, position: Int) {
    holder.bind(getItem(position), position)
}

class LooksViewHolder(private val binding: ItemHomeLooksBinding) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(look: HomeLook, index: Int) {

        if (index == FIRST_ITEM_INDEX) {
            binding.root.updateLayoutParams {
                width = binding.root.width / 2
                height = 310
            }
        } else {
            binding.root.updateLayoutParams {
                width = 100
                height = 150
            }
        }

        binding.lookImage.load(look.lookImage) {
            fallback(R.drawable.ic_camera_placeholder)
            error(R.drawable.ic_camera_placeholder)
        }
    }
}

我的ViewHolder代码:

class LooksHomeViewHolder(
    val binding: LooksGridViewBinding
) : RecyclerView.ViewHolder(binding.root) {

    companion object {
        fun from(parent: ViewGroup): LooksHomeViewHolder {
            return LooksHomeViewHolder(
            LooksGridViewBinding.inflate(LayoutInflater.from(parent.context), parent, false),
        )
    }
}

private val adapter = LooksAdapter().apply {
    stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
}

init {
    binding.looksGrid.adapter = adapter
    binding.looksGrid.itemAnimator = DefaultItemAnimator().apply {
        supportsChangeAnimations = false
    }
}

fun bind(
    lookBookEntry: HomeEntity.LookBookEntry,
    listener: (HomeLook) -> Unit,
    addPhotoListener: () -> Unit
) {
    adapter.submitList(lookBookEntry.looks)
    adapter.listener = listener
    binding.addLookButton.setOnClickListener { addPhotoListener.invoke() }
    binding.noLooksPlaceholder.isVisible = lookBookEntry.looks.isEmpty()
    }
}

我试着使用StaggerManager并更改imageView和根大小。但它没有帮助。

相关问题