自从 Android 5.0 之后,google 推出了一个 RecyclerView 控件,他是 support-v7 包中的新组件,是一个强大的滑动组件,与经典的 ListView 相比,同样拥有
引入 RecyclerVIew
compile 'com.android.support:recyclerview-v7:25.1.0'
创建布局文件
主布局文件
/*activity_main.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context="com.manu.mrecyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Item布局文件
/*item.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="@+id/tv_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="data"
android:background="#cac3c3"
android:padding="10dp"
android:textSize="20sp"/>
</LinearLayout>
创建Adapter
onCreateViewHolder()
onBindViewHolder()
getItemCount()
Adapter代码
/**
* Created by jzman on 2017/5/13 0013.
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
private Context mContext;
private ArrayList<String> mList;
public RvAdapter() {}
public RvAdapter(Context mContext,ArrayList<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
DataViewHolder holder = new DataViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(DataViewHolder holder,int position) {
holder.tv_data.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList.size();
}
public static class DataViewHolder extends RecyclerView.ViewHolder{
TextView tv_data;
public DataViewHolder(View itemView) {
super(itemView);
tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
}
}
/**
* Created by jzman on 2017/5/13 0013.
* RecycleView的Adapter
*/
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
private Context mContext;
private RecyclerView recyclerView;
private ArrayList<String> mList;
private ArrayList<Integer> mHeight;
public RvAdapter() {}
public RvAdapter(Context mContext,ArrayList<String> mList) {
this.mContext = mContext;
this.mList = mList;
}
/**
* 初始化每个Item的高度(瀑布流效果)
* @return
*/
public ArrayList<Integer> initHeight(){
mHeight = new ArrayList<>();
for (int i=0;i<mList.size();i++){
mHeight.add((int) (Math.random()*300)+200);
}
return mHeight;
}
/**
* 用于创建ViewHolder
* @param parent
* @param viewType
* @return
*/
@Override
public DataViewHolder onCreateViewHolder(ViewGroup parent,ViewGroup.LayoutParams.WRAP_CONTENT));
DataViewHolder holder = new DataViewHolder(view);
return holder;
}
/**
* 绑定数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(DataViewHolder holder,int position) {
ViewGroup.LayoutParams h = holder.tv_data.getLayoutParams();
h.height = mHeight.get(position);
holder.tv_data.setText(mList.get(position));
}
/**
* 选项总数
* @return
*/
@Override
public int getItemCount() {
return mList.size();
}
/**
* 创建ViewHolder
*/
public static class DataViewHolder extends RecyclerView.ViewHolder{
TextView tv_data;
public DataViewHolder(View itemView) {
super(itemView);
tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
}
}
/**
* 将RecycleView附加到Adapter上
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView= recyclerView;
initHeight();
}
/**
* 将RecycleView从Adapter解除
*/
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
this.recyclerView = null;
}
}
MainActivity
/**
* Created by jzman on 2017/5/13 0013.
*/
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
RvAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
adapter = new RvAdapter(this,initData());
rv.setAdapter(adapter);
}
public static ArrayList<String> initData(){
ArrayList<String> arrayList = new ArrayList<>();
for (int i=0;i<50;i++){
arrayList.add("第"+i+"条数据");
}
return arrayList;
}
}
显示效果
LinearLayoutManager |
GridLayoutManager |
StaggeredGridLayoutManager |
|
|
|