Simple use of RecyclerView
Also includes knowledge points: CardView
framework: Glide
Steps for usage
- Add dependencies
- Add the RecyclerView control to the main layout file. (activity_main.xml)
- Create a subitem layout file. (practiced_item.xml)
- Create a class to populate the object (content) for the child. (Practice.java)
- Create an adapter for RecycleView, simply put the sub-item written in step 3 into the RecycleView written in step 2. (PracticeAdapter.java)
- Called in the Activity that adds RecyclerView. (MainActivity.java)
1. Add dependency package
implementation 'com.android.support:recyclerview-v7:28.0.0'
2. Add the RecyclerView control (activity_main.xml) to the main layout file
and add the behavior attribute value "@string/appbar_scrolling_view_behavior" to prevent the toolbar from blocking the recyclerView.
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
3. Create a sub-item layout file (practiced_item.xml)
to use CardView here, you need to add the dependency library implementation 'com.android.support:cardview-v7:28.0.0'
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="4dp"
android:id="@+id/card_view">
<RelativeLayout
android:id="@+id/item_layout"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/item_text"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
4. Create a class to fill the object (content) for the subitem, here add the Practice class (Practice.java)
public class Practice {
private String name;
private int id;
public Practice(String name, int id) {
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public int getId() {
return id;
}
}
5. Create an adapter (PracticeAdapter.java) for RecycleView. Simply put, it is to load the sub-item written in step 3 into the RecycleView written in step 2.
//1、继承RecyclerView.Adapter,并将泛型指为<PracticeAdapter.ViewHolder>,
//需要重构 onCreateViewHolder()、onBindViewHolder()、getItemCount()三个方法
public class PracticeAdapter extends RecyclerView.Adapter<PracticeAdapter.ViewHolder> {
final int RETROFIT2 = 1;
public Context mContext;
public List<Practice> practiceList;
//2、建一个ViewHolder对控件实例进行缓存,需继承RecyclerView.ViewHolder。
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView practice_name_view;
RelativeLayout relativeLayout;
//参数view通常是RecycleView的子项的最外层布局
public ViewHolder(View view) {
super(view);
cardView = (CardView) view;
practice_name_view = view.findViewById(R.id.item_text);
relativeLayout = view.findViewById(R.id.item_layout);
}
}
public PracticeAdapter(List<Practice> practices) {
//3、将需要添加的数据源传进来,给到全局变量
this.practiceList = practices;
}
//4、重写onCreateViewHolder(),用于创建ViewHolder实例
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (mContext == null) {
mContext = viewGroup.getContext();
}
//将子项布局文件动态加载进来
View view = LayoutInflater.from(mContext).inflate(R.layout.practiced_item, viewGroup, false);
final ViewHolder viewHolder = new ViewHolder(view);
//对子项添加点击监听器
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取点击的位置
int position = viewHolder.getAdapterPosition();
//获取对应的对象
Practice practice = practiceList.get(position);
//自定义操作
selectPractice(practice);
}
});
//返回实例
return viewHolder;
}
//自定义操作
private void selectPractice(Practice practice) {
Intent intent;
switch (practice.getId()) {
case RETROFIT2:
intent = new Intent(mContext, SearchCity.class);
mContext.startActivity(intent);
break;
default:
Toast.makeText(mContext, "Testing", Toast.LENGTH_SHORT).show();
break;
}
}
//5、重写 onBindViewHolder(),作用是对子项数据进行赋值,每个子项被滚动到屏幕内时执行
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int position) {
Practice practice = practiceList.get(position);
viewHolder.practice_name_view.setText(practice.getName());
//这里使用了Glide框架加载子项的背景图片,relativeLayout在子项布局里设置了
SimpleTarget<Drawable> target = new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
//SimpleTarget<Drawable>()可以为RelativeLayout、LinearLayout等布局设置背景
viewHolder.relativeLayout.setBackground(resource);
}
};
//使用Glide需要添加依赖库 implementation 'com.github.bumptech.glide:glide:4.9.0'
Glide.with(mContext).load(R.drawable.bg_item).into(target);
}
//6、重写 getItemCount() ,返回子项的数量,一般返回数据源的长度即可
@Override
public int getItemCount() {
return practiceList.size();
}
}
6. Call (MainActivity.java) in the Activity that adds RecyclerView
//初始化数据源
public List<Practice> practiceList = new ArrayList<>();
practiceList.add(new Practice("Retrofit2", 1));
practiceList.add(new Practice("Test", 2));
RecyclerView recyclerView = findViewById(R.id.recycle_view);
//为recyclerView指定布局方式,GridLayout网格布局,(context,spanCount列数)
GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
recyclerView.setLayoutManager(layoutManager);
//设置适配器,把数据源传进去
PracticeAdapter adapter = new PracticeAdapter(practiceList);
recyclerView.setAdapter(adapter);