我有一个简单的问题,我有一个RecyclerView与10个项目,一个水平滚动与分页激活和我的项目出现在中间这样:
我现在想要的是这个。正如你所看到的,下一个项目在屏幕上可见:
下面是我的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条答案
按热度按时间ef1yzkbh1#
试试这个代码
回收商视图xml:
您的项目xml:
xuo3flqw2#
用以下代码更改布局