1.添加builder ,引用recycleview
implementation 'com.android.support:recyclerview-v7:26+'
2.效果图
3.主界面功能
package com.example.admin.zkotlin
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import kotlinx.android.synthetic.main.activity_photo.*
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_common.view.*
class MainActivity : AppCompatActivity() {
var mDataList: ArrayList<Pair<Int, String>> = ArrayList();
protected var mContext: Context? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_photo)
mContext = this
initData();
initView()
}
private fun initData() {
var index = 0
mDataList.add(Pair(index++,"1约束布局 constraint layout demo"))
mDataList.add(Pair(index++, "2面向对象六大原则(solid五大原则) >> "))
mDataList.add(Pair(index++, "3-23种设计模式简介 >> "))
mDataList.add(Pair(index++, "4Rxjava >> "))
//common_recycleview.adapter!!.notifyDataSetChanged()
}
private fun initView() {
common_swipe.setOnRefreshListener {
mDataList.add(0, Pair<Int, String>(mDataList.size + 1, "content00")) ////Pair的类型可以不固定
common_recycleview.adapter!!.notifyItemInserted(0)
// common_swipe.setRefreshing(false)
common_swipe.isRefreshing = false
}
common_recycleview.layoutManager = LinearLayoutManager(mContext)
common_recycleview.adapter = MyAdapter()
common_recycleview.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL))
}
inner class MyAdapter : RecyclerView.Adapter<MyViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(layoutInflater.inflate(R.layout.item_common,parent,false))
}
override fun getItemCount(): Int {
return mDataList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
if (holder is MyViewHolder){
var obj = mDataList.get(position) as Pair<Int,String>
holder.apply {
holder.itemView.item_title.setText(obj.first.toString())
holder.itemView.item_content.setText(obj.second)
holder.itemView.setOnClickListener{v:View->
nextAct(obj.first,v)
}
}
}
}
private fun nextAct(first: Int, v: View) {
var intent: Intent? = null
when(first){
0->intent = Intent(mContext,FirstActivity::class.java)
1,2-> intent = Intent(mContext,TwoActivity::class.java)
3-> intent = Intent(mContext,ThreeActivity::class.java)
}
intent?.let { startActivity(it) }
}
}
inner class MyViewHolder(itemView: View?):RecyclerView.ViewHolder(itemView!!)
}
4.主界面布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/common_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/common_recycleview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</FrameLayout>
5.item界面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/padding_10dp">
<ImageView
android:id="@+id/img_main"
android:layout_width="@dimen/dimen_nor_60dp"
android:layout_height="@dimen/dimen_nor_60dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/item_title"
style="@style/txt_nor_detail_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_10dp"
app:layout_constraintLeft_toRightOf="@+id/img_main"
app:layout_constraintRight_toLeftOf="@+id/img_arrow"
tools:text="1111"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/item_content"
style="@style/txt_nor_detail_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/img_main"
app:layout_constraintRight_toLeftOf="@+id/img_arrow"
app:layout_constraintTop_toBottomOf="@+id/item_title"
tools:text="2222" />
<ImageView
android:id="@+id/img_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
end,还少了一点color dimens style 大家自行补上吧,随便写点就行,那个并不重要
来源:CSDN
作者:cf8833
链接:https://blog.csdn.net/cf8833/article/details/103463385