RecyclerView的简单使⽤--(横向纵向)列表、⽹格、瀑布流的实现代码实现
RecyclerViewActivity.java
public class RecyclerViewActivity extends BaActivity {
private RecyclerView recyclerView;
private MyRecyclerViewAdapter adapter;
泡泡少儿英语教材
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
tContentView(R.layout.activity_recyclerview);
recyclerView = findViewById(lerview);
initRecyclerView();
}
/**
烂漫是什么意思
* 初始化 RecyclerView
*/
private void initRecyclerView() {
initLayoutManager();
initItemDivider();
initAdapter();诉讼双雄第三季
initItemClickListener();
}
/**
* 设置布局管理器
*/
private void initLayoutManager() {
/**
* ⽤于实现列表布局
*/
LinearLayoutManager manager = new LinearLayoutManager(this);单独考试
manager.tOrientation(LinearLayoutManager.VERTICAL);
recyclerView.tLayoutManager(manager);
/**
* ⽤于实现⽹格布局
*/
// GridLayoutManager manager = new GridLayoutManager(this, 3);
// manager.tOrientation(GridLayoutManager.VERTICAL);
// recyclerView.tLayoutManager(manager);
/**
* ⽤于实现瀑布流
* 第⼀个参数表⽰布局的列数/⾏数
* 第⼆个参数表⽰布局的⽅向
* StaggeredGridLayoutManager.VERTICAL -- 表⽰布局纵向排列
* StaggeredGridLayoutManager.HORIZONTAL -- 表⽰布局横向排列
*/
/
/ StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
// manager.tOrientation(StaggeredGridLayoutManager.HORIZONTAL);
// recyclerView.tLayoutManager(manager);
/**
* 设置 adapter
*/
private void initAdapter() {
adapter = new MyRecyclerViewAdapter(this, initBookList());
recyclerView.tAdapter(adapter);
}
/**
* 设置点击或长按事件监听
*/
private void initItemClickListener() {
OnRecyclerViewItemClickListener itemClickListener = new OnRecyclerViewItemClickListener() { /**
* item 点击事件
*
* @param position
*/
@Override
public void onItemClick(int position) {
Toast.makeText(RecyclerViewActivity.this, position + "被点击", Toast.LENGTH_SHORT).show();
}
/**
* item 长按事件
* @param position
*/
@Override
public boolean onItemLongClick(int position) {
Toast.makeText(RecyclerViewActivity.this, position + "被长按", Toast.LENGTH_SHORT).show();
return true;
}
};
adapter.tOnRecyclerViewItemClickListener(itemClickListener);
}
/**
* 给 item 添加分割线
sufferfrom
* 注意:只适⽤于列表布局(LinearLayout)的 RecyclerView
* ⽹格布局(GridLayout)和流式布局(StaggeredGridLayout)的 RecyclerView 就不适⽤了。
*/
private void initItemDivider() {
//添加 Android ⾃带的分割线
// DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//添加分割线,但最后⼀个 item 不添加分割线的效果
CustomDividerItemDecoration divider = new CustomDividerItemDecoration(this, DividerItemDecoration.VERTICAL);
大学英语四六级准考证打印入口//添加⾃定义图⽚/ 图形的分割线
divider.tDrawable(getResources().getDrawable(R.drawable.line_gradient));
recyclerView.addItemDecoration(divider);
}
/**
* 初始化集合数据
*
* @return mBookList
*/
private List<Book> initBookList() {
List<Book> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
list.add(new Book("book" + i, "author" + i));
}
return list;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<BookViewHolder> implements View.OnClickListener, View.OnLongClickListener
Context context;
List<Book> list;
private OnRecyclerViewItemClickListener itemClickListener = null;
/**
* 设置 item 点击监听器
* @param itemClickListener
*/
public void tOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public MyRecyclerViewAdapter(Context context, List<Book> list) {
this.list = list;
}
@Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.viewholder_recyclerview, parent, fal);
BookViewHolder viewHolder = new BookViewHolder(view);
view.tOnClickListener(this);
employmentview.tOnLongClickListener(this);
return viewHolder;
}
@Override
public void onBindViewHolder(BookViewHolder holder, int position) {
if (itemClickListener == null) {
return;
}
//被点击的 item 设置 tag 标签,⽤于 item 被点击时获取 position 索引值
holder.itemView.tTag(position);
}
@Override
public int getItemCount() {
农场对对碰
return list.size();
}
/**
* 当 item 被点击时,调⽤ item 点击监听,执⾏点击回调
* @param v
*/
@Override
public void onClick(View v) {
if (itemClickListener == null) {
return;
}
//被点击的 item 获取 tag 标签,即 position 索引值
}
@Override
public boolean onLongClick(View v) {
ItemLongClick((Integer) v.getTag());
}
/**
* RecyclerView 的 ViewHolder 实现类
*/
public class BookViewHolder extends RecyclerView.ViewHolder {
TextView textview;
public BookViewHolder(View itemView) {
super(itemView);
textview = itemView.findViewById(view);
}
}
}
参照 DividerItemDecoration ⾃定义 item 的分割线
/**
* 最后⼀个 item 不添加分割线
* 只能⽤于 LinearLayoutManager 列表布局,不能⽤于⽹格布局(GridLayoutManager)和瀑布流(StaggeredGridLayoutManager) */
public class CustomDividerItemDecoration extends RecyclerView.ItemDecoration {
public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
public static final int VERTICAL = LinearLayout.VERTICAL;
private static final String TAG = "CustomDividerItem";
private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };
private Drawable mDivider;
/**
* Current orientation. Either {@link #HORIZONTAL} or {@link #VERTICAL}.
*/
private int mOrientation;
private final Rect mBounds = new Rect();
/**
* 只能⽤于 LinearLayoutManager 列表布局,不能⽤于⽹格布局(GridLayoutManager)和瀑布流(StaggeredGridLayoutManager) *
* @param context Current context, it will be ud to access resources.
* @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
*/
public CustomDividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
if (mDivider == null) {
kevingarnettLog.w(TAG, "@android:attr/listDivider was not t in the theme ud for this "
+ "DividerItemDecoration. Plea t that attribute all call tDrawable()");
}
tOrientation(orientation);
}
/**
* Sets the orientation for this divider. This should be called if
* {@link RecyclerView.LayoutManager} changes orientation.
*
* @param orientation {@link #HORIZONTAL} or {@link #VERTICAL}
*/
if (orientation != HORIZONTAL && orientation != VERTICAL) {
throw new IllegalArgumentException(
"Invalid orientation. It should be either HORIZONTAL or VERTICAL"); }
mOrientation = orientation;
}
/**
* Sets the {@link Drawable} for this divider.
*
* @param drawable Drawable that should be ud as a divider.
*/
public void tDrawable(@NonNull Drawable drawable) {
if (drawable == null) {
throw new IllegalArgumentException("Drawable cannot be null.");
}
mDivider = drawable;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { if (LayoutManager() == null || mDivider == null) {
return;
按顺序 英文}
if (mOrientation == VERTICAL) {
drawVertical(c, parent);
} el {
drawHorizontal(c, parent);
}
}
private void drawVertical(Canvas canvas, RecyclerView parent) {
canvas.save();
final int left;
final int right;
//noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
if (ClipToPadding()) {
left = PaddingLeft();
right = Width() - PaddingRight();
canvas.clipRect(left, PaddingTop(), right,
} el {
left = 0;
right = Width();
}
final int childCount = ChildCount();
for (int i = 0; i < childCount - 1; i++) {
final View child = ChildAt(i);
final int bottom = mBounds.bottom + TranslationY());
final int top = bottom - IntrinsicHeight();
mDivider.tBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
}
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
canvas.save();
final int top;
final int bottom;
//noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
if (ClipToPadding()) {