【问题标题】:How can I reduce spacing between items in RecyclerView?如何减少 RecyclerView 中项目之间的间距?
【发布时间】:2018-11-10 10:29:22
【问题描述】:

我有一个简单的问题,我有一个包含 10 个项目的 RecyclerView,一个激活分页的水平滚动,我的项目出现在中间,如下所示:

我现在想要的是拥有这个。如您所见,屏幕上可以看到下一个项目:

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

<ImageView
    android:id="@+id/square"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:src="@drawable/square_elem"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 </android.support.constraint.ConstraintLayout>

我的主要活动:

class MainActivity : AppCompatActivity() {
{
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    recyclerInit()

    }

fun recyclerInit()
{
     val list : ArrayList<String> = ArrayList()


    for (i in 1..10) {
        list.add("$i")
    }

    recyclerview.layoutManager = LinearLayoutManager(this, 
    LinearLayoutManager.HORIZONTAL, false)


    recyclerview.setHasFixedSize(true)

    recyclerview.adapter = RecyclerAdapter(list)

    val snapHelper = PagerSnapHelper()
    snapHelper.attachToRecyclerView(recyclerview)
    }
}

这是我的适配器:

class RecyclerAdapter(val list: ArrayList<String>) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()
{

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view: View = LayoutInflater.from(parent.context).inflate((R.layout.box_elem), parent, false)
    return ViewHolder(view)
}

override fun getItemId(position: Int): Long {
    return position.toLong()
}

override fun getItemViewType(position: Int): Int {
    return position
}

override fun getItemCount(): Int {
    return list.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.image.setImageResource(R.drawable.original_box)
    holder.itemView.setOnClickListener(View.OnClickListener {
        removeElemAt(position);
    })
}

fun removeElemAt(position: Int) {
    list.removeAt(position)
    notifyItemRemoved(position)
    notifyItemRangeChanged(position, list.size)
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    var image: ImageView = itemView.findViewById(R.id.box_elem)
    }
}

【问题讨论】:

  • 您的图像视图对您的父级有约束,请将其删除或尝试设置约束布局宽度以包装内容
  • 根布局的高度和宽度wrap_content。
  • 如果我删除约束我的项目将不会在屏幕中间。
  • 使用 viewpager 代替 10 个不同的片段。我可以告诉你怎么做
  • 谢谢,但我不想使用 viewpager,我真的很想保持这种状态

标签: android xml layout android-recyclerview adapter


【解决方案1】:

试试这个代码

RecyclerView xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

你的项目 xml:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl"
    android:layout_width="YourOption"
    android:layout_height="YourOption">

            <ImageView
                android:id="@+id/img"
                android:layout_width="YourOption"
                android:layout_height="YourOption"
                android:gravity="YourOption"
                android:scaleType="YourOption"
                />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    使用此代码更改布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="wrap_content"
    android:layout_height="wrap_content">
    
    <ImageView
        android:id="@+id/square"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:src="@drawable/square_elem"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
     </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 我现在屏幕上有两个项目,但第一个不在屏幕中间,我的分页现在无法正常工作
    • 修复图片高度和宽度
    • 我的图片高度和宽度正在包裹我的内容,如你所见,“修复”是什么意思
    • android:layout_width="100dp" android:layout_height="100dp"
    • 不起作用,我真的需要保持内容的大小,但只需将其放在屏幕中间,就像您在第一个屏幕截图中看到的那样,但下一个项目旁边可见他