基本使用
//設(shè)置布局管理器
recyclerView.layoutManager =LinearLayoutManager(this);
//設(shè)置adapter
recyclerView.adapter = myAdapter(this);
// 設(shè)置分割線
recyclerView.addItemDecoration(Decoration());
// 設(shè)置 item 增加 移除動(dòng)畫
recyclerView.itemAnimator = itemAnimator();
// 設(shè)置item 固定高度
recyclerView.setHasFixedSize(true);
- Activity
package com.example.lijiangbing.recyclerviewdemo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.lijiangbing.recyclerviewdemo.R.id.id_recyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.cell.view.*
class MainActivity : AppCompatActivity(),RecyclerItemClickListenner {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
id_recyclerView.layoutManager = LinearLayoutManager(this);
// adapter 構(gòu)造函數(shù)
id_recyclerView.adapter = myAdapter(this, initData());
id_recyclerView.setHasFixedSize(true);
}
// item 點(diǎn)擊回調(diào)
override fun onRecyclerViewItemClick(view: View, position: Int) {
Log.e("view----:$view","position----:$position")
}
}
// 簡(jiǎn)單數(shù)據(jù)源
fun initData():ArrayList<String>{
var mDatas: ArrayList<String> = arrayListOf();
var i: Char = 'A';
while (i < 'Z'){
mDatas.add(i.toString());
i ++;
}
return mDatas;
}
- 點(diǎn)擊回調(diào)接口:RecyclerItemClickListenner
package com.example.lijiangbing.recyclerviewdemo
import android.view.View
interface RecyclerItemClickListenner {
fun onRecyclerViewItemClick(view: View, position: Int);
}
- myAdapter
// 定義構(gòu)造方法
class myAdapter(val mOnItemClickLitener: RecyclerItemClickListenner?,val mDatas: ArrayList<String>): RecyclerView.Adapter<myAdapter.myHolderView>(){
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): myHolderView {
return myHolderView(LayoutInflater.from(parent?.context).inflate(R.layout.cell,parent,false))
}
override fun onBindViewHolder(holder: myHolderView?, position: Int) {
holder?.tv?.text = mDatas.get(position);
// item 點(diǎn)擊回調(diào)
mOnItemClickLitener?.let {
holder?.itemView?.setOnClickListener {
mOnItemClickLitener.onRecyclerViewItemClick(holder?.itemView,position);
}
}
}
override fun getItemCount(): Int {
return mDatas.count();
}
class myHolderView(view: View): RecyclerView.ViewHolder(view){
val tv: TextView;
init {
tv = view.cell_textView
}
}
}
- 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="com.example.lijiangbing.recyclerviewdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
- item 布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<View
android:id="@+id/cell_view"
android:background="#3E3"
android:layout_width="match_parent"
android:layout_height="60dp" />
<TextView
android:id="@+id/cell_textView"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#FFFFFF"
android:textAlignment="center"
android:paddingVertical="13dp"
app:layout_constraintTop_toTopOf="@id/cell_view"
app:layout_constraintStart_toStartOf="@id/cell_view"
/>
</android.support.constraint.ConstraintLayout>
簡(jiǎn)單的CreyclerView 就完成了;
記錄一下導(dǎo)入CreyclerView 架包時(shí)犯得錯(cuò):
導(dǎo)入方法:
File -> project Structure ->app -> dependencies
add "com.android.support:com.android.support:recyclerview-v7:26.0.0-beta1"
查看Project 下 External Libraries 發(fā)現(xiàn) "com.android.support:recyclerview-v7:26.0.0-beta1" 已經(jīng)有了,大概率可以直接使用了,然而 .......還是出錯(cuò)了,沒有l(wèi)og 信息,就是運(yùn)行不起來(lái)
仔細(xì)看了看dependencies{} 問題就來(lái)了: appcompat竟然是 25.x.x
升級(jí)appcompat 到v7:26.0.0-beta1即可:
implementation 'com.android.support:appcompat-v7:26.0.0-beta1'