recyclerView 的万能适配器
2016-09-05 00:12
381 查看
activity
BaseRecyclerAdapter
BaseRecyclerHolder
item 分割线
这里写代码片package com.example.recyclerview; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; List<use> uses = new ArrayList<>(); RecyclerView.Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.recyView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); initData(); recyclerView.setAdapter(adapter=new RecyclerAdapter<use>(MainActivity.this, uses, R.layout.item_recycler) { @Override public void convert(RecycleHolder holder, use muse, int position) { holder.setText(R.id.name, muse.getName()); holder.setText(R.id.age, muse.getAge()); } }); } private void initData() { for (int i = 0; i <30 ; i++) { use myUse=new use(); myUse.setName("小王"); myUse.setAge("12"); uses.add(myUse); } } }
BaseRecyclerAdapter
package com.example.recyclerview; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * 万能的RecyclerView适配器 * Created by 南尘 on 16-7-30. */ public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> { private Context context;//上下文 private List<T> list;//数据源 private LayoutInflater inflater;//布局器 private int itemLayoutId;//布局id private boolean isScrolling;//是否在滚动 private OnItemClickListener listener;//点击事件监听器 private OnItemLongClickListener longClickListener;//长按监听器 private RecyclerView recyclerView; //在RecyclerView提供数据的时候调用 @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); this.recyclerView = recyclerView; } @Override public void onDetachedFromRecyclerView(RecyclerView recyclerView) { super.onDetachedFromRecyclerView(recyclerView); this.recyclerView = null; } /** * 定义一个点击事件接口回调 */ public interface OnItemClickListener { void onItemClick(RecyclerView parent, View view, int position); } public interface OnItemLongClickListener { boolean onItemLongClick(RecyclerView parent, View view, int position); } /** * 插入一项 * * @param item * @param position */ public void insert(T item, int position) { list.add(position, item); notifyItemInserted(position); } /** * 删除一项 * * @param position 删除位置 */ public void delete(int position) { list.remove(position); notifyItemRemoved(position); } public BaseRecyclerAdapter(Context context, List<T> list, int itemLayoutId) { this.context = context; this.list = list; this.itemLayoutId = itemLayoutId; inflater = LayoutInflater.from(context); // recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { // @Override // public void onScrollStateChanged(RecyclerView recyclerView, int newState) { // super.onScrollStateChanged(recyclerView, newState); // isScrolling = !(newState == RecyclerView.SCROLL_STATE_IDLE); // if (!isScrolling) { // notifyDataSetChanged(); // } // } // }); } @Override public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(itemLayoutId, parent, false); return BaseRecyclerHolder.getRecyclerHolder(context, view); } @Override public void onBindViewHolder(final BaseRecyclerHolder holder, int position) { if (listener != null){ // holder.itemView.setBackgroundResource(R.drawable.recycler_bg);//设置背景 } holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (listener != null && view != null && recyclerView != null) { int position = recyclerView.getChildAdapterPosition(view); listener.onItemClick(recyclerView, view, position); } } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { if (longClickListener != null && view != null && recyclerView != null) { int position = recyclerView.getChildAdapterPosition(view); longClickListener.onItemLongClick(recyclerView, view, position); return true; } return false; } }); convert(holder, list.get(position), position, isScrolling); } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; } public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) { this.longClickListener = longClickListener; } /** * 填充RecyclerView适配器的方法,子类需要重写 * * @param holder ViewHolder * @param item 子项 * @param position 位置 * @param isScrolling 是否在滑动 */ public abstract void convert(BaseRecyclerHolder holder, T item, int position, boolean isScrolling); }
BaseRecyclerHolder
package com.example.recyclerview; import android.content.Context; import android.graphics.Bitmap; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.View; import android.widget.ImageView; import android.widget.TextView; // import com.squareup.picasso.Picasso; /** * 万能的RecyclerView的ViewHolder * Created by 南尘 on 16-7-30. */ public class BaseRecyclerHolder extends RecyclerView.ViewHolder { private SparseArray<View> views; private Context context; private BaseRecyclerHolder(Context context,View itemView) { super(itemView); this.context = context; //指定一个初始为8 views = new SparseArray<>(8); } /** * 取得一个RecyclerHolder对象 * @param context 上下文 * @param itemView 子项 * @return 返回一个RecyclerHolder对象 */ public static BaseRecyclerHolder getRecyclerHolder(Context context,View itemView){ return new BaseRecyclerHolder(context,itemView); } public SparseArray<View> getViews(){ return this.views; } /** * 通过view的id获取对应的控件,如果没有则加入views中 * @param viewId 控件的id * @return 返回一个控件 */ @SuppressWarnings("unchecked") public <T extends View> T getView(int viewId){ View view = views.get(viewId); if (view == null ){ view = itemView.findViewById(viewId); views.put(viewId,view); } return (T) view; } /** * 设置字符串 */ public BaseRecyclerHolder setText(int viewId,String text){ TextView tv = getView(viewId); tv.setText(text); return this; } /** * 设置图片 */ public BaseRecyclerHolder setImageResource(int viewId,int drawableId){ ImageView iv = getView(viewId); iv.setImageResource(drawableId); return this; } /** * 设置图片 */ public BaseRecyclerHolder setImageBitmap(int viewId, Bitmap bitmap){ ImageView iv = getView(viewId); iv.setImageBitmap(bitmap); return this; } /* *//** * 设置图片 *//* public BaseRecyclerHolder setImageByUrl(int viewId,String url){ Picasso.with(context).load(url).into((ImageView) getView(viewId)); // ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context)); // ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId)); return this; }*/ }
item 分割线
package com.example.recyclerview; import android.support.v7.widget.RecyclerView; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.TypedValue; import android.view.View; /** * Created by Administrator on 2016/9/4. */ public class DividerItemDecoration extends RecyclerView.ItemDecoration{ /* * RecyclerView的布局方向,默认先赋值 * 为纵向布局 * RecyclerView 布局可横向,也可纵向 * 横向和纵向对应的分割想画法不一样 * */ private int mOrientation = LinearLayoutManager.VERTICAL ; /** * item之间分割线的size,默认为1 */ private int mItemSize = 1 ; /** * 绘制item分割线的画笔,和设置其属性 * 来绘制个性分割线 */ private Paint mPaint ; /** * 构造方法传入布局方向,不可不传 * @param context * @param orientation */ public DividerItemDecoration(Context context,int orientation) { this.mOrientation = orientation; if(orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL){ throw new IllegalArgumentException("请传入正确的参数") ; } mItemSize = (int) TypedValue.applyDimension(mItemSize, TypedValue.COMPLEX_UNIT_DIP,context.getResources().getDisplayMetrics()); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ; mPaint.setColor(Color.BLUE); /*设置填充*/ mPaint.setStyle(Paint.Style.FILL); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { if(mOrientation == LinearLayoutManager.VERTICAL){ drawVertical(c,parent) ; }else { drawHorizontal(c,parent) ; } } /** * 绘制纵向 item 分割线 * @param canvas * @param parent */ private void drawVertical(Canvas canvas,RecyclerView parent){ final int left = parent.getPaddingLeft() ; final int right = parent.getMeasuredWidth() - parent.getPaddingRight() ; final int childSize = parent.getChildCount() ; for(int i = 0 ; i < childSize ; i ++){ final View child = parent.getChildAt( i ) ; RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getBottom() + layoutParams.bottomMargin ; final int bottom = top + mItemSize ; canvas.drawRect(left,top,right,bottom,mPaint); } } /** * 绘制横向 item 分割线 * @param canvas * @param parent */ private void drawHorizontal(Canvas canvas,RecyclerView parent){ final int top = parent.getPaddingTop() ; final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom() ; final int childSize = parent.getChildCount() ; for(int i = 0 ; i < childSize ; i ++){ final View child = parent.getChildAt( i ) ; RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + layoutParams.rightMargin ; final int right = left + mItemSize ; canvas.drawRect(left,top,right,bottom,mPaint); } } /** * 设置item分割线的size * @param outRect * @param view * @param parent * @param state */ @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if(mOrientation == LinearLayoutManager.VERTICAL){ outRect.set(0,0,0,mItemSize); }else { outRect.set(0,0,mItemSize,0); } } }
相关文章推荐
- 万能RecyclerView适配器
- RecyclerView万能适配器,点击事件,分割线,间距等写法
- Android(无需javabean)的RecyclerView万能适配器,解耦从这里做起
- RecyclerView (万能的适配器和实现列表项的拖动和左右滑动)
- 【工具类】打造万能的Listview、GridView和ViewPager和RecyclerView的适配器
- 偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder
- RecyclerView 万能适配器的抽取
- 打造android偷懒神器———RecyclerView的万能适配器
- 打造RecyclerView的万能适配器
- 开源框架BaseRecyclerViewAdapterHelper使用——RecyclerView万能适配器
- android 用mvp模式来架构自己的app+打造Recyclerview万能适配器
- RecyclerView的万能适配器+定义可以到底部自动刷新的RecyclerView
- Android 轻量级万能适配器,通吃所有的 AbsListView、RecyclerView
- 偷懒新姿势,打造属于RecyclerView的万能适配器Adapter和ViewHolder
- recyclerview万能适配器用法以及源码分析
- 安卓RecyclerView万能适配器之baserecyclerviewadapterhelper详解
- Android 开源框架BaseRecyclerViewAdapterHelper使用——RecyclerView万能适配器
- RecyclerView万能适配器加强版———可选择添加头和尾(含点击事件)
- 属于RecyclerView的万能适配器Adapter和ViewHolder
- PowerfulRecyclerViewAdapter:一种RecyclerView的万能适配器实现