recycler view是安卓5.0中的重大更新。在主类中重写Myviewholder和Myadpter,然后在Recycleview中绑定adpter,实现初步功能。 首先在gradle中添加依赖库:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
在主活动中添加引入:
import androidx.recyclerview.widget.RecyclerView;
在activity_main.xml中添加布局控件:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
recycler view是基于相同事物的“数组”模型,不妨先建立news类文件:
package com.example.myapplication10;
public class News {
public String title;
public String content;
}
建立“数组”,初始化数据,实例化Recyclerview变量:
RecyclerView mRecycleView=findViewById(R.id.recyclerview);
for(int i=0;i<20;i++)
{
News news=new News();
news.title="林允儿和你结婚第"+i+"天";
news.content="睡觉"+i+"次";
mNewsList.add(news);
}
建立Recycler View中的“数组”中的每个元素的布局问题:
<?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:id="@+id/rootview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="29dp"
android:layout_marginTop="33dp"
android:text="标题"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="18dp"
android:text="内容"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
MyviewHolder重写: 对布局文件中的控件进行实例化,实际上是控制“元素”视图的逻辑代码:
class MyviewHolder extends RecyclerView.ViewHolder
{
TextView textitle;
TextView textcontent;
ConstraintLayout mrootview;
public MyviewHolder(@NonNull View itemView) {
super(itemView);
textitle=itemView.findViewById(R.id.textView);
textcontent=itemView.findViewById(R.id.textView2);
mrootview=itemView.findViewById(R.id.rootview);
}
}
其中rootview是“元素”布局文件的id:
android:id="@+id/rootview"
重写adpter,第一个函数是实例化view元素布局,生成元素视图控件,第二个函数是绑定元素视图控件到特定的位置,第三个是返回大小:
class Myadapter extends RecyclerView.Adapter<MyviewHolder>
{
@NonNull
@Override
public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=View.inflate(MainActivity.this,R.layout.item_list,null);
MyviewHolder Myviewholder=new MyviewHolder(view);
return Myviewholder;
}
@Override
public void onBindViewHolder(@NonNull MyviewHolder holder, final int position) {
News news=mNewsList.get( position);
holder.textitle.setText(news.title);
holder.textcontent.setText(news.content);
holder.mrootview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你们有"+position+"个孩子",Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
}
设置管理器,设置风格,设置分割线:
Myadapter myadapter=new Myadapter();
mRecycleView.setAdapter(myadapter);
LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
mRecycleView.setLayoutManager(layoutManager);
DividerItemDecoration divd=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
mRecycleView.addItemDecoration(divd);
则完成如下app:
Main activity总代码:
package com.example.myapplication10;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.List;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
List<News> mNewsList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView mRecycleView=findViewById(R.id.recyclerview);
for(int i=0;i<20;i++)
{
News news=new News();
news.title="林允儿和你结婚第"+i+"天";
news.content="睡觉"+i+"次";
mNewsList.add(news);
}
Myadapter myadapter=new Myadapter();
mRecycleView.setAdapter(myadapter);
LinearLayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
mRecycleView.setLayoutManager(layoutManager);
DividerItemDecoration divd=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
mRecycleView.addItemDecoration(divd);
}
class MyviewHolder extends RecyclerView.ViewHolder
{
TextView textitle;
TextView textcontent;
ConstraintLayout mrootview;
public MyviewHolder(@NonNull View itemView) {
super(itemView);
textitle=itemView.findViewById(R.id.textView);
textcontent=itemView.findViewById(R.id.textView2);
mrootview=itemView.findViewById(R.id.rootview);
}
}
class Myadapter extends RecyclerView.Adapter<MyviewHolder>
{
@NonNull
@Override
public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=View.inflate(MainActivity.this,R.layout.item_list,null);
MyviewHolder Myviewholder=new MyviewHolder(view);
return Myviewholder;
}
@Override
public void onBindViewHolder(@NonNull MyviewHolder holder, final int position) {
News news=mNewsList.get( position);
holder.textitle.setText(news.title);
holder.textcontent.setText(news.content);
holder.mrootview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你们有"+position+"个孩子",Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
}
|