标签:
1 /** 2 * 设置Adapter 3 */ 4 mRecyclerView.setAdapter(mListAdapter); 5 /** 6 * 设置布局管理器 7 */ 8 mRecyclerView.setLayoutManager(linearLayoutManager); 9 /** 10 * 设置item分割线 11 */ 12 mRecyclerView.addItemDecoration(itemDecoration); 13 /** 14 * 设置item动画 15 */ 16 mRecyclerView.setItemAnimator(new DefaultItemAnimator());
1 package com.bbk.lling.recyclerview; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.os.Bundle; 5 import android.support.v7.widget.DefaultItemAnimator; 6 import android.support.v7.widget.LinearLayoutManager; 7 import android.support.v7.widget.RecyclerView; 8 import android.view.Menu; 9 import android.view.MenuItem; 10 import android.view.View; 11 import android.widget.Toast; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 /** 17 * @Class: ListLayoutActivity 18 * @Description: RecycleView实现listview的功能 19 * @author: lling(www.liuling123.com) 20 * @Date: 2015/10/29 21 */ 22 public class ListLayoutActivity extends ActionBarActivity { 23 24 private RecyclerView mRecyclerView; 25 private ListAdapter mListAdapter; 26 private List<String> mDatas; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_list_layout); 32 initData(); 33 mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); 34 mListAdapter = new ListAdapter(this, mDatas); 35 mListAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() { 36 @Override 37 public void onItemClick(View view, int position) { 38 Toast.makeText(ListLayoutActivity.this, "Click" + mDatas.get(position), Toast.LENGTH_SHORT).show(); 39 } 40 41 @Override 42 public void onItemLongClick(View view, int position) { 43 mListAdapter.remove(position); //remove the item 44 Toast.makeText(ListLayoutActivity.this, "LongClick" + mDatas.get(position), Toast.LENGTH_SHORT).show(); 45 } 46 }); 47 mRecyclerView.setAdapter(mListAdapter); 48 /** 49 * 设置布局管理器,listview风格则设置为LinearLayoutManager 50 * gridview风格则设置为GridLayoutManager 51 * pu瀑布流风格的设置为StaggeredGridLayoutManager 52 */ 53 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 54 // 设置item分 55 mRecyclerView.addItemDecoration(new ListItemDecoration(this, LinearLayoutManager.VERTICAL)); 56 // 设置item动画 57 mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 58 59 } 60 61 @Override 62 public boolean onCreateOptionsMenu(Menu menu) { 63 getMenuInflater().inflate(R.menu.menu_list_layout, menu); 64 return true; 65 } 66 67 @Override 68 public boolean onOptionsItemSelected(MenuItem item) { 69 switch (item.getItemId()) { 70 case R.id.add_first: 71 mListAdapter.add(0, "add first"); 72 break; 73 case R.id.add_last: 74 mListAdapter.add(mListAdapter.getItemCount(), "add last"); 75 break; 76 case R.id.remove_first: 77 String value = mListAdapter.remove(0); 78 Toast.makeText(ListLayoutActivity.this, "remove:" + value, Toast.LENGTH_SHORT).show(); 79 break; 80 case R.id.remove_last: 81 String value1 = mListAdapter.remove(mListAdapter.getItemCount()-1); 82 Toast.makeText(ListLayoutActivity.this, "remove:" + value1, Toast.LENGTH_SHORT).show(); 83 break; 84 case R.id.horizontal: 85 mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 86 mRecyclerView.addItemDecoration(new ListItemDecoration(this, LinearLayoutManager.HORIZONTAL)); 87 break; 88 case R.id.vertical: 89 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 90 mRecyclerView.addItemDecoration(new ListItemDecoration(this, LinearLayoutManager.VERTICAL)); 91 break; 92 } 93 return super.onOptionsItemSelected(item); 94 } 95 96 /* ==========This Part is not necessary========= */ 97 98 /** 99 * Create datas 100 */ 101 protected void initData() { 102 mDatas = new ArrayList<String>(); 103 for (int i = 0; i < 100; i++) { 104 mDatas.add(String.valueOf(i)); 105 } 106 } 107 108 /* ==========This Part is not necessary========= */ 109 }
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <android.support.v7.widget.RecyclerView 7 android:id="@+id/recyclerview" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" /> 10 11 </RelativeLayout>
1 package com.bbk.lling.recyclerview; 2 3 import android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.support.v7.widget.RecyclerView; 6 import android.util.Log; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.TextView; 11 12 import java.util.List; 13 14 /** 15 * @Class: ListAdapter 16 * @Description: 数据适配器 17 * @author: lling(www.liuling123.com) 18 * @Date: 2015/10/29 19 */ 20 public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> { 21 22 private List<String> mDatas; 23 private LayoutInflater mInflater; 24 private OnItemClickListener mOnItemClickListener; 25 26 public ListAdapter(Context context, List<String> mDatas) { 27 this.mDatas = mDatas; 28 mInflater = LayoutInflater.from(context); 29 } 30 31 @Override 32 public int getItemCount() { 33 return mDatas.size(); 34 } 35 36 @SuppressLint("NewApi") 37 @Override 38 public void onBindViewHolder(final ItemViewHolder itemViewHolder, final int i) { 39 itemViewHolder.mTextView.setText(mDatas.get(i)); 40 if(mOnItemClickListener != null) { 41 /** 42 * 这里加了判断,itemViewHolder.itemView.hasOnClickListeners() 43 * 目的是减少对象的创建,如果已经为view设置了click监听事件,就不用重复设置了 44 * 不然每次调用onBindViewHolder方法,都会创建两个监听事件对象,增加了内存的开销 45 */ 46 if(!itemViewHolder.itemView.hasOnClickListeners()) { 47 Log.e("ListAdapter", "setOnClickListener"); 48 itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() { 49 @Override 50 public void onClick(View v) { 51 int pos = itemViewHolder.getPosition(); 52 mOnItemClickListener.onItemClick(v, pos); 53 } 54 }); 55 itemViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { 56 @Override 57 public boolean onLongClick(View v) { 58 int pos = itemViewHolder.getPosition(); 59 mOnItemClickListener.onItemLongClick(v, pos); 60 return true; 61 } 62 }); 63 } 64 } 65 } 66 67 @Override 68 public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 69 /** 70 * 使用RecyclerView,ViewHolder是可以复用的。这根使用ListView的VIewHolder复用是一样的 71 * ViewHolder创建的个数好像是可见item的个数+3 72 */ 73 Log.e("ListAdapter", "onCreateViewHolder"); 74 ItemViewHolder holder = new ItemViewHolder(mInflater.inflate( 75 R.layout.item_layout, viewGroup, false)); 76 return holder; 77 } 78 79 /** 80 * 向指定位置添加元素 81 * @param position 82 * @param value 83 */ 84 public void add(int position, String value) { 85 if(position > mDatas.size()) { 86 position = mDatas.size(); 87 } 88 if(position < 0) { 89 position = 0; 90 } 91 mDatas.add(position, value); 92 /** 93 * 使用notifyItemInserted/notifyItemRemoved会有动画效果 94 * 而使用notifyDataSetChanged()则没有 95 */ 96 notifyItemInserted(position); 97 } 98 99 /** 100 * 移除指定位置元素 101 * @param position 102 * @return 103 */ 104 public String remove(int position) { 105 if(position > mDatas.size()-1) { 106 return null; 107 } 108 String value = mDatas.remove(position); 109 notifyItemRemoved(position); 110 return value; 111 } 112 113 114 public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) { 115 this.mOnItemClickListener = mOnItemClickListener; 116 } 117 118 /** 119 * 处理item的点击事件和长按事件 120 */ 121 interface OnItemClickListener { 122 public void onItemClick(View view, int position); 123 public void onItemLongClick(View view, int position); 124 } 125 126 class ItemViewHolder extends RecyclerView.ViewHolder { 127 128 private TextView mTextView; 129 130 public ItemViewHolder(View itemView) { 131 super(itemView); 132 mTextView = (TextView) itemView.findViewById(R.id.textview); 133 } 134 } 135 136 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:id="@+id/textview" 7 android:layout_width="wrap_content" 8 android:layout_height="48dp" 9 android:minWidth="48dp" 10 android:gravity="center" 11 android:layout_centerInParent="true" 12 android:text="XXX"/> 13 </RelativeLayout>
1 package com.bbk.lling.recyclerview; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Rect; 6 import android.graphics.drawable.Drawable; 7 import android.support.v7.widget.LinearLayoutManager; 8 import android.support.v7.widget.RecyclerView; 9 import android.view.View; 10 11 /** 12 * @Class: ListItemDecoration 13 * @Description: listview的item分割线 14 * @author: lling(www.liuling123.com) 15 * @Date: 2015/10/29 16 */ 17 public class ListItemDecoration extends RecyclerView.ItemDecoration { 18 19 private Drawable mDrawable; 20 21 private final static int DEFAULT_ORENTATION = LinearLayoutManager.VERTICAL; 22 23 private int mOrientation; 24 25 public ListItemDecoration(Context context, int orientation) { 26 if(orientation != LinearLayoutManager.HORIZONTAL && orientation != LinearLayoutManager.VERTICAL) { 27 this.mOrientation = DEFAULT_ORENTATION; 28 } else { 29 this.mOrientation = orientation; 30 } 31 mDrawable = context.getResources().getDrawable(R.drawable.divider); 32 } 33 34 @Override 35 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 36 if(mOrientation == LinearLayoutManager.HORIZONTAL) { 37 drawHorizontal(c, parent); 38 } else { 39 drawVertical(c, parent); 40 } 41 } 42 43 private void drawHorizontal(Canvas c, RecyclerView parent) { 44 int top = parent.getPaddingTop(); 45 int bottom = parent.getHeight() - parent.getPaddingBottom(); 46 47 int childCount = parent.getChildCount(); 48 for (int i = 0; i < childCount; i++) { 49 View child = parent.getChildAt(i); 50 RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 51 .getLayoutParams(); 52 int left = child.getRight() + params.rightMargin; 53 int right = left + mDrawable.getIntrinsicHeight(); 54 mDrawable.setBounds(left, top, right, bottom); 55 mDrawable.draw(c); 56 } 57 } 58 59 private void drawVertical(Canvas c, RecyclerView parent) { 60 int left = parent.getPaddingLeft(); 61 int right = parent.getWidth() - parent.getPaddingRight(); 62 63 int childCount = parent.getChildCount(); 64 for (int i = 0; i < childCount; i++) { 65 View child = parent.getChildAt(i); 66 android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext()); 67 RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 68 .getLayoutParams(); 69 int top = child.getBottom() + params.bottomMargin; 70 int bottom = top + mDrawable.getIntrinsicHeight(); 71 mDrawable.setBounds(left, top, right, bottom); 72 mDrawable.draw(c); 73 } 74 } 75 76 @Override 77 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 78 super.getItemOffsets(outRect, view, parent, state); 79 } 80 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 3 <size android:height="1dp" android:width="1dp"/> 4 <!--<solid android:color="#e0e0e0"/>--> 5 <solid android:color="#ff0000"/> 6 </shape>
标签:
原文地址:http://www.cnblogs.com/liuling/p/2015-11-04-01.html