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;
}
-
适配器【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;
}
-
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;