[Android] 零碎知識匯總 - RecyclerView 頂部懸浮

package com.example.dq.app_itemdecoration;

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.RecyclerView;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;

/**
 * Created by DQ on 2017/8/7
 * 城市列表的分割線, 主要用于繪制字母分組
 */
public class CityItemDecoration extends RecyclerView.ItemDecoration {

  private int mGroupHeight = 60;
  private int mSideMargin = 10;
  private Paint mGroupPaint;
  private TextPaint mTextPaint;
  private GroupListener mListener;

  public CityItemDecoration(Context context, GroupListener pListener) {

    mGroupPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mGroupPaint.setColor(context.getResources().getColor(android.R.color.holo_blue_dark));

    mListener = pListener;

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setTextSize(40);
    mTextPaint.setColor(Color.WHITE);
    mTextPaint.setTextAlign(Paint.Align.LEFT);
  }

  // 偏移距離嚼隘,可以變相的理解成欧宜,分割線的高度
  @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    int position = parent.getChildAdapterPosition(view);
    // 不是省份
    if (mListener == null || TextUtils.isEmpty(mListener.getGroupName(position))) {
      return;
    }
    // 是否顯示 省份標簽
    if (isFirstInGroup(position)) {
      outRect.top = mGroupHeight;
    }
  }

  @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);

    final int itemCount = state.getItemCount();
    final int childCount = parent.getChildCount();

    final int left = parent.getLeft() + parent.getPaddingLeft();
    final int right = parent.getRight() + parent.getPaddingRight();

    String preGroupName; // 標記上一個item的省份
    String currentGroupName = null; // 當前item的省份

    for (int i = 0; i < childCount; i++) {
      View child = parent.getChildAt(i);
      int position = parent.getChildAdapterPosition(child);

      preGroupName = currentGroupName;
      currentGroupName = mListener.getGroupName(position);

      // 普通城市 or 和上一次省份相同
      if (TextUtils.equals(currentGroupName, preGroupName)) {
        continue;
      }

      // 計算高度
      int bottom = Math.max(mGroupHeight, child.getTop());

      if (position < itemCount - 1) {
        String nextGroupName = mListener.getGroupName(position + 1);
        int viewBottom = child.getBottom();
        if (!currentGroupName.equals(nextGroupName) && viewBottom < bottom) {
          bottom = viewBottom;
        }
      }

      // 接下來, 就是省份的繪制了
      c.drawRect(left, bottom - mGroupHeight, right, bottom, mGroupPaint);
      // 繪制文字
      Paint.FontMetrics fm = mTextPaint.getFontMetrics();
      float baseline = bottom - (mGroupHeight - (fm.bottom - fm.top)) / 2 - fm.bottom;
      // TODO: 2017/8/7 DQ: 這里添加的是左右邊距础废,是可以自己設置的
      float textWidth = mTextPaint.measureText(currentGroupName);
      float marginLeft = true ? 0 : right - textWidth;
      mSideMargin = Math.abs(mSideMargin);
      mSideMargin = true ? mSideMargin : -mSideMargin;
      c.drawText(currentGroupName, left + mSideMargin + marginLeft, baseline, mTextPaint);
    }
  }

  public interface GroupListener {
    String getGroupName(int position);
  }

  private boolean isFirstInGroup(int position) {
    if (position == 0) {
      return true;
    } else {
      String prevGroupId = mListener.getGroupName(position - 1);
      String groupId = mListener.getGroupName(position);
      return !TextUtils.equals(prevGroupId, groupId);
    }
  }
}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市罕模,隨后出現(xiàn)的幾起案子评腺,更是在濱河造成了極大的恐慌,老刑警劉巖淑掌,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蒿讥,死亡現(xiàn)場離奇詭異,居然都是意外死亡抛腕,警方通過查閱死者的電腦和手機芋绸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來担敌,“玉大人摔敛,你說我怎么就攤上這事∪猓” “怎么了马昙?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長刹悴。 經(jīng)常有香客問我行楞,道長,這世上最難降的妖魔是什么土匀? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任子房,我火速辦了婚禮,結果婚禮上恒削,老公的妹妹穿的比我還像新娘池颈。我一直安慰自己,他們只是感情好钓丰,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布躯砰。 她就那樣靜靜地躺著,像睡著了一般携丁。 火紅的嫁衣襯著肌膚如雪琢歇。 梳的紋絲不亂的頭發(fā)上兰怠,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音李茫,去河邊找鬼揭保。 笑死,一個胖子當著我的面吹牛魄宏,可吹牛的內(nèi)容都是我干的秸侣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宠互,長吁一口氣:“原來是場噩夢啊……” “哼味榛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起予跌,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤搏色,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后券册,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體频轿,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年烁焙,在試婚紗的時候發(fā)現(xiàn)自己被綠了航邢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡考阱,死狀恐怖翠忠,靈堂內(nèi)的尸體忽然破棺而出鞠苟,到底是詐尸還是另有隱情乞榨,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布当娱,位于F島的核電站吃既,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏跨细。R本人自食惡果不足惜鹦倚,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望冀惭。 院中可真熱鬧震叙,春花似錦、人聲如沸散休。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽戚丸。三九已至划址,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背夺颤。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工痢缎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人世澜。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓独旷,卻偏偏與公主長得像,于是被迫代替她去往敵國和親寥裂。 傳聞我的和親對象是個殘疾皇子势告,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容