RecyclerView是谷歌提供的控件,与ListView相似,但更强大
1.导入依赖:
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
在activity_main.xml创建列表视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_recyclerview"
>
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
再创建单个列表元素的视图:
recycler_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:gravity="center"
android:orientation="horizontal"
>
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/iv"
android:src="@drawable/ic_launcher_foreground"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textSize="20sp"
android:textColor="#FF8F03"
android:text="哈士奇"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/introduce"
android:textSize="16sp"
android:layout_marginTop="10dp"
android:layout_below="@+id/name"
android:textColor="#FF716C6D"
android:maxLines="2"
android:ellipsize="end"
android:text="西伯利亚雪橇犬,别名二哈,或者哈士奇"
/>
</RelativeLayout>
</LinearLayout>
逻辑MainActivity.java:
package com.example.againrecyclerview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private HomeAdapter homeAdapter;
private String[] names = {"鱼人","猪人","猎犬","蝙蝠","克劳斯"};
private String[] introduces = {"西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇","西伯利亚雪橇犬,别名二哈,或者哈士奇"};
private int[] icons = {R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = ((RecyclerView) findViewById(R.id.id_recyclerview));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
homeAdapter = new HomeAdapter();
recyclerView.setAdapter(homeAdapter);
}
class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.recycler_item,parent,false));
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.name.setText(names[position]);
holder.introduce.setText(introduces[position]);
holder.iv.setBackgroundResource(icons[position]);
}
@Override
public int getItemCount() {
return names.length;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView iv;
TextView introduce;
public MyViewHolder(View view){
super(view);
name = ((TextView) view.findViewById(R.id.name));
iv = ((ImageView) view.findViewById(R.id.iv));
introduce = ((TextView) view.findViewById(R.id.introduce));
}
}
}
}
要点创建两个内部类:
HomeAdapter 继承 RecyclerView.Adapter类 重写 其方法 :
onCreatViewHolder()方法主要是加载布局文件
onBindViewHolder()方法主要将数据设置到对应的控件上
getItemCount()方法获取列表条目总数
MyViewHolder 类继承 RecyclerView.ViewHolder
主要获取Item界面上的控件