欢迎大家和我分享
欢迎转载,但请保留文章原始出处:
哈哩波特大:https://www.jianshu.com/u/b7d80b891802
- activity_main.xml
- 创建Adapter(根据需求创建不同的Adapter)
/*******************************************
* @author : 奥特曼
* @Email : [email protected]
* @Description : 适配器 示例类
*******************************************/
public class MeAdapter extends BaseQuickAdapter {
public MeAdapter(@LayoutRes int layoutResId, @Nullable List data) {
super(layoutResId, data);
}
public MeAdapter(@LayoutRes int layoutResId) {
super(layoutResId);
}
@Override
protected void convert(BaseViewHolder viewHolder, String item) {
//添加Item子控件的点击事件
helper.addOnClickListener(R.id.tv_name);
//赋值
viewHolder.setText(R.id.tv_me_title_name,"Hello:"+item);
ImageView iv_cover = viewHolder.getView(R.id.iv_cover);
iv_cover.setImageResource(mContext.getResources().getIdentifier("ic_palette_0" + viewHolder.getLayoutPosition() % 4, "drawable", mContext.getPackageName()));
}
}
- 初始化Adapter
private void initAdapter() {
mAdapter = new MeAdapter(R.layout.item_fragment_me);
/** --------------------------- 动画效果 --------------------------- **/
//开启动画效果
mAdapter.openLoadAnimation();
//设置动画效果
/**
* 渐显 ALPHAIN
* 缩放 SCALEIN
* 从下到上 SLIDEIN_BOTTOM
* 从左到右 SLIDEIN_LEFT
* 从右到左 SLIDEIN_RIGHT
*/
mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
mAdapter.setNewData(mData);
/** --------------------------- 点击事件 --------------------------- **/
//设置Item点击事件
mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
ToastUitl.showShort("onItemClick:" + position);
}
});
//设置Item长按事件
mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
ToastUitl.showShort("onItemLongClick:" + position);
return false;
}
});
//设置Item中子控件点击事件
mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//判断子控件
if (view.getId() == R.id.tv_name) {
ToastUitl.showShort("onItemChildClick:" + position);
}
}
});
/** --------------------------- 加载更多 --------------------------- **/
//设置加载更多
mAdapter.setOnLoadMoreListener(this, mRecyclerView);
//默认第一次加载会进入回调,如果不需要可以配置
mAdapter.disableLoadMoreIfNotFullPage();
//当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
mAdapter.setPreLoadNumber(1);
/** --------------------------- 添加头部尾部 --------------------------- **/
View item_head = LayoutInflater.from(getContext()).inflate(R.layout.item_head, (ViewGroup) mRecyclerView.getParent(), false);
mAdapter.addHeaderView(item_head);
View item_footer = LayoutInflater.from(getContext()).inflate(R.layout.item_footer, (ViewGroup) mRecyclerView.getParent(), false);
mAdapter.addFooterView(item_footer);
}
- 初始化RecyclerView
private void initRecyclerView() {
//设置布局方式
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//解决数据加载不完的问题
mRecyclerView.setNestedScrollingEnabled(false);
//当知道Adapter内Item的改变不会影响RecyclerView宽高的时候,可以设置为true让RecyclerView避免重新计算大小
mRecyclerView.setHasFixedSize(true);
//解决数据加载完成后, 没有停留在顶部的问题
mRecyclerView.setFocusable(false);
//添加分割线
mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
//设置适配器
mRecyclerView.setAdapter(mAdapter);
// -----------------------点击长按事件的使用方式-----------------------//
// //另一种Item点击事件
// mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
// @Override
// public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemClick:" + position);
// }
// });
// //另一种Item长按事件
// mRecyclerView.addOnItemTouchListener(new OnItemLongClickListener() {
// @Override
// public void onSimpleItemLongClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemLongClick:" + position);
// }
// });
// //另一种设置Item中子控件点击事件
// mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
// @Override
// public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemChildClick:" + position);
// }
// });
// //另一种设置Item中子控件长按事件
// mRecyclerView.addOnItemTouchListener(new OnItemChildLongClickListener() {
// @Override
// public void onSimpleItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemChildLongClick:" + position);
// }
// });
// //if you wish to implement various forms of click(如果你希望实施各种形式的点击)
// mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
// @Override
// public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemClick:" + position);
// }
//
// @Override
// public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemLongClick:" + position);
// }
//
// @Override
// public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemChildClick:" + position);
// }
//
// @Override
// public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
// ToastUitl.showShort("mRecyclerView.onItemChildLongClick:" + position);
// }
// });
// -----------------------End点击长按事件的使用方式----------------------- //
}
- 加载更多
//1.实现
implements BaseQuickAdapter.RequestLoadMoreListener
//2.请求数据
@Override
public void onLoadMoreRequested() {
// 1秒钟后刷新页面
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
// 模拟网络请求获取数据
List data = new ArrayList<>();
mCurrentPage += 1;
for (int i = 0; i < 10; i++) {
data.add(i + "");
}
if (mCurrentPage <= 5) {
mAdapter.addData(data);
//数据加载完成
mAdapter.loadMoreComplete();
} else {
//数据加载完毕
mAdapter.loadMoreEnd();
}
}
}, 1000);
}
- 下拉刷新
//1.实现
implements SwipeRefreshLayout.OnRefreshListener
//2.请求数据
@Override
public void onRefresh() {
//刷新的时候禁止加载更多
mAdapter.setEnableLoadMore(false);
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
mCurrentPage = 1;
//模拟数据
List data = Arrays.asList(AppApplication.getAppContext().getResources().getStringArray(R.array.care_name));
mData = new ArrayList(data);
//更新数据
mAdapter.setNewData(mData);
//刷新完成取消刷新动画
mSwipeRefreshLayout.setRefreshing(false);
//刷新完成重新开启加载更多
mAdapter.setEnableLoadMore(true);
}
}, 1000);
}
- DividerItemDecoration.java
/*******************************************
* @author : 奥特曼
* @Email : [email protected]
* @Description : 分割线
*******************************************/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
// 线性列表 方向
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
private Paint mPaint;
private int mDividerHeight = 2;
/**
* 默认样式分割线
* 宽度为2 颜色为灰色
*
* @param context
* @param orientation
*/
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
/**
* 自定义分割线
*
* @param context
* @param orientation 列表方向
* @param drawableId 分割线图片
*/
public DividerItemDecoration(Context context, int orientation, int drawableId) {
this(context, orientation);
mDivider = ContextCompat.getDrawable(context, drawableId);
mDividerHeight = mDivider.getIntrinsicHeight();
}
/**
* 自定义分割线
*
* @param context
* @param orientation 列表方向
* @param dividerHeight 分割线高度
* @param dividerColor 分割线颜色
*/
public DividerItemDecoration(Context context, int orientation, int dividerHeight, int dividerColor) {
this(context, orientation);
mDividerHeight = dividerHeight;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(context.getResources().getColor(dividerColor));
mPaint.setStyle(Paint.Style.FILL);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
}
// 绘制垂直排列的分割线
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
RecyclerView v = new RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
if (mPaint != null) {
c.drawRect(left, top, right, bottom, mPaint);
}
}
}
// 绘制水平排列的分割线
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
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 left = child.getRight() + params.rightMargin;
final int right = left + mDividerHeight;
if (mDivider != null) {
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
if (mPaint != null) {
c.drawRect(left, top, right, bottom, mPaint);
}
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDividerHeight);
} else {
outRect.set(0, 0, mDividerHeight, 0);
}
}
}