关于RecyclerView的基本用法
开发步骤
1.在app/build.gradle中添加依赖
dependencies {
compile 'com.android.support:recyclerview-v7:25.1.0'
}
2.Activity中的布局
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.junjie.recyclerviewdemo.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimaryDark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="RecyclerView的使用小结"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3.RecyclerView的adapter
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.junjie.recyclerviewdemo.R;
import java.util.ArrayList;
import java.util.List;
/**
*@author JunJie
*
* RecyclerView的adapter
*
*@time 2017/1/2 19:47
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHodler> {
private Context context;//上下文
private List<String> list;//数据源
private OnRecyclerViewListener recyclerViewListener;//向外提供的接口
public void setRecyclerViewListener(OnRecyclerViewListener recyclerViewListener) {
this.recyclerViewListener = recyclerViewListener;
}
public MyRecyclerViewAdapter(Context context, List<String> list) {
this.context = context;
setList(list);
}
/**
* 防止数据源为空 空指针
*
* @param list
*/
public void setList(List<String> list) {
if (list == null) {
list = new ArrayList<>();
}
this.list = list;
}
//RecyclerView显示的子View
//该方法返回是ViewHolder,当有可复用View时,就不再调用
@Override
public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
// 此处代码与ListView中的adapter 的写法有点小小区别 不这样写 会出现item不能铺满的情况
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_list,parent, false);
ViewHodler holder = new ViewHodler(view);
return holder;
}
//将数据绑定到子View,会自动复用View
@Override
public void onBindViewHolder(ViewHodler holder, int position) {
String s = list.get(position);
holder.tvName.setText(s);
holder.position =position;
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHodler extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tvName;
int position;
public ViewHodler(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tv_stu_name);
itemView.setOnClickListener(this); // 被复用的view的点击事件
}
@Override
public void onClick(View v) {
if(recyclerViewListener!=null){
recyclerViewListener.onItemClick(v,position);
}
}
}
public interface OnRecyclerViewListener{
void onItemClick(View view,int position); // 单点
}
}
4.RecyclerView的item布局
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_stu_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="#aabbff"
android:gravity="center"
android:padding="10dp" />
</LinearLayout>
5.Activity中代码
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.OnRecyclerViewListener {
@InjectView(R.id.recyclerView)
RecyclerView recyclerView;
private List<String> studentList = new ArrayList<>();//数据源
private MyRecyclerViewAdapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
initData();
//线性布局的管理器
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);//竖直方向
// manager.setOrientation(LinearLayoutManager.HORIZONTAL);//横着方向
recyclerView.setLayoutManager(manager);
recyclerViewAdapter = new MyRecyclerViewAdapter(this, studentList);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setRecyclerViewListener(this);
}
private void initData() {
for (int i = 0; i < 20; i++) {
studentList.add("item" + i);
}
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "被点击 位置" + position, Toast.LENGTH_SHORT).show();
}
6.生成和GridView一样的效果
//使用GridView的管理器 4表示的是网格中的列或行数. GridLayoutManager.VERTICAL表示布局方向
//最后一个参数当设置为true,从结束到开始布局。
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
7.生成瀑布流效果
//activity中 管理器换成瀑布流效果的管理器
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
private List<Integer> integerList = new ArrayList<>();//随机高度的集合
//瀑布流的管理器
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerViewAdapter = new MyRecyclerViewAdapter(this, studentList,integerList);
recyclerView.setAdapter(recyclerViewAdapter);
for (int j = 0; j < studentList.size(); j++) {
integerList.add((int) (100 + Math.random() * 300));
}
adapter中
private List<Integer> heightList;//高度集合数据源
public MyRecyclerViewAdapter(Context context, List<String> list,List<Integer> heightList) {
this.context = context;
this.heightList =heightList;
setList(list);
}
//将数据绑定到子View,会自动复用View
@Override
public void onBindViewHolder(ViewHodler holder, int position) {
// 产生随机高度
ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
layoutParams.height=heightList.get(position);
holder.itemView.setLayoutParams(layoutParams);
String s = list.get(position);
holder.tvName.setText(s);
holder.position =position;
}