recyclerview的简单使用(小白笔记版)

recyclerview的简单使用(小白笔记版)

前言

关于recyclerview的教程网上已经有很多了,这篇文章主要是本人学习使用recyclerview的学习笔记
最近学习Kotlin,所以activity和fragment和adapter会使用Kotlin

recyclerview是Android5.0推出的,是support-v7包中的新组件
首选:导入support-v7包,并且,要是5.0以上的,在build.gradle的dependencies中加入依赖

dependencies {
    implementation 'com.android.support:appcompat-v7:27.0.0'
    implementation 'com.android.support:recyclerview-v7:27.0.0'
}

布局文件 activity_main.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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <!-- recyclerView 定义 -->
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
        <!-- recyclerView 定义 -->
        
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

定义item的布局文件 recycler_item.xml

<?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">
    
    <TextView
        android:id="@+id/item_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

定义Aadapter: MyAdapter.kt

package com.lzx.demo

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast

class MyAdapter(private var context: Context,private var data: List<String>): RecyclerView.Adapter<MyAdapter.ViewHolder>(){

    /**
     * 定义更新数据的方法
     */
    fun update(updateList: List<String>){
        this.data = updateList
        notifyDataSetChanged()
    }

    /**
     * 这个方法用来初始化recycler的holder
     */
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val view = View.inflate(context, R.layout.recycler_item, null)
        return ViewHolder(view)
    }

    /**
     * 这个方法 告诉 recyclerView 的 item 的数量
     */
    override fun getItemCount(): Int = data!!.size

    /**
     * 对视图做数据绑定
     */
    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        val s = data!![position]
        holder!!.itemText!!.text= s
    }

    /**
     * 自定义holder
     */
    inner class ViewHolder(var view: View) : RecyclerView.ViewHolder(view) {
        var itemText:TextView?= null
        init {
            itemText = view.findViewById(R.id.item_text)
            itemText!!.setOnClickListener(object: View.OnClickListener{
                override fun onClick(p0: View?) {
                    Toast.makeText(context,"当前点击了:"+data[layoutPosition],Toast.LENGTH_LONG).show()
                }
            })
        }
    }
}

使用 RecyclerView 在 MainActivity.kt 使用

package com.lzx.demo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private var mAdapter: MyAdapter? = null
    private var mData: MutableList<String>? = null

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

        mData = ArrayList<String>()
        mData!!.add("第一条")
        mAdapter = MyAdapter(this, mData as ArrayList<String>)
        recycler_view_list.adapter = mAdapter
        //布局方式
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        recycler_view_list.layoutManager = linearLayoutManager

        mData!!.add("第二条")
        mAdapter!!.update(mData as ArrayList<String>)

    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值