如何解决Android RecyclerView 宽度比屏幕宽
我正在使用 CardViews 设置 RecyclerView。然而,尽管有所有限制,CardViews 还是延伸到了屏幕的右侧。
这是我的代码:
///////////////////////////////////////////// ///////// 适配器:
class ExpertsRecyclerAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var expertsData: List<Map<String,Any>> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val v = inflater.inflate(R.layout.layout_experts_item,parent,false)
return ExpertsViewHolder(v)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder,position: Int) {
when (holder) {
is ExpertsViewHolder -> {
// holder.setup(expertsData[position])
}
}
}
override fun getItemCount(): Int {
return 10
}
fun submitList (data: List<Map<String,Any>> ) {
expertsData = data
}
inner class ExpertsViewHolder constructor(expertsView: View): RecyclerView.ViewHolder(expertsView) {
fun setup(data: Map<String,Any>) {
}
}
}
///////////////////////////////////////////// //////////////////////// 包含 RecyclerView 的片段
class ExpertsFragment : Fragment() {
private lateinit var expertsAdapter: ExpertsRecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_experts,container,false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initRecyclerView()
}
private fun initRecyclerView () {
experts_recycler_view.hasFixedSize()
experts_recycler_view.layoutManager = linearlayoutmanager(context)
experts_recycler_view.adapter = ExpertsRecyclerAdapter()
experts_recycler_view.itemAnimator = DefaultItemAnimator()
}
}
///////////////////////////////////////////// /////// RecyclerView 项目的 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="wrap_content"
android:background="@color/secondaryGray"
android:orientation="vertical"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:id="@+id/corner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
///////////////////////////////////////////// ////// RecyclerView 片段的 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"
android:background="@color/secondaryGray"
android:layout_alignParentRight="true"
tools:context=".ExpertsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/experts_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方法
试试这个简单的删除 ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_xxx"/>
</LinearLayout>
我不确定这一点,但您也可以尝试使用以下代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="@color/white"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:id="@+id/corner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
/>
</androidx.cardview.widget.CardView>
,
我想通了。我错误地更改了底部导航栏上方片段的宽度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。