RecyclerView常规写法-竖向,横向
代码
竖向
- gradle(moude):
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha04'//RecyclerView布局
- Menifest:
<activity android:name=".recyclerView.RecyclerViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
- Layout:
fruit_recyclerview_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_name"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"/>
</LinearLayout>
activity_recyclerview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
- Kotlin:
Fruit:
class Fruit(val name:String,val imageId:Int)
RecyclerViewAdapter:
package com.mozhimen.myapplication.recyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mozhimen.myapplication.R
import com.mozhimen.myapplication.common.Fruit
class RecyclerViewAdapter(private val fruitList: List<Fruit>) :
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val fruitImage: ImageView = view.findViewById(R.id.fruit_recyclerview_image)
val fruitName: TextView = view.findViewById(R.id.fruit_recyclerview_name)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.fruit_recyclerview_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val fruit = fruitList[position]
holder.fruitImage.setImageResource(fruit.imageId)
holder.fruitName.text = fruit.name
holder.itemView.setOnClickListener {
val position=holder.bindingAdapterPosition
Log.d("Tag",position.toString())
}
holder.fruitImage.setOnClickListener {
val position=holder.bindingAdapterPosition
Log.d("Tag",position.toString())
}
}
override fun getItemCount() = fruitList.size
}
RecyclerViewActivity:
class RecyclerViewActivity : AppCompatActivity() {
private lateinit var recyclerviewBinding: ActivityRecyclerviewBinding
private val fruitList = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
recyclerviewBinding = ActivityRecyclerviewBinding.inflate(layoutInflater)
setContentView(recyclerviewBinding.root)
initFruits()
val layoutManager=LinearLayoutManager(this)
recyclerviewBinding.recyclerRecyclerView.layoutManager=layoutManager
val adapter=RecyclerViewAdapter(fruitList)
recyclerviewBinding.recyclerRecyclerView.adapter=adapter
}
private fun initFruits() {
repeat(2) {
fruitList.apply {
add(Fruit("Apple", R.drawable.fruit))
add(Fruit("Pear", R.drawable.fruit))
add(Fruit("Banana", R.drawable.fruit))
add(Fruit("Watermelon", R.drawable.fruit))
add(Fruit("Orange", R.drawable.fruit))
add(Fruit("Grape", R.drawable.fruit))
add(Fruit("Pineapple", R.drawable.fruit))
add(Fruit("Strawberry", R.drawable.fruit))
add(Fruit("Cherry", R.drawable.fruit))
add(Fruit("Mango", R.drawable.fruit))
}
}
}
}
竖向
- Menifest:
<activity android:name=".recyclerView.RecyclerViewHorizontalActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
- Layout:
fruit_recyclerview_item_horizontal:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:orientation="vertical">
<ImageView
android:id="@+id/fruit_recyclerview_horizontal_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/fruit_recyclerview_horizontal__name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp" />
</LinearLayout>
- Kotlin:
RecyclerViewHorizontalAdapter:
class RecyclerViewHorizontalAdapter(private val fruitList: List<Fruit>) :
RecyclerView.Adapter<RecyclerViewHorizontalAdapter.ViewHolder>() {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val fruitImage: ImageView = view.findViewById(R.id.fruit_recyclerview_horizontal_image)
val fruitName: TextView = view.findViewById(R.id.fruit_recyclerview_horizontal__name)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.fruit_recyclerview_item_horizontal, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val fruit = fruitList[position]
holder.fruitImage.setImageResource(fruit.imageId)
holder.fruitName.text = fruit.name
}
override fun getItemCount() = fruitList.size
}
RecyclerViewHorizontalActivity:
initFruits()
val layoutManager= LinearLayoutManager(this)
layoutManager.orientation=LinearLayoutManager.HORIZONTAL
recyclerviewBinding.recyclerRecyclerView.layoutManager=layoutManager
val adapter=RecyclerViewHorizontalAdapter(fruitList)
recyclerviewBinding.recyclerRecyclerView.adapter=adapter
效果:
横向
竖向