如何使用
1.build.gradle中添加该类
compile 'com.android.support:recyclerview-v7:28.0.0'
2.视图层中定义RecyclerView
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
3.绑定RecyclerView
RecyclerView rv = findViewById(R.id.rv);
RvAdapter rvAdapter = new RvAdapter(list);
rv.setAdapter(rvAdapter);
// rv.setLayoutManager(new GridLayoutManager(this,4));
rv.setLayoutManager(new
LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
RecyclerView中有两个必须的设置,setLayoutManager()和setAdapter.
LayoutManager有两种,GridLayoutManager,LinearLayoutManager.
GridLayoutManager使用了2个参数的构造;GridLayoutManager(Context context, int spanCount)
context :上下文
spancout: 每行显示的数量。
LinearLayoutManager使用了3个参数的构造,LinearLayoutManager(Context context, int orientation, boolean reverseLayout)。
context:上下文
orientation:方向,垂直还是水平
reverseLayout:是否倒置,true为倒置
定义Adapter之前创建子视图
activity_item
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/border"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/rv_text"
android:background="@drawable/border_blue"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
4.定义Adapter
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHolder> {
private List<String> list;
private MyClickListener myClickListener;
public void setMyClickListener(MyClickListener myClickListener) {
this.myClickListener=myClickListener;
}
public RvAdapter(List<String> list) {
this.list=list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//绑定子视图
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_item, viewGroup, false);
MyViewHolder myViewHolder=new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
TextView textView =myViewHolder.itemView.findViewById(R.id.rv_text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myClickListener.setTextClickListener(i);
}
});
textView.setText(list.get(i));
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myClickListener.setOnClickListener(i);
}
});
myViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
myClickListener.setOnLongClickListener(i);
return false;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
//定义视图管理器
class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(@NonNull View itemView) {
super(itemView);
}
}
//事件监听
public interface MyClickListener{
void setOnClickListener(int i);
void setOnLongClickListener(int i);
void setTextClickListener(int i);
}
}
Adapter中重写了三个方法
onCreateViewHolder:用来获取子视图控制器,MyViewHolder继承RecyclerView.ViewHolder类。
onBindViewHolder:用来获取子视图里的控件,第二个参数表示第几个item。
getItemCount():表示页面需要显示的item数量。
RecyclerView只有item的点击事件没有长按事件,使用事件监听给外部暴露长按事件。
rvAdapter.setMyClickListener(new RvAdapter.MyClickListener() {
@Override
public void setOnClickListener(int i) {
Toast.makeText(MainActivity.this,"点击了第 "+ i +"个",Toast.LENGTH_SHORT).show();
}
@Override
public void setOnLongClickListener(int i) {
Toast.makeText(MainActivity.this,"长按了第 "+ i +"个",Toast.LENGTH_SHORT).show();
}
@Override
public void setTextClickListener(int i) {
Toast.makeText(MainActivity.this,"点击了第 "+ i +"个text",Toast.LENGTH_SHORT).show();
}
});
5.效果