滚动列表。
一、使用步骤
2 个布局文件(容器布局+列表项布局),3 个java类(列表项实体类+适配器类+加载适配器的活动类)。
1. 添加依赖项
打开 app/build.gradle,添加 RecyclerView 依赖项,之后点击编辑器右上角 Sync Now 同步更新依赖。
dependencies {
....
implementation 'com.android.support:recyclerview-v7:28.0.0'
....
}
2. 添加子控件
在布局文件中添加 RecyclerView 控件,需要写全包名。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3. 列表项布局文件
RecyclerView 控件相当于类表最外层的容器,内部每个项还需要单独布局,在 layout 目录下新建单个项的布局文件 list_novel_item.xml。
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/novel_cover"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"/>
<TextView android:id="@+id/novel_book"
android:layout_width="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<TextView android:id="@+id/novel_author"
android:layout_width="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
4. 实体类
列表项需要个实体类:Novel.java。
public class Novel {
private int id;
private String authorName;
private String bookName;
private String url;
public Novel(int id, String authorName, String bookName, String url) {
this.id = id;
this.authorName = authorName;
this.bookName = bookName;
this.url = url;
}
public int getId() {
return id;
}
public String getAuthorName() {
return authorName;
}
public String getBookName() {
return bookName;
}
public String getUrl() {
return url;
}
}
5. 适配器
布局文件整好还不行,还需要有适配器类把列表项布局跟列表关联起来。在 java 目录下新建适配器文件 NovelAdapter.java。适配器需要继承 RecyclerView.Adapter 类,并实现它的三个方法:
onCreateViewHolder
创建控件,这个方法里关联列表项
布局文件;onBindViewHolder
绑定数据;getItemCount
列表总数目。
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class NovelAdapter extends RecyclerView.Adapter<NovelAdapter.ViewHolder> {
private List<Novel> novelList;
static class ViewHolder extends RecyclerView.ViewHolder {
View novelView;
ImageView novelCover;
TextView novelAuthorName;
TextView novelBookName;
public ViewHolder(View view) {
super(view);
novelView = view;
this.novelCover = view.findViewById(R.id.novel_cover);
this.novelAuthorName = view.findViewById(R.id.novel_author);
this.novelBookName = view.findViewById(R.id.novel_book);
}
}
public NovelAdapter(List<Novel> novelList) {
this.novelList = novelList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.novel_item,
viewGroup, false);
final ViewHolder holder = new ViewHolder(view);
// 给整个 view 绑定事件
holder.novelCover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Novel novel = novelList.get(position);
Toast.makeText(view.getContext(), "You click view " + novel.getBookName(),
Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Novel novel = novelList.get(position);
holder.novelCover.setImageResource(R.drawable.cover);
holder.novelAuthorName.setText(novel.getAuthorName());
holder.novelBookName.setText(novel.getBookName());
}
@Override
public int getItemCount() {
return novelList.size();
}
}
6. 在活动中引用
public class MainActivity extends AppCompatActivity {
private List<Novel> novelList = new ArrayList<Novel>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initNovel();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
NovelAdapter novelAdapter = new NovelAdapter(novelList);
recyclerView.setAdapter(novelAdapter);
}
// 模拟列表数据,真实情况为请求服务端数据
private void initNovel () {
for (int i = 0; i < 30; i++) {
Novel novel1 = new Novel(12, "净无痕"+i, "太古神王"+i, "https://www.biquge5200.cc/2_2157/");
novelList.add(novel1);
}
}
}
二、横向滚动列表
修改列表项布局 list_novel_item.xml,给每一项固定宽度。如下修改 LinearLayout 控件宽度。
<LinearLayout android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/novel_cover"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"/>
......
</LinearLayout>
修改活动类中代码。
...
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // 新增代码
recyclerView.setLayoutManager(linearLayoutManager);
...
三、瀑布流滚动列表
使用 StaggeredGridLayoutManager 类管理 RecyclerView 的布局,第一个参数表示有几列,第二个参数表示垂直滚动还是水平滚动。
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,
StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
四、网格滚动列表
使用 GridLayoutManager 类管理 RecyclerView 的布局,和 StaggeredGridLayoutManager 布局很相似,默认只能垂直滚动,第一个参数是当前活动,第二个参数设置有几列。
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);