1、概述ide
RecyclerView能夠當作是ListView的升級版。RecyclerView比ListView更加靈活,可是使用起來也更加麻煩。動畫
2、使用 this
一、RecyclerView.setLayoutManager,設置整個RecyclerView的顯示樣式。Android提供了幾個已經實現好的樣式,LinearLayoutManager、GridLayoutManager、StaggeredGridLayoutManager。能夠直接使用。spa
二、RecyclerView.setItemAnimator,設置增長、刪除Item時候的動畫效果。不設置的話,使用DefaultItemAnimator。code
三、RecyclerView.setItemDecoration,一般用於設置Item之間的間隔符。blog
四、RecyclerView.setAdapter,設置RecyclerView對應的數據,及每條數據的展示。這個與ListView相似。可是RecyclerView對應點Adapter須要繼承RecyclerView.Adapter<VH extends ViewHolder>,也就是說RecyclerView強制用戶使用ViewHolder,這樣效率更高。繼承
五、RecyclerView設置Item被點擊時的事件。在Adapter中寫一個事件相關的接口,在外面添加處理的方法。在Adapter中添加刪除、修改、添加的方法,並在方法中調用notifyItem*,我這裏只添加了add的方法。接口
3、代碼事件
一、整體代碼get
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); recyclerView = new RecyclerView(this); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.addItemDecoration(new RecyclerItemDecoration()); recyclerView.setLayoutParams(params); final RecyclerAdapter adapter = new RecyclerAdapter(infos , this); adapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(View view) { if (view instanceof TextView) { Toast.makeText(RecyclerViewActivity.this, ((TextView)view).getText().toString() , Toast.LENGTH_SHORT).show(); } } }); recyclerView.setAdapter(adapter); layout.addView(recyclerView); Button btnAdd = new Button(this); btnAdd.setText("Add"); layout.addView(btnAdd); btnAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { adapter.addItem("" + count, count); count++; } }); }
二、ItemDecorator,重寫getItemOffsets,onDraw,onDrawOver方法。
public class RecyclerItemDecoration extends ItemDecoration { private int orientation; private ShapeDrawable shapeDrawable; private int shapeWidth = 5; private int shapeHeight = 5; public RecyclerItemDecoration() { this.orientation = RecyclerView.VERTICAL; initShape(); } public RecyclerItemDecoration(int orientation) { if (orientation == RecyclerView.HORIZONTAL) { this.orientation = RecyclerView.HORIZONTAL; } else { this.orientation = RecyclerView.VERTICAL; } initShape(); } private void initShape() { shapeDrawable = new ShapeDrawable(); shapeDrawable.getPaint().setColor(Color.RED); } @Override public void onDraw(Canvas c, RecyclerView parent, State state) { if (orientation == RecyclerView.HORIZONTAL) { drawHorizontal(c, parent, state); } else { drawVertical(c, parent, state); } } private void drawVertical(Canvas c, RecyclerView parent, State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); int count = parent.getChildCount(); for(int index = 0; index < count; index++) { View child = parent.getChildAt(index); RecyclerView.LayoutParams params = (LayoutParams) child.getLayoutParams(); int top = child.getBottom() + params.bottomMargin; int bottom = top + shapeHeight; shapeDrawable.setBounds(left, top, right, bottom); shapeDrawable.draw(c); } } private void drawHorizontal(Canvas c, RecyclerView parent, State state) { // TODO Auto-generated method stub } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) { if (orientation == RecyclerView.HORIZONTAL) { outRect.set(0, 0, shapeWidth, 0); } else { outRect.set(0, 0, 0, shapeHeight); } } }
其中,onDraw和onDrawOver的區別,onDraw繪製在最底層、Item繪製在中間層、onDrawOver繪製在最上層。
三、Adapter
public class RecyclerAdapter extends Adapter<RecyclerViewHolder> { public interface OnItemClickListener { void onItemClick(View view); } private OnItemClickListener itemClickListener = null; private static final String RecyclerViewTag = "RecyclerViewTag"; private String[] infos = null; private Context context = null; public RecyclerAdapter(String[] infos, Context context) { this.infos = infos; this.context = context; } @Override public int getItemCount() { return infos.length; } @Override public void onBindViewHolder(final RecyclerViewHolder holder, final int i) { holder.bindView(infos[i]); Log.i(RecyclerViewTag, ">>>>>>>>>>onBindViewHolder:>>>" + i); } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup viewgroup, int i) { LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.recycler_item, null); final RecyclerViewHolder holder = new RecyclerViewHolder(view); holder.textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i(RecyclerViewTag, "Clicked"); if (itemClickListener != null) { itemClickListener.onItemClick(holder.textView); } } }); return holder; } public void addItem(String info, int position) { String[] newInfos = new String[infos.length + 1]; for (int i = 0; i < position; i++) { newInfos[i] = infos[i]; } newInfos[position] = info; for (int i = position; i < infos.length; i++) { newInfos[i + 1] = infos[i]; } infos = newInfos; notifyItemInserted(position); } public void setOnItemClickListener(OnItemClickListener listener) { itemClickListener = listener; } static class RecyclerViewHolder extends ViewHolder { public LinearLayout layout; public TextView textView; public RecyclerViewHolder(View arg0) { super(arg0); layout = (LinearLayout) arg0; textView = (TextView) layout.findViewById(R.id.tv_recycler_item); textView.setHeight(100); textView.setWidth(300); } public void bindView(String info) { textView.setText(info); } } }