一、前言:RecylerView也是一种类似于ListView的控件,用途基本和ListView一毛一样,但是他相对于ListView来说,会简单一些,而且功能上面更加强大,比如ListView只能将内容竖向排列,而且效率不高,但是RecylerView就可以自己设定其他的排列方式:横向,网格,瀑布等形式。所以说RecylerView是ListView的加强版。不过来笔记中我只写了它的横向排列的实现方法。
二、效果截图:
可以看出来这个真的是十分的简单了。那么直接看看代码吧。
三、实现过程
1. 要使用RecylerView这个控件,首先要在项目的build.grade里面添加依赖库,打开app/build.grade,代码如下
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
//添加RecyclerView依赖库
compile 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
2. 在activity_main.xml中添加RecylerView控件
<?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="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/mRv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
/>
</LinearLayout>
3.写一个java类China,他的属性里面包括name和Image就是要显示在RecylerView里的东西
public class China {
private String name;
private int image;
public China(String name,int image){
this.name = name;
this.image = image;
}
public String getName(){
return name;
}
public int getIcon(){
return image;
}
}
4.自定义要显示在RecylerView里面的标签(Item)布局,里面一个ImageView用来显示图片,TextView用来显示文字
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="100dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
</LinearLayout>
5.自定义一个适配器,用来将数据传递布局文件Item中
public class ChinaAdapter extends RecyclerView.Adapter <ChinaAdapter.ViewHolder>{
private List<China> mFL;
static class ViewHolder extends RecyclerView.ViewHolder{
View chinaView;
ImageView image;
TextView name;
public ViewHolder(View view){
super(view);
chinaView = view;
image = view.findViewById ( R.id.image );
name = view.findViewById ( R.id.name );
}
}
public ChinaAdapter(List<China> chinaList){
mFL = chinaList;
}
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view = LayoutInflater.from (parent.getContext() )
.inflate ( R.layout.item,parent,false );
final ViewHolder holder = new ViewHolder ( view );
//点击事件的监控和处理
holder.chinaView.setOnClickListener ( new View.OnClickListener (){
public void onClick(View v){
int position = holder.getAdapterPosition ();
China china = mFL.get ( position );
Toast.makeText ( v.getContext (),"you clicked view "+
china.getName (),Toast.LENGTH_SHORT ).show ();
}
} );
return holder;
}
public void onBindViewHolder(ViewHolder holder,int i){
China china = mFL.get ( i );
holder.image.setImageResource ( china.getIcon () );
holder.name.setText ( china.getName () );
}
public int getItemCount(){
return mFL.size ();
}
}
这样大部分的工作就已经做完了,最后在MainActivity文件中将数据加载到RecyclerView中:
6. MainActivity文件:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRv;
private List<China> chinaList = new ArrayList<> ( );
private String[] data = { "北京","重庆","甘肃","上海", "浙江","江苏"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
initChina ();//初始化数据
mRv = findViewById ( R.id.mRv );
//布局设置LinearLayoutManager线性布局、GridLayoutManager网格布局、
// StaggeredGridLayoutManager瀑布流布局
LinearLayoutManager layoutManager = new LinearLayoutManager ( this );
layoutManager.setOrientation ( LinearLayoutManager.HORIZONTAL );
mRv.setLayoutManager ( layoutManager );
ChinaAdapter adapter = new ChinaAdapter (chinaList);
mRv.setAdapter ( adapter );
}
private void initChina(){
for(int i = 0;i < 2;i++){
China bj = new China ( data[0],R.mipmap.bj );
chinaList.add ( bj );
China cq = new China ( data[1],R.mipmap.cq );
chinaList.add ( cq );
China gs = new China ( data[2],R.mipmap.gs );
chinaList.add ( gs );
China sh = new China ( data[3],R.mipmap.sh );
chinaList.add ( sh );
China zj = new China ( data[4],R.mipmap.zj );
chinaList.add ( zj );
China js = new China ( data[5],R.mipmap.js);
chinaList.add ( js );
}
}
}
这样就完成了。