Android kotlin 实现搜索和展示列表功能(SearchView+RecyclerView+BRVAH3.0.6+androidx)

文章目录

  • 一、实现效果
  • 二、引入依赖
  • 三、主要有那些功能?
  • 四、实现源码
    • 1、适配器
    • 2、实现视图

一、实现效果

Android kotlin 实现搜索和展示列表功能(SearchView+RecyclerView+BRVAH3.0.6+androidx)_第1张图片 Android kotlin 实现搜索和展示列表功能(SearchView+RecyclerView+BRVAH3.0.6+androidx)_第2张图片 Android kotlin 实现搜索和展示列表功能(SearchView+RecyclerView+BRVAH3.0.6+androidx)_第3张图片 Android kotlin 实现搜索和展示列表功能(SearchView+RecyclerView+BRVAH3.0.6+androidx)_第4张图片

二、引入依赖

appbuild.gradle在添加以下代码
1、implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.6',这个里面带的适配器,直接调用就即可

BaseRecyclerViewAdapterHelper简称BRVAH

Android SDK 是否支持BaseRecyclerViewAdapterHelper:3.0.6
android compileSdkVersion 29
android compileSdkVersion 30
android compileSdkVersion 31
android compileSdkVersion 32
android compileSdkVersion 33

这依赖包还需要得到要添加,在Projectbuild.gradle在添加以下代码,不添加就不行

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }//加上
    }
}

三、主要有那些功能?

  • 展示一个100条内容的recyclerview
  • 通过searchview输入关键词,改变recyclerview显示列表

四、实现源码

1、适配器

RvAdapter.kt

package com.example.myapplication3.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.myapplication3.R
import kotlinx.android.synthetic.main.item.view.*

class RvAdapter(layoutResId: Int = R.layout.item) :
    BaseQuickAdapter<String, BaseViewHolder>(layoutResId) {
    override fun convert(holder: BaseViewHolder, item: String) {
        holder.itemView.run {
            tv_content.text = item
        }
    }
}

item布局item.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_menu_layout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/item_bg"
    android:gravity="center">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="菜单" />
LinearLayout>

布局样式item_bg.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:width="1.0px" android:color="@color/line" />

            <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
        shape>
    item>

    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />

            <stroke android:width="1.0px" android:color="@color/line" />
        shape>
    item>

    <item>
        <shape android:shape="rectangle">
            <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />

            <stroke android:width="1.0px" android:color="@color/line" />
        shape>
    item>

selector>

2、实现视图

MainActivity.kt

package com.example.myapplication3

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication3.adapter.RvAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val list = mutableListOf<String>()

    private val mAdapter by lazy {
        RvAdapter()
    }

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

        for (i in 1..100) {
            list.add("这里是第 $i 行")
        }

        //添加RecyclerView的样式和数据更新方法
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = mAdapter
        mAdapter.setList(list)

        //规定SearchView的侦听事件
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(keyWord: String): Boolean {
                //当提交了输入时的操作
                return false
            }

            override fun onQueryTextChange(keyWord: String): Boolean {
                // 当修改了输入时的操作,根据关键字过滤列表,让Adapter填入新列表
                // 如果只是更新部分数据,推荐使用notifyItemRangeChanged()或者notifyItemChanged()
                // notifyItemChanged(int)
                // notifyItemInserted(int)
                // notifyItemRemoved(int)
                // notifyItemRangeChanged(int, int)
                // notifyItemRangeInserted(int, int)
                // notifyItemRangeRemoved(int, int)
                val filterList = filter(keyWord)
                recyclerView.adapter = mAdapter
                mAdapter.setList(filterList)
                return false
            }
        })
    }

    private fun filter(keyWord: String): List<String> {
        // 过滤原本的列表,返回一个新的列表
        val filterList = mutableListOf<String>()

        for (l in list) {
            if (l.contains(keyWord)) filterList.add(l)
        }

        return filterList
    }
}

activity_main.xml


<LinearLayout 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"
    android:layout_marginTop="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

LinearLayout>

你可能感兴趣的:(Android,kotlin开源项目-功能,kotlin,SearchView,RecyclerView,BRVAH3.0.6,androidx)