Recyclerview的使用

2021-06-23  本文已影响0人  别看后面有人

1、Recyclerview对竖向列表的使用
1> 定义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/recycleview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

2>Recyclerview的适配器:适配器继承Recyclerview.Adapter,并将泛型指定为IconAdapter.ViewHolder,ViewHolder为自定义的一个内部类:

class IconAdapter(val iconList:List<IconData>) : RecyclerView.Adapter<IconAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val iconImg:ImageView =itemView.findViewById(R.id.imgiconitem)
        val icontx:TextView=itemView.findViewById(R.id.tviconitem)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
       val view=LayoutInflater.from(p0.context).inflate(R.layout.icon_item,p0,false)
        return ViewHolder(view)
    }

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

    override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
      val icondata=iconList[p1]
        p0.iconImg.setImageResource(icondata.iconDataImg)
        p0.icontx.setText(icondata.iconDataName)
    
}

onCreateViewHolder()、getItemCount()和onBindViewHolder()
3>调用setAdaper()

class ThirdActivity : BaseActivity() {

    val dataList=ArrayList<IconData>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.third_layout)
        initData()
        val layoutmanager=LinearLayoutManager(this)
        recycleview.layoutManager=layoutmanager

        val adapter=IconAdapter(dataList)
        recycleview.adapter=adapter

    }

    fun initData(){
        repeat(5){
            dataList.add(IconData("分类",R.drawable.ic_catefory_pressed))
            dataList.add(IconData("首页",R.drawable.home_on))
            dataList.add(IconData("购物车",R.drawable.cart_on))
            dataList.add(IconData("我的",R.drawable.my_on))
        }

    }

2、横向列表:
1>item的修改

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 <ImageView
     android:id="@+id/imgiconitem"
     android:layout_width="30dp"
     android:layout_height="30dp"
     android:layout_margin="10dp"
     android:layout_gravity="center"
     android:src="@drawable/my_on"/>
    <TextView
        android:id="@+id/tviconitem"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"  
        android:layout_height="match_parent"
        android:text="我的"/>
</LinearLayout>

2>调用修改的地方:

  val layoutmanager=LinearLayoutManager(this)
        layoutmanager.orientation=LinearLayoutManager.HORIZONTAL
        recycleview.layoutManager=layoutmanager
        val adapter=IconAdapter(dataList)

3、网格布局和瀑布流
网格布局是GridLayoutManager
瀑布流StaggeredGridLayoutManager
适配器还是之前的
数据为了明显添加了长度:

class ThirdActivity : BaseActivity() {

    val dataList=ArrayList<IconData>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.third_layout)
        initData()
        val layoutmanager=StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
//        layoutmanager.orientation=LinearLayoutManager.HORIZONTAL
        recycleview.layoutManager=layoutmanager

        val adapter=IconAdapter(dataList)
        recycleview.adapter=adapter


    }

    fun initData(){
        repeat(5){
            dataList.add(IconData(getRandomLengthString("分类"),R.drawable.ic_catefory_pressed))
            dataList.add(IconData(getRandomLengthString("首页"),R.drawable.home_on))
            dataList.add(IconData(getRandomLengthString("购物车"),R.drawable.cart_on))
            dataList.add(IconData(getRandomLengthString("我的"),R.drawable.my_on))
        }

    }


    fun getRandomLengthString(str:String):String{
        val n=(1..20).random()

        val build=StringBuilder()
        repeat(n){
            build.append(str)
        }
        return build.toString()
    }

瀑布流.png
上一篇下一篇

猜你喜欢

热点阅读