RecycleView+卡片+下拉刷新
程序员文章站
2022-03-20 08:54:14
1.添加依赖 implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.recyclerview:recyclerview:1.0.0' implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"2.主界面package com.example.myapplication3import androi...
1.添加依赖
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
2.主界面
package com.example.myapplication3
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var swipe_view: SwipeRefreshLayout
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main);
val myDataset = mutableListOf<String>()
for (i in 0 until 20) {
myDataset.add(i.toString())
}
viewManager = LinearLayoutManager(this)
viewAdapter = MyAdapter(myDataset)
recyclerView = findViewById<RecyclerView>(R.id.recycle_view).apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
}
swipe_view = findViewById(R.id.swipe_view)
swipe_view.setColorSchemeColors(resources.getColor(R.color.white))
swipe_view.setProgressBackgroundColorSchemeResource(R.color.gray)
swipe_view.setOnRefreshListener {
//your code Handler
Handler().postDelayed({ swipe_view.isRefreshing = false }, 2000)
}
}
}
3.适配器
package com.example.myapplication3
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyAdapter(private val myDataset: MutableList<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val textView: TextView
init {
v.setOnClickListener { Log.e("TAG", "$adapterPosition") }
textView = v.findViewById(R.id.textViews)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.layout_item, parent, false);
return MyViewHolder(v)
}
override fun getItemCount(): Int {
return myDataset.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textView.text = myDataset[position];
}
companion object {
private val TAG = "CustomAdapter"
}
}
4.主界面布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
5.item布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/white"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardMaxElevation="10dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true">
<TextView
android:id="@+id/textViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</androidx.cardview.widget.CardView>
6.color资源
<color name="white">#fff</color>
<color name="gray">#999</color>
运行效果:
本文地址:https://blog.csdn.net/cf8833/article/details/107936172
下一篇: (WDM)波分复用的总结