RecyclerView系列:RecyclerView基本使用java版
Posted zhangjin1120
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView系列:RecyclerView基本使用java版相关的知识,希望对你有一定的参考价值。
RecyclerView系列一直缺一篇基础使用的文章,终于补上了,为后续分析第三方库BaseRecyclerViewAdapterHelper
做准备。
先上效果图,为了醒目,我加了边框背景。
我用的是android studio BumbleBee | 2021.1.1 Patch 3
,新建项目后,默认导入了recyclerview,所以不用手动导入recyclerView的依赖。如下图:
低版本Android Studio还是需要手动导入的,可以参考:RecyclerView使用指南(一)—— 基本使用
布局文件activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
item的布局文件navigation_item.xml
<?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"
android:id="@+id/nav_item_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/nav_item_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/icon_tabbar_home_sel"
app:layout_constraintBottom_toTopOf="@id/nav_item_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/nav_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="首页"
android:textColor="#ffffff"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/nav_item_icon" />
</androidx.constraintlayout.widget.ConstraintLayout>
ItemData,NavigationViewAdapter一起贴出来
public class ItemData
public String title;
public int imgId;
public ItemData(String title, int imgId)
this.title = title;
this.imgId = imgId;
public class NavigationViewAdapter extends RecyclerView.Adapter<NavigationViewAdapter.NavigationViewHolder>
private ArrayList<ItemData> list;
public NavigationViewAdapter(ArrayList<ItemData> list)
this.list = list;
@NonNull
@Override
public NavigationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.navigation_item, parent, false);
NavigationViewHolder viewHolder = new NavigationViewHolder(view);
return viewHolder;
@Override
public void onBindViewHolder(@NonNull NavigationViewHolder holder, int position)
ItemData itemData = list.get(position);
holder.nagv_icon.setImageResource(itemData.imgId);
holder.nagv_title.setText(itemData.title);
@Override
public int getItemCount()
return list.size();
public static class NavigationViewHolder extends RecyclerView.ViewHolder
ImageView nagv_icon;
TextView nagv_title;
public NavigationViewHolder(@NonNull View itemView)
super(itemView);
nagv_icon = itemView.findViewById(R.id.nav_item_icon);
nagv_title = itemView.findViewById(R.id.nav_item_text);
MainActivity中初始化RecyclerView,设置LayoutMananger和Adapter
public class MainActivity extends AppCompatActivity
RecyclerView rv;
ArrayList<ItemData> mList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
initList();
NavigationViewAdapter adapter = new NavigationViewAdapter(mList);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
private void initList()
mList = new ArrayList<>();
mList.add(new ItemData("首页", R.mipmap.icon_tabbar_home_sel));
mList.add(new ItemData("房间", R.mipmap.icon_tabbar_room));
mList.add(new ItemData("全屋", R.mipmap.icon_tabbar_home_sel));
mList.add(new ItemData("应用", R.mipmap.icon_tabbar_home_sel));
mList.add(new ItemData("通行", R.mipmap.icon_tabbar_home_sel));
mList.add(new ItemData("社区", R.mipmap.icon_tabbar_home_sel));
需要的两张图片
直接附在博客上有水印,所以大家自己去阿里巴巴矢量图标库上找吧!
开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系以上是关于RecyclerView系列:RecyclerView基本使用java版的主要内容,如果未能解决你的问题,请参考以下文章
Android L 的 RecyclerView 面临的挑战