整理总结自鸿洋的博客:http://blog.csdn.net/lmj623565791/article/details/38173061/
一、初次使用RecyclerView实现”适配器“的功能
1、MainActivity.java
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private GalleryAdapter mAdapter;
private List<Integer> mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//获得数据
initDatas();
//得到控件
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
//设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
//设置适配器
mAdapter = new GalleryAdapter(this, mDatas);
mRecyclerView.setAdapter(mAdapter);
}
private void initDatas() {
mDatas = new ArrayList<Integer>(Arrays.asList(R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e, R.mipmap.f, R.mipmap.g, R.mipmap.h, R.mipmap.l));
}
}
2、activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview_horizontal"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_centerVertical="true"
android:background="#FF0000"
android:scrollbars="none" />
</RelativeLayout>
3、activity_index_gallery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@drawable/item_bg02">
<ImageView
android:id="@+id/id_index_gallery_item_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/id_index_gallery_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/id_index_gallery_item_image"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="some info"
android:textColor="#ff0000"
android:textSize="12dp" />
</RelativeLayout>
4、GalleryAdapter.java
/**
* Created by jiatao on 2016/06/14 18:26
* Description: 应用RecyclerView的适配器
*/
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
private LayoutInflater mInflater;
private List<Integer> mDatas;
public GalleryAdapter(Context context, List<Integer> datats) {
mInflater = LayoutInflater.from(context);
mDatas = datats;
}
/**
* 创建内部静态类,必须继承RecyclerView.ViewHolder。承上启下。
* 上:RecyclerView.Adapter<GalleryAdapter.ViewHolder>
* 下:ViewHolder viewHolder = new ViewHolder(view);
* 详细说明:
* 我们创建的ViewHolder必须继承RecyclerView.ViewHolder,这个RecyclerView.ViewHolder在构造时必须传入一个View,
* 这个View相当于我们ListView getView中的convertView (即:把我们需要inflate的item布局传入)
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView mImg;
TextView mTxt;
public ViewHolder(View view) {
super(view);
}
}
/**
* 获取总条目数,同BaseAdapter
*/
@Override
public int getItemCount() {
return mDatas.size();
}
/**
* 创建ViewHolder
*/
@Override
public ViewHolder onCreateViewHolder(Vi