android学习日记 RecyclerView的简单使用
文章目录
一、如何使用RecyclerView?
大致步骤如下:
1.导入依赖包;
2.编写RecyclerView适配器 (绑定控件类+控件操作);
3.编写RecyclerView布局文件;
4.编写主activity完成绑定适配器显示在界面上;
二、使用步骤
代码如下:
1、首先在build:gradle(:app)下引入包
implementation 'androidx.recyclerview:recyclerview:1.1.0'
2、编写适配器 MyAdapter.java
package com.example.practice.adapter;
import android.graphics.Color;
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 com.example.practice.R;
/**
* 创建RecylerView适配器
*
* 注意:初始化时线
*
* */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
/**
* 传入参数 这里替代为数组 后续可替换为List<Fragment>
* */
String[] str;
/**
* 创建构造方法 传入参数
* */
public MyAdapter(String[] str) {
this.str = str;
}
/**
* 绑定布局文件 R.layout.item_recyclerview
* */
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,parent,false);
return new MyViewHolder(itemView);
}
/**
*为holder上的 控件赋值
* */
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
//赋值图片
holder.imageView.setImageResource(R.mipmap.ic_launcher);
//赋值文本
holder.textView.setText("item "+position);
//赋值颜色
holder.textView.setTextColor(Color.BLACK);
holder.itemView.setBackgroundColor(Color.RED);
}
/**
* 返回制作的控件个数 (以 数组/集合 的长度为基准)
* */
@Override
public int getItemCount() {
return str.length;
}
/**
* 绑定布局文件的控件
* */
class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.itemImageView);
textView = itemView.findViewById(R.id.itemTextView);
}
}
}
3、编写布局文件 item_recyclerview.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 注意:这里必须使用RelactiveLayout -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left|center"
>
<ImageView
android:id="@+id/itemImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/itemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="张三"
android:textSize="25dp"
android:textColor="@color/black"/>
</LinearLayout>
</RelativeLayout>
最后 编写主Activity文件或者fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
</LinearLayout>
package com.example.practice;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.practice.adapter.MyAdapter;
public class MainActivity extends AppCompatActivity {
/**
*创建 RecyclerView控件对象
* */
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
/**
* 绑定 RecyclerView控件
* */
recyclerView = findViewById(R.id.recyclerview);
/**这里表示可以传入相应的碎片集合
* 这里用数组代替 后续可以代替为
* List<Fragment>*/
//绑定适配器并显示在界面上
recyclerView.setAdapter(new MyAdapter(new String[]{"1","2","3"}));
// 添加分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
// 转换为网格布局
// recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
}
}