如何减少RecyclerView中项目之间的间距?

时间:2018-05-31 11:58:29

标签: android xml layout android-recyclerview adapter

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

enter image description here

我现在想要什么呢?如您所见,屏幕上显示下一个项目:

enter image description here

这是我的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)
    }
}

2 个答案:

答案 0 :(得分:0)

使用此代码更改您的布局

<?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>

答案 1 :(得分:0)

试试此代码

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>

YourItem 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>