public abstract class RefreshAdapter extends RecyclerView.Adapter {
protected Context mContext;
protected List<T> mList;
protected LayoutInflater mInflater;
protected RecyclerView mRecyclerView;
protected OnItemClickListener<T> mOnItemClickListener;
public RefreshAdapter(Context context) {
this(context, new ArrayList<T>());
}
public RefreshAdapter(Context context, List<T> list) {
mList = list;
mContext = context;
mInflater = LayoutInflater.from(mContext);
setHasStableIds(true);
}
@Override
public int getItemCount() {
if (mList != null) {
return mList.size();
}
return 0;
}
public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
}
public RecyclerView getRecyclerView() {
return mRecyclerView;
}
public void setList(List<T> list) {
if (mList != null) {
mList.clear();
mList.addAll(list);
}
}
public void refreshData(List<T> list) {
if (mRecyclerView != null && list != null) {
mList.clear();
mList.addAll(list);
notifyDataSetChanged();
}
}
public void insertList(List<T> list) {
if (mRecyclerView != null && mList != null && list != null && list.size() > 0) {
int p = mList.size();
mList.addAll(list);
notifyItemRangeInserted(p, list.size());
}
}
public void clearData() {
if (mRecyclerView != null && mList != null) {
mList.clear();
notifyDataSetChanged();
}
}
@Override
public long getItemId(int position) {
return position;
}
protected boolean canClick() {
return ClickUtil.canClick();
}
public List<T> getList() {
return mList;
}
}
/**
- Created by cxf on 2017/8/9.
- RecyclerView的Adapter点击事件
*/
public interface OnItemClickListener {
void onItemClick(T bean, int position);
}
/**
- RecyclerView分割线
*/
public class ItemDecoration extends RecyclerView.ItemDecoration {
private static final String TAG = “ItemDecoration”;
private Drawable mDivider;
private int dividerHeight;
private int dividerWidth;
private int dividerColor;
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
/**
* 设置是否显示左右边界线
/
private boolean drawBorderLeftAndRight = false;
/*
* 设置是否显示上下边界线
*/
private boolean drawBorderTopAndBottom = false;
/**
* 是否只留空白,不画分割线
*/
private boolean onlySetItemOffsetsButNoDraw=false;
/**
* 是否是线性布局
*/
private boolean isLinearLayoutManager=true;
/**
* 布局方向
*/
private int orientation=VERTICAL_LIST;
public ItemDecoration(Context context) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
this.dividerHeight=mDivider.getIntrinsicHeight();
this.dividerWidth=mDivider.getIntrinsicWidth();
}
/**
* 自定义分割线
*
* @param context
* @param drawableId 分割线图片
*/
public ItemDecoration(Context context, @DrawableRes int drawableId) {
mDivider = ContextCompat.getDrawable(context, drawableId);
this.dividerHeight=mDivider.getIntrinsicHeight();
this.dividerWidth=mDivider.getIntrinsicWidth();
}
/**
* 自定义分割线
* 也可以使用{@link Canvas#drawRect(float, float, float, float, Paint)}或者{@link Canvas#drawText(String, float, float, Paint)}等等
* 结合{@link Paint}去绘制各式各样的分割线
* @param context
* @param color 整型颜色值,非资源id
* @param dividerWidth 单位为dp
* @param dividerHeight 单位为dp
*/
public ItemDecoration(Context context, @ColorInt int color, @Dimension float dividerWidth, @Dimension float dividerHeight) {
mDivider = new ColorDrawable(color);
this.dividerWidth= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dividerWidth,context.getResources().getDisplayMetrics());
this.dividerHeight= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dividerHeight,context.getResources().getDisplayMetrics());
}
/**
* 垂直滚动,item宽度充满,高度自适应
* 水平滚动,item高度充满,宽度自适应
* 在itemView绘制完成之前调用,也就是说此方法draw出来的效果将会在itemView的下面
* onDrawOver方法draw出来的效果将叠加在itemView的上面
* @param c
* @param parent
* @param state
*/
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if(onlySetItemOffsetsButNoDraw){
return;
}
if(isLinearLayoutManager){
drawLinearItemDivider(c,parent);
}else{
drawHorizontalLine(c, parent);
drawVerticalLine(c, parent);
}
}
private void drawLinearItemDivider(Canvas c, RecyclerView parent){
int spanCount = getSpanCount(parent);
// int allChildCount = parent.getAdapter().getItemCount();
int top=0,bottom=0,left=0,right=0;
for (int i = 0; i < parent.getChildCount(); i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
if(orientationVERTICAL_LIST){//画横线
left = child.getLeft() - params.leftMargin;
right = child.getRight() + params.rightMargin;
if(drawBorderTopAndBottom){
//加上第一条
if(isFirstRaw(parent,params.getViewLayoutPosition(),spanCount)){
top=child.getTop()-params.topMargin-dividerHeight;
bottom = top + dividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw©;
}
}else{
// if(isLastRaw(parent,params.getViewLayoutPosition(),spanCount,allChildCount)){
// continue;
// }
}
top = child.getBottom() + params.bottomMargin;
bottom = top + dividerHeight;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw©;
}else{//画竖线
top=child.getTop()-params.topMargin;
bottom=child.getBottom()+params.bottomMargin;
if(drawBorderLeftAndRight){
//加上第一条
if(isFirstColumn(parent,params.getViewLayoutPosition(),spanCount)){
left=child.getLeft()-params.leftMargin-dividerWidth;
right = left + dividerWidth;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw©;
}
}else{
// if(isLastColum(parent,params.getViewLayoutPosition(),spanCount,allChildCount)){
// continue;
// }
}
left = child.getRight() + params.rightMargin;
right = left + dividerWidth;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw©;
}
}
if(orientationVERTICAL_LIST){
if(drawBorderLeftAndRight){
top = parent.getPaddingTop();
bottom = parent.getHeight() - parent.getPaddingBottom();
left=parent.getPaddingLeft();
right=left+dividerWidth;
//画左边界
mDivider.setBounds(left,top,right,bottom);
mDivider.draw©;
left=parent.getWidth()-parent.getPaddingRight()-dividerWidth;
ri