为什么 layout_constraintEnd_toEndOf 在 recyclerView 的 constaintLayout 中不起作用?
why doesn't layout_constraintEnd_toEndOf work in constaintLayout in recyclerView?
我正在尝试使用 RecyclerView
的简单 customApp。但是按钮的layout_constraintEnd_toEndOf
不适用的错误发生了。我预期的结果是下图(正确的结果)。但真正的结果是下图(真实结果)。要使真正的结果成为正确的结果,我必须做什么?
代码:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/constraintRoot"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
CustomAdapter.xml
class CustomViewHolder(view : View) : RecyclerView.ViewHolder(view){
val image : ImageView = view.findViewById(R.id.imageView)
val text : TextView = view.findViewById(R.id.textView)
val button : Button = view.findViewById(R.id.button)
init{
view.setOnClickListener {
Log.d("himaru", "${adapterPosition}")
}
}
}
class Data(val imageInt : Int, val name : String)
class CustomAdapter(val context : Context, val dataList : ArrayList<Data>) : RecyclerView.Adapter<CustomViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layout = LayoutInflater.from(parent.context).inflate(R.layout.list_activity, null)
return CustomViewHolder(layout)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val data = dataList[position]
holder.image.setImageResource(data.imageInt)
holder.text.text = data.name
holder.button.text = data.name + "Button"
holder.button.setOnClickListener{
Toast.makeText(context, holder.button.text, Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount() = dataList.size
}
MainActivity.xml
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val arrayList = arrayListOf<Data>()
arrayList.add(Data(R.drawable.yihyun1, "yihyun1"))
arrayList.add(Data(R.drawable.yihyun2, "yihyun2"))
arrayList.add(Data(R.drawable.yihyun3, "yihyun3"))
arrayList.add(Data(R.drawable.yihyun4, "yihyun4"))
arrayList.add(Data(R.drawable.yihyun5, "yihyun5"))
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
val adapter = CustomAdapter(this, arrayList)
recyclerView.adapter = adapter
}
}
实际结果:
正确结果:
默认情况下,回收器视图中子视图的布局参数为 WRAP_CONTENT、WRAP_CONTENT
创建视图后设置参数:
itemView.layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
或Java:
itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
val layout = LayoutInflater.from(parent.context).inflate(R.layout.list_activity, null)
这里有两个问题:
第一个:你把parent设置为null
,这可能是它使用默认WRAP_CONTENT
的主要原因] 而不是使用 parent 的 RecyclerView
的 MATCH_PARENT
您需要传递 parent
而不是
第二个:
您使用 inflate()
的 two-arg version,它假设 attachToRoot
变量的值为真,以防 R.layout.list_activity
不为空。
但是您需要使用 three-arg version:
将 attachToRoot
设置为 false
应用:
val layout = LayoutInflater.from(parent.context).inflate(R.layout.list_activity, parent, false)
我正在尝试使用 RecyclerView
的简单 customApp。但是按钮的layout_constraintEnd_toEndOf
不适用的错误发生了。我预期的结果是下图(正确的结果)。但真正的结果是下图(真实结果)。要使真正的结果成为正确的结果,我必须做什么?
代码:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/constraintRoot"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.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="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
CustomAdapter.xml
class CustomViewHolder(view : View) : RecyclerView.ViewHolder(view){
val image : ImageView = view.findViewById(R.id.imageView)
val text : TextView = view.findViewById(R.id.textView)
val button : Button = view.findViewById(R.id.button)
init{
view.setOnClickListener {
Log.d("himaru", "${adapterPosition}")
}
}
}
class Data(val imageInt : Int, val name : String)
class CustomAdapter(val context : Context, val dataList : ArrayList<Data>) : RecyclerView.Adapter<CustomViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layout = LayoutInflater.from(parent.context).inflate(R.layout.list_activity, null)
return CustomViewHolder(layout)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val data = dataList[position]
holder.image.setImageResource(data.imageInt)
holder.text.text = data.name
holder.button.text = data.name + "Button"
holder.button.setOnClickListener{
Toast.makeText(context, holder.button.text, Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount() = dataList.size
}
MainActivity.xml
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val arrayList = arrayListOf<Data>()
arrayList.add(Data(R.drawable.yihyun1, "yihyun1"))
arrayList.add(Data(R.drawable.yihyun2, "yihyun2"))
arrayList.add(Data(R.drawable.yihyun3, "yihyun3"))
arrayList.add(Data(R.drawable.yihyun4, "yihyun4"))
arrayList.add(Data(R.drawable.yihyun5, "yihyun5"))
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
val adapter = CustomAdapter(this, arrayList)
recyclerView.adapter = adapter
}
}
实际结果:
正确结果:
默认情况下,回收器视图中子视图的布局参数为 WRAP_CONTENT、WRAP_CONTENT
创建视图后设置参数:
itemView.layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
或Java:
itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
val layout = LayoutInflater.from(parent.context).inflate(R.layout.list_activity, null)
这里有两个问题:
第一个:你把parent设置为null
,这可能是它使用默认WRAP_CONTENT
的主要原因] 而不是使用 parent 的 RecyclerView
的 MATCH_PARENT
您需要传递 parent
而不是
第二个:
您使用 inflate()
的 two-arg version,它假设 attachToRoot
变量的值为真,以防 R.layout.list_activity
不为空。
但是您需要使用 three-arg version:
将attachToRoot
设置为 false
应用:
val layout = LayoutInflater.from(parent.context).inflate(R.layout.list_activity, parent, false)