RecyclerView是Android 5.0新增的控件,在android-support-v7下面。官方文檔對RecycleView介紹很簡潔到位,如下:
成都網站建設、成都做網站,成都做網站公司-創(chuàng)新互聯已向上千企業(yè)提供了,網站設計,網站制作,網絡營銷等服務!設計與技術結合,多年網站推廣經驗,合理的價格為您打造企業(yè)品質網站。
A flexible view for providing a limited window into a large data set.
大概意思就是說:在有限大小的窗口里顯示大量數據的一個靈活的view。
下面是ReccleView繼承圖:
看到這里我們自然想到了與之類似的控件ListView,RecyclerView和ListView都是在有限的屏幕區(qū)域里維護少量的View來進行顯示大量的數據,其實RecyclerView是ListView的升級版,功能更加強大,更加靈活,擴展性更強,Google建議使用RecycleView替代ListView。
下面是RecycleView結構圖:
RecycleView主要包括以下幾個部件:
1. RecyclerView.Adapter
數據和視圖的分離設計是程序解耦和可維護的重要指標,RecycleView作為展示大量數據的view肯定是遵循這個設計的,ListView也不例外。通過繼承RecycleView.Adapter即可輕松實現自己的adapter,主要重寫RecycleView.Adapter的下面三個方法即可實現:
public MyRecycleViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) public void onBindViewHolder(MyRecycleViewAdapter.MyViewHolder holder, int position) public int getItemCount()
上面MyRecycleViewAdapter是我自己定義的ViewHolder
2. ViewHolder
ViewHolder用來保存列表中item view引用的類,這樣做的目的是重用item視圖,從而提高性能。
在ListView中,ViewHolder不是ListView自帶的,是需要自己定義的,當然你也可以不使用ViewHolder,這樣的后果就是ListView每次調用getView()方法的時候都會調用findViewById()方法,要知道findViewById()性能很差,它通過遞歸的方式去從view樹中找特定的子view的,最終往往帶來很差的性能體驗,所以使用ViewHolder也成了優(yōu)化ListView最重要的手段。
在RecyclerView中,Android內置了RecyclerView.ViewHolder,這意味著ViewHolder使用成了必須。
3. 分割線 RecycleView.ItemDecoration
ListView添加分割線很簡單,只需在ListView的xml中配置”divider”屬性即可,如:
android:dividerHeight="2dp" android:divider="@color/red"
但RecycleView添加分割線是比較麻煩的,并且默認是不帶分割線的。但實際開發(fā)中一般都需要分割線,使用RecycleView.ItemDecoration為RecycleView添加分割線
4. 布局管理器 LayoutManager
RecycleView支持多種布局模式:
1、LinearLayoutManager
線性布局管理器,支持水平布局和垂直布局兩種
2、GridLayoutManager
網格布局管理器
3、StaggerdGridLayoutManager
錯列的網格布局管理器,可以實現瀑布流列表
RecycleView使用時和ListView很類似,直接看一個簡單的例子,分別展示一個垂直方向和水平方向上的列表,并且都支持使用顏色和圖片做分割線,效果圖如下:
1. MainActivity
package com.cjl.recycleviewtest; ... public class MainActivity extends AppCompatActivity { private VerticalFragment mVerticalFragment; private HorizontalFragment mHorizontalFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //垂直 mVerticalFragment = new VerticalFragment(); //水平 mHorizontalFragment = new HorizontalFragment(); //默認為設置垂直 getFragmentManager().beginTransaction().replace(R.id.activity_main, mVerticalFragment).commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_recycleview, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { //垂直方向 case R.id.vertical: VerticalFragment mVerticalFragment = new VerticalFragment(); getFragmentManager().beginTransaction().replace(R.id.activity_main, mVerticalFragment).commit(); break; //水平方向 case R.id.horizontal: getFragmentManager().beginTransaction().replace(R.id.activity_main, mHorizontalFragment).commit(); break; } return super.onOptionsItemSelected(item); } }
MainActivity布局文件 R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
2. 垂直方向上 VerticalFragment
public class VerticalFragment extends Fragment implements View.OnClickListener{ private RecyclerView mRecycleViewDrawable; private RecyclerView mRecycleViewColor; private LinearLayoutManager mManagerColor; private LinearLayoutManager mManagerDrawable; private ListmData; private Button mDrawable; private Button mColor; private MyRecycleViewAdapter mRecycleViewAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_vertical_layout, container, false); mRecycleViewDrawable = (RecyclerView) view.findViewById(R.id.recycleview_drawable); mRecycleViewColor = (RecyclerView) view.findViewById(R.id.recycleview_color); mDrawable = (Button) view.findViewById(R.id.btn_drawable); mDrawable.setOnClickListener(this); mColor = (Button) view.findViewById(R.id.btn_color); mColor.setOnClickListener(this); //1. 顏色分割線 mManagerColor = new LinearLayoutManager(getActivity()); mManagerColor.setOrientation(LinearLayoutManager.VERTICAL); mRecycleViewColor.setLayoutManager(mManagerColor); //設置顏色分割線 mRecycleViewColor.addItemDecoration(new LinearDivider(getActivity(), LinearLayoutManager.VERTICAL, 10, this.getResources().getColor(R.color.colorAccent))); //2. 圖片分割線 mManagerDrawable = new LinearLayoutManager(getActivity()); mManagerDrawable.setOrientation(LinearLayoutManager.VERTICAL); mRecycleViewDrawable.setLayoutManager(mManagerDrawable); //設置圖片分割線 Drawable drawable = ContextCompat.getDrawable(getActivity(), R.mipmap.divider); mRecycleViewDrawable.addItemDecoration(new LinearDivider(getActivity(), LinearLayoutManager.VERTICAL, 20, drawable)); mData = new ArrayList (); initData(mData); //自定義adapter mRecycleViewAdapter = new MyRecycleViewAdapter(getActivity(), R.layout.item_vertical_recycleview, mData); //設置adapter mRecycleViewColor.setAdapter(mRecycleViewAdapter); mRecycleViewDrawable.setAdapter(mRecycleViewAdapter); return view; } private void initData(List dataList) { for (int i = 0; i < 30; i++) { dataList.add("item" + i); } } @Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.btn_drawable: mRecycleViewColor.setVisibility(View.INVISIBLE); mRecycleViewDrawable.setVisibility(View.VISIBLE); break; case R.id.btn_color: mRecycleViewColor.setVisibility(View.VISIBLE); mRecycleViewDrawable.setVisibility(View.INVISIBLE); break; } } }
VerticalFragment 的布局文件 R.layout.fragment_vertical_layout
<?xml version="1.0" encoding="utf-8"?>
3. 直接繼承RecyclerView.Adapter
public class MyRecycleViewAdapter extends RecyclerView.Adapter{ private LayoutInflater mLayoutInflater; private List mDataList; private int mItemLayout; public MyRecycleViewAdapter(Context context, int itemLayout, List datalist) { mLayoutInflater = LayoutInflater.from(context); mItemLayout = itemLayout; mDataList = datalist; } @Override public MyRecycleViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyViewHolder(mLayoutInflater.inflate(mItemLayout, parent, false)); } @Override public void onBindViewHolder(MyRecycleViewAdapter.MyViewHolder holder, int position) { holder.mTextView.setText(mDataList.get(position)); } @Override public int getItemCount() { return mDataList.size(); } class MyViewHolder extends RecyclerView.ViewHolder { private TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView.findViewById(R.id.tv); } } }
item布局文件 item_vertical_recycleview.xml
<?xml version="1.0" encoding="utf-8"?>
4. 分割線 RecyclerView.ItemDecoration
public class LinearDivider extends RecyclerView.ItemDecoration { private Drawable mDividerDarwable; private int mOrientation; private int mDividerHight = 1; private Paint mColorPaint; public final int[] ATRRS = new int[]{ android.R.attr.listDivider }; /* orientation 方向 */ public LinearDivider(Context context, int orientation) { final TypedArray ta = context.obtainStyledAttributes(ATRRS); this.mDividerDarwable = ta.getDrawable(0); ta.recycle(); setOrientation(orientation); } /* int orientation 方向 int dividerHight 分割線的線寬 Drawable dividerDrawable 充當分割線的圖片 */ public LinearDivider(Context context, int orientation, int dividerHight, Drawable dividerDrawable) { this(context, orientation); mDividerHight = dividerHight; mDividerDarwable = dividerDrawable; } /* int orientation 方向 int dividerHight 分割線的線寬 int dividerColor 分割線的顏色 */ public LinearDivider(Context context, int orientation, int dividerHight, int dividerColor) { this(context, orientation); mDividerHight = dividerHight; mColorPaint = new Paint(); mColorPaint.setColor(dividerColor); } public void setOrientation(int orientation) { if (orientation != LinearLayoutManager.HORIZONTAL && orientation != LinearLayoutManager.VERTICAL) { throw new IllegalArgumentException("方向參數錯誤!"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); if (mOrientation == LinearLayoutManager.HORIZONTAL) { drawHorizontalDivider(c, parent); } else { drawVirticalDivider(c, parent); } } //畫垂直分割線 public void drawVirticalDivider(Canvas c, RecyclerView parent) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDividerHight; if (mDividerDarwable != null) { mDividerDarwable.setBounds(left, top, right, bottom); mDividerDarwable.draw(c); } if (mColorPaint != null) { c.drawRect(left, top, right, bottom, mColorPaint); } } } //畫水平分割線 public void drawHorizontalDivider(Canvas c, RecyclerView parent) { int top = parent.getPaddingTop(); int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mDividerHight; if (mDividerDarwable != null) { mDividerDarwable.setBounds(left, top, right, bottom); mDividerDarwable.draw(c); } if (mColorPaint != null) { c.drawRect(left, top, right, bottom, mColorPaint); } } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); if (mOrientation == LinearLayoutManager.HORIZONTAL) { outRect.set(0, 0, 0, mDividerHight); } else { outRect.set(0, 0, mDividerHight, 0); } } }
未完待續(xù)……
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯。