引入第三方库
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
xml简单布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".RecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
简单的数据格式
package com.example.viewpagerapplication.data;
import android.view.View;
public class Menu {
public int age;
public String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1.0组织数据
2.0.拿到recyclerView布局
// 3.0 布局模式(这里是线性布局) //4.new 一个 Adapter加入
package com.example.viewpagerapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.example.viewpagerapplication.data.Menu;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
protected RecyclerView recyclerView; // 主菜单
private List<Menu> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
// 1.0组织数据
for (int i = 0; i <100 ; i++) {
Menu mainMenu = new Menu();
mainMenu.setAge(i+10);
mainMenu.setName("名称"+i);
data.add(mainMenu);
}
//2.0.拿到recyclerView布局
recyclerView = findViewById(R.id.recyclerView);
// 3.0 布局模式(这里是默认线性布局)
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
// 3.1 如果是需要横屏
// linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
// 网格布局
// GridLayoutManager gridLayoutManager = new GridLayoutManager(this,4);
// recyclerView.setLayoutManager(gridLayoutManager);
//4.new 一个 Adapter加入
MainMenuAdapter mainMenuAdapter = new MainMenuAdapter(data,this);
recyclerView.setAdapter(mainMenuAdapter);
}
}
MainMenuAdapter
里面的定义
package com.example.viewpagerapplication;
import android.content.Context;
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 com.example.viewpagerapplication.data.Menu;
import java.util.List;
public class MainMenuAdapter extends RecyclerView.Adapter<MainMenuAdapter.MenuViewHolder> {
private List<Menu> data;
private Context context;
public MainMenuAdapter(List<Menu> data, Context context) {
this.data = data;
this.context = context;
}
@NonNull
@Override
public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.recycler_view_item,null);
return new MenuViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MenuViewHolder holder, int position) {
holder.tv.setText(data.get(position).name);
}
@Override
public int getItemCount() {
return data!=null ? data.size() :0;
}
public class MenuViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
private ImageView imageV;
public MenuViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
// imageV = itemView.findViewById(R.id.imageViewSrc);
}
}
}
recycler_view_item简单布局
<?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">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>