【发布时间】:2020-11-16 23:48:33
【问题描述】:
只是想创建一个简单的网格 2 行视图,如屏幕,但我所有嵌套视图的高度都在拉伸。有点像它伸展到屏幕的高度。我不是 100% 为什么会发生这种情况。提前致谢!
这是我的 MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadAdapter(listOf("URL_1", "URL_2"))
}
private fun loadAdapter(images: List<String>){
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView?.layoutManager = GridLayoutManager(this, 2)
recyclerView?.adapter = GridViewAdapter(this, images)
}
}
我的适配器:
class GridViewAdapter(
private val context: Context,
private val dataSet: List<String>
) :
RecyclerView.Adapter<GridViewAdapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val imageView: ImageView = view.findViewById(R.id.imageView)
init {
}
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.view_image, viewGroup, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
Glide.with(context).load(dataSet[position]).into(viewHolder.imageView)
}
override fun getItemCount() = dataSet.size
}
recyclerview 视图单元格:
<?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"
app:cardBackgroundColor="@android:color/transparent"
android:layout_margin="8dp"
app:layout_constraintDimensionRatio="H,1:1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.cardview.widget.CardView>
MainActivity 布局:
<?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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
标签: gridview android-recyclerview layout adapter