Android RecyclerView基本使用详解
作者:a13069730106 时间:2023-07-24 21:13:30
什么是RecyclerView
关于RecyclerView,是一个主要用于展示和回收View的有一个控件,在官用了一句话来概括
RecyclerView 是一种通过提供有限的数据项窗口有效显示大数据集的视图。
基提供几个基本的功能,
控制其显示的方式 ———>LayoutManager—(必需)
控制Item间的间隔 ———>ItemDecoration—-(非必需)
控制Item增删的动画——->ItemAnimator——(非必需)
以上就可以完成ListView大部的功能了,但是确缺少了一个很常用的功能,就是Item的点需事件。当然,这个并不是什么问题,上文也说了,RecyclerView是一个用于控制view展示的回收的,其它的都由用户去定制。给了用户当的自由度。下面来看一些基本的使方式。
基本使用
由于RecyclerView并不是在Android的基本库中,所以如果开发者需要使用RecyclerView里,需要gradle引用对应的扩展包,
compile 'com.android.support:recyclerview-v7:25.0.1'
这后实大家需要做的和使用ListView差别也不大,只需要调置LayoutManager和Adapter就可以完成最基础的显示了。Adapter及Adapter需要的ViewHolder均继承自RecyvlerView的内部类。
RecyclerView的Adapter
public class DemoAdapter extends RecyclerView.Adapter<DemoViewHolder> {
private List<String> data;
public DemoAdapter(List<String> data) {
this.data = data;
}
@Override
public DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
DemoViewHolder holder = new DemoViewHolder(LayoutInflater.from(
parent.getContext()).inflate(R.layout.demo_recycler_view_item, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(DemoViewHolder holder, int position) {
holder.getView().setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
}
RecyclerView的ViewHolder
public class DemoViewHolder extends RecyclerView.ViewHolder {
private TextView view;
public DemoViewHolder(View itemView) {
super(itemView);
view = (TextView) itemView.findViewById(R.id.item_view);
}
public TextView getView() {
return view;
}
}
项目主Activity
public class RecyclerViewDemoActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view_demo);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
String[] datas = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
List<String> strings = Arrays.asList(datas);
DemoAdapter adapter = new DemoAdapter(strings);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
activity layout文件布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_recycler_view_demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yjeeq.study.recyclerview.RecyclerViewDemoActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
item layout文件布局
<?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="40dp"
android:orientation="vertical">
<TextView
android:id="@+id/item_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark"
android:gravity="center" />
</LinearLayout>
以下是运行效果
上面已经完成基本的展示,这里就介绍一下怎么为Recycler加上点击事件。其实思路很简单,就是在RecyclerView上的addOnItemTouchListener上做文章。这里给出具体实现。相信大家一看就懂。
* OnItemTouchListener的实现*
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private View childView;
private RecyclerView touchView;
public RecyclerItemClickListener(Context context, final OnItemClickListener mListener) {
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onItemClick(childView, touchView.getChildPosition(childView));
}
return true;
}
@Override
public void onLongPress(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onLongClick(childView, touchView.getChildAdapterPosition(childView));
}
}
});
}
GestureDetector mGestureDetector;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongClick(View view, int posotion);
}
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
mGestureDetector.onTouchEvent(motionEvent);
childView = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
touchView = recyclerView;
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
使用方式
首先在主类继承 RecyclerItemClickListener.OnItemClickListener,之后在主类中添加如下方法。
private void setOnItemClick() {
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, this));
}
public void onItemClick(View view, int position) {
Log.d("XXXXXX", "onItemClick : postion " + position);
}
@Override
public void onLongClick(View view, int posotion) {
Log.d("XXXXXX", "onLongClick position : " + posotion);
}