RecyclerView出来很长时间了,一直没有去尝试学习,想想很是忏愧!今天整理了一点关于RecyclerView的简单使用方法,算作学习笔记了。
RecyclerView的使用,有三个是必须的Adapter,ViewHolder,LayoutManager,而ItemDecoration和ItemAnimator是可选的。
以下是简单的代码示例:
首先是VIewHoder的:
class TestRecycleHolder extends RecyclerView.ViewHolder{
protected TextView textView;
public TestRecycleHolder(View itemView) {
super(itemView);
textView= (TextView) itemView.findViewById(R.id.item_text);
}
}
该类必需继承extends RecyclerView.ViewHolder。
适配器Adapter部分:
class RecycleAdapter extends RecyclerView.Adapter<TestRecycleHolder>
{
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public TestRecycleHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view= LayoutInflater.from(context).inflate(R.layout.item_view,null);
return new TestRecycleHolder(view);
}
@Override
public void onBindViewHolder(TestRecycleHolder holder, int position)
{
final String name=dataArrayList.get(position);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You clickd TextView at index of "+name,
Toast.LENGTH_SHORT).show();
}
});
holder.textView.setText(name);
}
@Override
public int getItemCount()
{
return dataArrayList.size();
}
}
该类必需继承RecyclerView.Adapter类。
至于LayoutManager,可以使用系统提供的LinearLayoutManager类,代码如下:
LinearLayoutManager layoutManager = new LinearLayoutManager(this,
LinearLayout.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
也可以自己继承RecyclerView.LayoutManager的类,实现该类
class MyDefineLayoutManager extends RecyclerView.LayoutManager {
private final MyDefineLayoutManager myff=this;
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)
{
super.onLayoutChildren(recycler, state);
if (state.isPreLayout())
{
return;
}
if (getItemCount() == 0)
return;
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams()
{
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
}
如果想知道如何写自己的LayoutManager,请参阅http://wiresareobsolete.com/2014/09/building-a-recyclerview-layoutmanager-part-1/文章
ItemDecoration,是对Item进行装饰的,比如给Item加个边距等。比如,下面的代码仅仅加了个上边距:
class SpacesItemDecoration extends RecyclerView.ItemDecoration{
public SpacesItemDecoration() {
super();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.top=10;
}
}
文章写得比较粗糙,仅做为一个笔记参考。敬请见谅
参考文献:http://www.tuicool.com/articles/URvIB32
https://www.javacodegeeks.com/2014/11/a-guide-to-android-recyclerview-and-cardview.html
http://www.cnblogs.com/littlepanpc/p/3859983.html
https://github.com/vilyever/HowToCustomLayoutManager
http://www.jianshu.com/p/de31cce8e62f
http://wiresareobsolete.com/2014/09/building-a-recyclerview-layoutmanager-part-1/