RecylerView是support-v7包中的新组件,是一个强大的滑动组件,那么已经有了ListView,为什么还要存在RecyclerView,因为他的优势有很多啊。
1.自带缓存原理
2.灵活的控制item的增删动画,布局构造也很方便改变。
3.扩展性好
1.添加依赖包
2.创建布局文件(类比电动车)
3.创建实体类(类比充电器电线)
4.创建适配器(类比充电器核心部分)
5.填充数据,绑定适配器(电源,同时连接充电器,电动车)
compile 'com.android.support:recyclerview-v7:26.1.0'
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_imgv"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="梅西"
android:textSize="25sp"
android:gravity="center_vertical"/>
LinearLayout>
public class Player {
//表示球星的名字
private String playerName;
//表示图片的id
private int imgID;
public Player(String playerName, int imgID) {
this.playerName = playerName;
this.imgID = imgID;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getImgID() {
return imgID;
}
public void setImgID(int imgID) {
this.imgID = imgID;
}
}
//自定义类继承RecyclerView.Adapter,将泛型指定为PlayerAdapter.ViewHolder
public class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.ViewHolder> {
//创建list集合,泛型为之前定义的实体类
private List playerList;
//添加构造方法
public PlayerAdapter(List playerList) {
this.playerList = playerList;
}
//在onCreateViewHolder()中完成布局的绑定,同时创建ViewHolder对象,返回ViewHolder对象
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_item,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
//在内部类中完成对控件的绑定
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.item_imgv);
textView = itemView.findViewById(R.id.item_tv);
}
}
//在onBindViewHolder()中完成对数据的填充
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageResource(playerList.get(position).getImgID());
holder.textView.setText(playerList.get(position).getPlayerName());
}
//这个方法很简单了,返回playerList中的子项的个数
@Override
public int getItemCount() {
return playerList.size();
}
}
1.修改MainActivity中的布局,添加RecyclerView控件:
"1.0" encoding="utf-8"?>
.support.constraint.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="com.example.recyclerviewtest.MainActivity">
.support.v7.widget.RecyclerView
android:id="@+id/main_rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
.support.v7.widget.RecyclerView>
.support.constraint.ConstraintLayout>
2.修改MainActivity中的代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List playerList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initList()方法用于给playerList填充数据
initList();
recyclerView = findViewById(R.id.main_rv);
//创建LinearLayoutManager,用于决定RecyclerView的布局方式
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(linearLayoutManager);
//创建适配器
PlayerAdapter adapter = new PlayerAdapter(playerList);
recyclerView.setAdapter(adapter);
}
private void initList() {
for (int i = 0;i<3;i++){
//通过条用构造方法,赋值
Player messi = new Player("梅西",R.mipmap.messi);
//将数据填充到playerList中去
playerList.add(messi);
Player cluo = new Player("C罗",R.mipmap.cluo);
playerList.add(cluo);
Player guadi = new Player("瓜迪奥拉",R.mipmap.guadi);
playerList.add(guadi);
Player debu = new Player("德布劳内",R.mipmap.debu);
playerList.add(debu);
Player xiaobai = new Player("小白",R.mipmap.xiaobai);
playerList.add(xiaobai);
Player sunke = new Player("孙可",R.mipmap.sunke);
playerList.add(sunke);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_imgv"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:id="@+id/item_tv"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="梅西"
android:textSize="15sp"
/>
LinearLayout>
recyclerView = findViewById(R.id.main_rv);
//创建LinearLayoutManager,用于决定RecyclerView的布局方式
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
//通过setOrientation()设置布局的排列方向
linearLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
//创建适配器
PlayerAdapter adapter = new PlayerAdapter(playerList);
recyclerView.setAdapter(adapter);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/item_imgv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:text="梅西"
android:textSize="15sp"
android:layout_marginTop="10dp"/>
LinearLayout>
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List playerList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initList()方法用于给playerList填充数据
initList();
recyclerView = findViewById(R.id.main_rv);
//创建StaggeredGridLayoutManager,瀑布布局
/**
* @param spanCount If orientation is vertical, spanCount is number of columns. If
* orientation is horizontal, spanCount is number of rows. 分割的数目,具体可以看英文备注
* @param orientation {@link #VERTICAL} or {@link #HORIZONTAL} 布局的排列方式
*/
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
//创建适配器
PlayerAdapter adapter = new PlayerAdapter(playerList);
recyclerView.setAdapter(adapter);
}
private void initList() {
for (int i = 0;i<3;i++){
//通过条用构造方法,赋值,调用getRandomLengthName()方法获取随机长度的名字
Player messi = new Player(getRandomLengthName("梅西"),R.mipmap.messi);
//将数据填充到playerList中去
playerList.add(messi);
Player cluo = new Player(getRandomLengthName("C罗"),R.mipmap.cluo);
playerList.add(cluo);
Player guadi = new Player(getRandomLengthName("瓜迪奥拉"),R.mipmap.guadi);
playerList.add(guadi);
Player debu = new Player(getRandomLengthName("德布劳内"),R.mipmap.debu);
playerList.add(debu);
Player xiaobai = new Player(getRandomLengthName("小白"),R.mipmap.xiaobai);
playerList.add(xiaobai);
Player sunke = new Player(getRandomLengthName("孙可"),R.mipmap.sunke);
playerList.add(sunke);
}
}
//这里创建getRandomLengthName()方法是因为,如果名字的长度几乎一样长,瀑布流的效果就不能很明显得展现出来
private String getRandomLengthName(String name){
//产生1-10的随机数
Random random = new Random();
int length = random.nextInt(10)+1;
//创建StringBuilder对象,他是字符长变量,可以通过append()方法增长他的长度
StringBuilder builder = new StringBuilder();
for (int i=0;ireturn builder.toString();
}
}
//自定义类继承RecyclerView.Adapter,将泛型指定为PlayerAdapter.ViewHolder
public class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.ViewHolder> {
//创建list集合,泛型为之前定义的实体类
private List playerList;
//添加构造方法
public PlayerAdapter(List playerList) {
this.playerList = playerList;
}
//在onCreateViewHolder()中完成布局的绑定,同时创建ViewHolder对象,返回ViewHolder对象
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_item,parent,false);
final ViewHolder holder = new ViewHolder(view);
//在onCreateViewHolder()中对子项进行监听
//holder.playerView说明他是对整个子项进行监听
holder.playerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//通过getAdapterPosition()方法获取点击的是哪个子项
int position = holder.getAdapterPosition();
Toast.makeText(v.getContext(),playerList.get(position).getPlayerName(), Toast.LENGTH_SHORT).show();
}
});
//holder.imageView说明他是对图片进行监听
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Toast.makeText(v.getContext(), ""+playerList.get(position).getImgID(), Toast.LENGTH_SHORT).show();
}
});
return holder;
}
//在内部类中完成对控件的绑定
static class ViewHolder extends RecyclerView.ViewHolder {
//创建View变量playerView,用来对整个子项进行监听
private View playerView;
private ImageView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
//将子项对外层的布局itemView赋给playerView,这样就能表示整个子项
playerView = itemView;
imageView = itemView.findViewById(R.id.item_imgv);
textView = itemView.findViewById(R.id.item_tv);
}
}
//在onBindViewHolder()中完成对数据的填充
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageResource(playerList.get(position).getImgID());
holder.textView.setText(playerList.get(position).getPlayerName());
}
//这个方法很简单了,返回playerList中的子项的个数
@Override
public int getItemCount() {
return playerList.size();
}
}
如果对你有所帮助:这是我的支付宝账户:18018346990,哈哈