RecyclerView 的使用
作者:互联网
关于Adapter的使用
-
xml视图 //Adapter的使用常常伴随着ListView ;GridView;RecyclerView 等视图
-
xml视图对应 item类 //根据item内容将item封装成一个类
-
item类适配器 //根据item和xml视图做适配
-
MainActivity //视图准备,适配器准备,itemlist数据准备,为视图设置适配器对象
RecyclerView 的使用
-
xml视图 【activity_main.xml】
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fitsSystemWindows="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerView"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout> -
item_view 【rv_item.xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
</LinearLayout> -
item封装 【AppItem.java】 //将item封装成一个类,以便适配
package com.neostra.test;
public class AppItem {
private int icon;
private String app_name;
public AppItem(int icon, String app_name) {
this.icon = icon;
this.app_name = app_name;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getApp_name() {
return app_name;
}
public void setApp_name(String app_name) {
this.app_name = app_name;
}
@Override
public String toString() {
return "AppItem{" +
"icon=" + icon +
", app_name='" + app_name + '\'' +
'}';
}
} -
适配器【RvAdapter.java】 //适配器是重点 ;注意适配器的继承选择和内部类
package com.neostra.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
private ArrayList<AppItem> dataList;
private Context mContext;
public RvAdapter(ArrayList<AppItem> dataList,Context context) {
this.dataList=dataList;
this.mContext=context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//找到xml视图文件
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false);
//返回匹配好的视图
return new ViewHolder(view);
}
//绑定item viewholder 中的视图
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//匹配视图,将datalist中的视图匹配到viewHolder
holder.iv.setImageResource(dataList.get(position).getIcon());//获取图片id
holder.tv.setText(dataList.get(position).getApp_name());
}
//rv_item个数
@Override
public int getItemCount() {
return dataList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
private ImageView iv;
private TextView tv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
iv=itemView.findViewById(R.id.iv);
tv=itemView.findViewById(R.id.tv);
}
}
}
-
MainActivity 【MainActivity.java】
package com.neostra.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ArrayList<AppItem> dataList;
private RvAdapter rvAdapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList=new ArrayList<AppItem>();
dataList.add(new AppItem(R.drawable.icon_dog,"dog"));
dataList.add(new AppItem(R.drawable.icon_cat,"cat"));
dataList.add(new AppItem(R.drawable.icon_tuzi,"tuzi"));
recyclerView=findViewById(R.id.rv);
//管理渲染布局
recyclerView.setLayoutManager(new GridLayoutManager(this,3));
//设置分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this, GridLayoutManager.VERTICAL));
rvAdapter=new RvAdapter(dataList,this);
recyclerView.setAdapter(rvAdapter);
}
}
。。
标签:layout,name,dataList,使用,import,android,RecyclerView,public 来源: https://www.cnblogs.com/a-n-yan/p/16220570.html