Android 最流行的吸顶效果的实现及代码( 二 )


item的布局:

第五步:一定不能忘!!!
不仅要设置适配器还要设置布局管理者,否则图片不显示
GridLayoutManager manager = new GridLayoutManager(this, 1);recyclerView.setLayoutManager(manager);
此时简单的完成效果如下:

Android 最流行的吸顶效果的实现及代码

文章插图
下面开始做 可推动的 悬浮导航栏:
第一步:首先我们来写一个类,它起标记的作用,来放每一个item的对应的悬浮栏的字符串
public class NameBean {String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}
public class SectionDecoration extends RecyclerView.ItemDecoration {private static final String TAG = "SectionDecoration";private List dataList;private DecorationCallback callback;private TextPaint textPaint;private Paint paint;private int topGap;private int alignBottom;private Paint.FontMetrics fontMetrics;public SectionDecoration(List dataList, Context context, DecorationCallback decorationCallback) {Resources res = context.getResources();this.dataList = dataList;this.callback = decorationCallback;//设置悬浮栏的画笔---paintpaint = new Paint();paint.setColor(res.getColor(R.color.colorGray));//设置悬浮栏中文本的画笔textPaint = new TextPaint();textPaint.setAntiAlias(true);textPaint.setTextSize(DensityUtil.dip2px(context, 14));textPaint.setColor(Color.DKGRAY);textPaint.setTextAlign(Paint.Align.LEFT);fontMetrics = new Paint.FontMetrics();//决定悬浮栏的高度等topGap = res.getDimensionPixelSize(R.dimen.sectioned_top);//决定文本的显示位置等alignBottom = res.getDimensionPixelSize(R.dimen.sectioned_alignBottom);}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);int pos = parent.getChildAdapterPosition(view);Log.i(TAG, "getItemOffsets:" + pos);String groupId = callback.getGroupId(pos);if (groupId.equals("-1")) return;//只有是同一组的第一个才显示悬浮栏if (pos == 0 || isFirstInGroup(pos)) {outRect.top = topGap;if (dataList.get(pos).getName() == "") {outRect.top = 0;}} else {outRect.top = 0;}}@Overridepublic void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDraw(c, parent, state);int left = parent.getPaddingLeft();int right = parent.getWidth() - parent.getPaddingRight();int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {View view = parent.getChildAt(i);int position = parent.getChildAdapterPosition(view);String groupId = callback.getGroupId(position);if (groupId.equals("-1")) return;String textLine = callback.getGroupFirstLine(position).toUpperCase();if (textLine == "") {float top = view.getTop();float bottom = view.getTop();c.drawRect(left, top, right, bottom, paint);return;} else {if (position == 0 || isFirstInGroup(position)) {float top = view.getTop() - topGap;float bottom = view.getTop();//绘制悬浮栏c.drawRect(left, top - topGap, right, bottom, paint);//绘制文本c.drawText(textLine, left, bottom, textPaint);}}}}@Overridepublic void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDrawOver(c, parent, state);int itemCount = state.getItemCount();int childCount = parent.getChildCount();int left = parent.getPaddingLeft();int right = parent.getWidth() - parent.getPaddingRight();float lineHeight = textPaint.getTextSize() + fontMetrics.descent;String preGroupId = "";String groupId = "-1";for (int i = 0; i