RecyclerView 小记录

示例:向下滑动,逐步显示内容


基于kotlin as4.1


一、首先布局中加入 主角 RecyclerView

   <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />

二、编写一个 POJO


data class New(var title: String, var thing: String)

布局

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/titletv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="标题" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:text="内容" />
    </LinearLayout>
</com.google.android.material.card.MaterialCardView>

三、编写一个 RecyclerView Adapter

适配器:


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.bottomtabtest.R

class NewAda(val news: List<New>) : RecyclerView.Adapter<NewAda.RecHolder>() {
//内部类,
    inner class RecHolder(
        itemView: View,
        var titletv: TextView = itemView.findViewById(R.id.titletv),
        var thingtv: TextView = itemView.findViewById(R.id.textView2)
    ) :
        RecyclerView.ViewHolder(itemView) {
    }
//可以不写的点击触发器
    interface OnItemClickListener {
        fun OnItemClick(view: View, position: Int)
    }

    private lateinit var onClick: OnItemClickListener

    fun setOnItemClick(onItemClickListener: OnItemClickListener) {
        this.onClick = onItemClickListener
    }
//
//
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecHolder =
        RecHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false))


    override fun onBindViewHolder(holder: RecHolder, position: Int) {
        holder.titletv.text = news[position].title
        holder.thingtv.text = news[position].thing
        holder.itemView.setOnClickListener { onClick.OnItemClick(holder.itemView, position) }
    }

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


四、编写主界面代码


class MainActivity : AppCompatActivity() {
//列表,用于容纳rv中的pojo
    var newList = ArrayList<New>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        //
        val mrc: RecyclerView = findViewById(R.id.rv1)
        //填充内容
        for (i in 0..10) {
            var s: String = "内容$i"
            val b = StringBuilder()
            repeat(i) {
                b.append(s)
            }
            s = b.toString()
            val new: New = New("标题$i", s)
            newList.add(new)

        }
        
        val mada: NewAda = NewAda(newList)
        val layoutManager = LinearLayoutManager(this);
        //不同的滑动方向
//        layoutManager.orientation = RecyclerView.HORIZONTAL 
        mrc.layoutManager = layoutManager
        
        mada.setOnItemClick(object : NewAda.OnItemClickListener {
            override fun OnItemClick(view: View, position: Int) {
                Toast.makeText(this@MainActivity, "saad", Toast.LENGTH_LONG).show()
            }
        })

        mrc.adapter = mada
        //设置向上刷新和向下加载 
        val refreshLayout: RefreshLayout = findViewById(R.id.sr);
        refreshLayout.setOnRefreshListener {
            it.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            newList.clear();
            for (i in 0..10) {
                val news: New = New("标题 新内容$i", "内容$i");
                newList.add(news);
            }
            mada.notifyDataSetChanged();
        }
        refreshLayout.setOnLoadMoreListener {
            it.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            for (i in 0..10) {
                val news: New = New("标题 新内容$i", "内容$i");
                newList.add(news);
            }
            mada.notifyDataSetChanged();
        };

设置向上刷新和向下加载 需要把rv放到该view中

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        //需要指定为这样
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            //该属性决定滑动时的跟随行为,是立即划出还是不消失还是
            app:layout_scrollFlags="scroll|snap"
            app:logo="@mipmap/ic_launcher"
            app:title="Title" />

    </com.google.android.material.appbar.AppBarLayout>

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/sr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        //该属性放在和 AppBarLayout 并列的view中 使rv 不和toolbar重叠
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            
            />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值