Android 简单的侧边栏A-Z
Android 简单的侧边栏A-Z
你不懂的神__经 发表于5个月前
Android 简单的侧边栏A-Z
  • 发表于 5个月前
  • 阅读 25
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 侧边栏实现- 最简单 -最简单的逻辑....

1.拼音解析类

public class PinyinParser {
    private static int[] pyvalue = new int[]{-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032,
            -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,
            -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261,
            -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961,
            -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490,
            -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988,
            -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692,
            -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733,
            -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419,
            -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,
            -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631,
            -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180,
            -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941,
            -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857,
            -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353,
            -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094,
            -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831,
            -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,
            -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860,
            -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300,
            -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358,
            -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014,
            -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309,
            -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254};
    public static String[] pystr = new String[]{"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian",
            "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che",
            "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan",
            "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du",
            "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang",
            "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang",
            "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian",
            "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken",
            "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng",
            "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai",
            "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai",
            "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan",
            "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu",
            "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re",
            "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha",
            "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun",
            "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao",
            "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi",
            "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi",
            "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha",
            "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui",
            "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"};
    private StringBuilder buffer;
    private String resource;
    private static PinyinParser pinyinParser = new PinyinParser();

    public static PinyinParser getInstance() {
        return pinyinParser;
    }

    /**
     * 汉字转成ASCII码 * * @param chs * @return
     */
    private int getChsAscii(String chs) {
        int asc = 0;
        try {
            byte[] bytes = chs.getBytes("gb2312");
            if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
                throw new RuntimeException("illegal resource string");
            }
            if (bytes.length == 1) {
                asc = bytes[0];
            }
            if (bytes.length == 2) {
                int hightByte = 256 + bytes[0];
                int lowByte = 256 + bytes[1];
                asc = (256 * hightByte + lowByte) - 256 * 256;
            }
        } catch (Exception e) {
            HxLog.d("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e);
        }
        return asc;
    }

    /**
     * 单字解析 * * @param str * @return
     */
    public String convert(String str) {
        String result = null;
        int ascii = getChsAscii(str);
        if (ascii > 0 && ascii < 160) {
            result = String.valueOf((char) ascii);
        } else {
            for (int i = (pyvalue.length - 1); i >= 0; i--) {
                if (pyvalue[i] <= ascii) {
                    result = pystr[i];
                    break;
                }
            }
        }
        return result;
    }

    /**
     * 获得全拼 默认返回 小写
     *
     * @param chs
     * @return
     */
    public String getSelling(String chs) {
        String key, value;
        buffer = new StringBuilder();
        for (int i = 0; i < chs.length(); i++) {
            key = chs.substring(i, i + 1);
            if (key.getBytes().length >= 2) {
                value = convert(key);
                if (value == null) {
                    value = "unknown";
                }
            } else {
                value = key;
            }
            buffer.append(value);
        }
        return buffer.toString().toLowerCase();
    }

    /**
     * 获得首字母 默认返回 小写
     *
     * @param chs
     * @return
     */
    public String getInitial(String chs) {
        String key, value;
        buffer = new StringBuilder();
        if (chs.length() > 0) {
            key = chs.substring(0, 1);
            if (key.getBytes().length >= 2) {
                value = convert(key);
                if (value == null) {
                    value = "unknown";
                }
            } else {
                value = key;
            }
            buffer.append(value);
            return buffer.substring(0, 1).toString().toLowerCase();
        }
        return buffer.toString();
    }

2.自定义 SiderBar

public class Sidebar extends View {
    private final static int DEFAULT_TEXT_SIZE = 14; // sp
    private final static int DEFAULT_MAX_OFFSET = 80; //dp

    private final static String[] DEFAULT_INDEX_ITEMS = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    private String[] mIndexItems;

    /**
     * the index in {@link #mIndexItems} of the current selected index item,
     * it's reset to -1 when the finger up
     */
    private int mCurrentIndex = -1;

    /**
     * Y coordinate of the point where finger is touching,
     * the baseline is top of {@link #mStartTouchingArea}
     * it's reset to -1 when the finger up
     */
    private float mCurrentY = -1;

    private Paint mPaint;
    private int mTextColor;
    private float mTextSize;

    /**
     * the height of each index item
     */
    private float mIndexItemHeight;

    /**
     * offset of the current selected index item
     */
    private float mMaxOffset;

    /**
     * {@link #mStartTouching} will be set to true when {@link MotionEvent#ACTION_DOWN}
     * happens in this area, and the side bar should start working.
     */
    private RectF mStartTouchingArea = new RectF();

    /**
     * height and width of {@link #mStartTouchingArea}
     */
    private float mBarHeight;
    private float mBarWidth;

    /**
     * Flag that the finger is starting touching.
     * If true, it means the {@link MotionEvent#ACTION_DOWN} happened but
     * {@link MotionEvent#ACTION_UP} not yet.
     */
    private boolean mStartTouching = false;

    /**
     * if true, the {@link OnSelectIndexItemListener#onSelectIndexItem(String)}
     * will not be called until the finger up.
     * if false, it will be called when the finger down, up and move.
     */
    private boolean mLazyRespond = false;

    /**
     * the position of the side bar, default is {@link #POSITION_RIGHT}.
     * You can set it to {@link #POSITION_LEFT} for people who use phone with left hand.
     */
    private int mSideBarPosition;
    public static final int POSITION_RIGHT = 0;
    public static final int POSITION_LEFT = 1;

    /**
     * the alignment of items, default is {@link #TEXT_ALIGN_CENTER}.
     */
    private int mTextAlignment;
    public static final int TEXT_ALIGN_CENTER = 0;
    public static final int TEXT_ALIGN_LEFT = 1;
    public static final int TEXT_ALIGN_RIGHT = 2;


    /**
     * observe the current selected index item
     */
    private OnSelectIndexItemListener onSelectIndexItemListener;

    /**
     * the baseline of the first index item text to draw
     */
    private float mFirstItemBaseLineY;

    /**
     * for {@link #dp2px(int)} and {@link #sp2px(int)}
     */
    private DisplayMetrics mDisplayMetrics;


    public Sidebar(Context context) {
        this(context, null);
    }

    public Sidebar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Sidebar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mDisplayMetrics = context.getResources().getDisplayMetrics();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Sidebar);
        mLazyRespond = typedArray.getBoolean(R.styleable.Sidebar_sidebar_lazy_respond, false);
        mTextColor = typedArray.getColor(R.styleable.Sidebar_sidebar_text_color, Color.GRAY);
        mTextSize = typedArray.getDimension(R.styleable.Sidebar_sidebar_text_size, sp2px(DEFAULT_TEXT_SIZE));
        mMaxOffset = typedArray.getDimension(R.styleable.Sidebar_sidebar_max_offset, dp2px(DEFAULT_MAX_OFFSET));
        mSideBarPosition = typedArray.getInt(R.styleable.Sidebar_sidebar_position, POSITION_RIGHT);
        mTextAlignment = typedArray.getInt(R.styleable.Sidebar_sidebar_text_alignment, TEXT_ALIGN_CENTER);
        typedArray.recycle();

        mIndexItems = DEFAULT_INDEX_ITEMS;

        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mTextColor);
        mPaint.setTextSize(mTextSize);
        switch (mTextAlignment) {
            case TEXT_ALIGN_CENTER: mPaint.setTextAlign(Paint.Align.CENTER); break;
            case TEXT_ALIGN_LEFT:   mPaint.setTextAlign(Paint.Align.LEFT); break;
            case TEXT_ALIGN_RIGHT:  mPaint.setTextAlign(Paint.Align.RIGHT); break;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        mIndexItemHeight = fontMetrics.bottom - fontMetrics.top;
        mBarHeight = mIndexItems.length * mIndexItemHeight;

        // calculate the width of the longest text as the width of side bar
        for (String indexItem : mIndexItems) {
            mBarWidth = Math.max(mBarWidth, mPaint.measureText(indexItem));
        }

        float areaLeft = (mSideBarPosition == POSITION_LEFT) ? 0 : (width - mBarWidth - getPaddingRight());
        float areaRight = (mSideBarPosition == POSITION_LEFT) ? (getPaddingLeft() + areaLeft + mBarWidth) : width;
        float areaTop = height/2 - mBarHeight/2;
        float areaBottom = areaTop + mBarHeight;
        mStartTouchingArea.set(
                areaLeft,
                areaTop,
                areaRight,
                areaBottom);

        // the baseline Y of the first item' text to draw
        mFirstItemBaseLineY = (height/2 - mIndexItems.length*mIndexItemHeight/2)
                + (mIndexItemHeight/2 - (fontMetrics.descent-fontMetrics.ascent)/2)
                - fontMetrics.ascent;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // draw each item
        for (int i = 0, mIndexItemsLength = mIndexItems.length; i < mIndexItemsLength; i++) {
            float baseLineY = mFirstItemBaseLineY + mIndexItemHeight*i;

            // calculate the scale factor of the item to draw
            float scale = getItemScale(i);

            int alphaScale = (i == mCurrentIndex) ? (255) : (int) (255 * (1-scale));
            mPaint.setAlpha(alphaScale);

            mPaint.setTextSize(mTextSize + mTextSize*scale);

            float baseLineX = 0f;
            if (mSideBarPosition == POSITION_LEFT) {
                switch (mTextAlignment) {
                    case TEXT_ALIGN_CENTER:
                        baseLineX = getPaddingLeft() + mBarWidth/2 + mMaxOffset*scale;
                        break;
                    case TEXT_ALIGN_LEFT:
                        baseLineX = getPaddingLeft() + mMaxOffset*scale;
                        break;
                    case TEXT_ALIGN_RIGHT:
                        baseLineX = getPaddingLeft() + mBarWidth + mMaxOffset*scale;
                        break;
                }
            } else {
                switch (mTextAlignment) {
                    case TEXT_ALIGN_CENTER:
                        baseLineX = getWidth() - getPaddingRight() - mBarWidth/2 - mMaxOffset*scale;
                        break;
                    case TEXT_ALIGN_RIGHT:
                        baseLineX = getWidth() - getPaddingRight() - mMaxOffset*scale;
                        break;
                    case TEXT_ALIGN_LEFT:
                        baseLineX = getWidth() - getPaddingRight() - mBarWidth - mMaxOffset*scale;
                        break;
                }
            }

            // draw
            canvas.drawText(
                    mIndexItems[i], //item text to draw
                    baseLineX, //baseLine X
                    baseLineY, // baseLine Y
                    mPaint);
        }

        // reset paint
        mPaint.setAlpha(255);
        mPaint.setTextSize(mTextSize);
    }

    /**
     * calculate the scale factor of the item to draw
     *
     * @param index the index of the item in array {@link #mIndexItems}
     * @return the scale factor of the item to draw
     */
    private float getItemScale(int index) {
        float scale = 0;
        if (mCurrentIndex != -1) {
            float distance = Math.abs(mCurrentY - (mIndexItemHeight*index+mIndexItemHeight/2)) / mIndexItemHeight;
            scale = 1 - distance*distance/16;
            scale = Math.max(scale, 0);
        }
        return scale;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mIndexItems.length == 0) {
            return super.onTouchEvent(event);
        }

        float eventY = event.getY();
        float eventX = event.getX();
        mCurrentIndex = getSelectedIndex(eventY);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mStartTouchingArea.contains(eventX, eventY)) {
                    mStartTouching = true;
                    if (!mLazyRespond && onSelectIndexItemListener != null) {
                        onSelectIndexItemListener.onSelectIndexItem(mIndexItems[mCurrentIndex]);
                    }
                    invalidate();
                    return true;
                } else {
                    mCurrentIndex = -1;
                    return false;
                }

            case MotionEvent.ACTION_MOVE:
                if (mStartTouching && !mLazyRespond && onSelectIndexItemListener != null) {
                    onSelectIndexItemListener.onSelectIndexItem(mIndexItems[mCurrentIndex]);
                }
                invalidate();
                return true;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (mLazyRespond && onSelectIndexItemListener != null) {
                    onSelectIndexItemListener.onSelectIndexItem(mIndexItems[mCurrentIndex]);
                }
                mCurrentIndex = -1;
                mStartTouching = false;
                invalidate();
                return true;
        }

        return super.onTouchEvent(event);
    }

    private int getSelectedIndex(float eventY) {
        mCurrentY = eventY - (getHeight()/2 - mBarHeight /2);
        if (mCurrentY <= 0) {
            return 0;
        }

        int index = (int) (mCurrentY / this.mIndexItemHeight);
        if (index >= this.mIndexItems.length) {
            index = this.mIndexItems.length - 1;
        }
        return index;
    }

    private float dp2px(int dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, this.mDisplayMetrics);
    }

    private float sp2px(int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, this.mDisplayMetrics);
    }

    public void setIndexItems(String... indexItems) {
        mIndexItems = Arrays.copyOf(indexItems, indexItems.length);
        requestLayout();
    }

    public void setTextColor(int color) {
        mTextColor = color;
        mPaint.setColor(color);
        invalidate();
    }

    public void setPosition(int position) {
        if (position != POSITION_RIGHT && position != POSITION_LEFT) {
            throw new IllegalArgumentException("the position must be POSITION_RIGHT or POSITION_LEFT");
        }

        mSideBarPosition = position;
        requestLayout();
    }

    public void setMaxOffset(int offset) {
        mMaxOffset = offset;
        invalidate();
    }

    public void setLazyRespond(boolean lazyRespond) {
        mLazyRespond = lazyRespond;
    }

    public void setTextAlign(int align) {
        if (mTextAlignment == align) {
            return;
        }
        switch (align) {
            case TEXT_ALIGN_CENTER: mPaint.setTextAlign(Paint.Align.CENTER); break;
            case TEXT_ALIGN_LEFT:   mPaint.setTextAlign(Paint.Align.LEFT); break;
            case TEXT_ALIGN_RIGHT:  mPaint.setTextAlign(Paint.Align.RIGHT); break;
            default:
                throw new IllegalArgumentException(
                        "the alignment must be TEXT_ALIGN_CENTER, TEXT_ALIGN_LEFT or TEXT_ALIGN_RIGHT");
        }
        mTextAlignment = align;
        invalidate();
    }

    public void setTextSize(float size) {
        if (mTextSize == size) {
            return;
        }
        mTextSize = size;
        mPaint.setTextSize(size);
        invalidate();
    }

    public void setOnSelectIndexItemListener(OnSelectIndexItemListener onSelectIndexItemListener) {
        this.onSelectIndexItemListener = onSelectIndexItemListener;
    }

    public interface OnSelectIndexItemListener {
        void onSelectIndexItem(String index);
    }

attr.xml

<declare-styleable name="Sidebar">
       <attr name="sidebar_lazy_respond" format="boolean"/>
       <attr name="sidebar_text_size" format="dimension"/>
       <attr name="sidebar_text_color" format="color"/>
       <attr name="sidebar_max_offset" format="dimension"/>
       <attr name="sidebar_position" format="enum">
           <enum name="right" value="0"/>
           <enum name="left" value="1"/>
       </attr>
       <attr name="sidebar_text_alignment" format="enum">
           <enum name="center" value="0"/>
           <enum name="left" value="1"/>
           <enum name="right" value="2" />
       </attr>
   </declare-styleable>

3. 排序显示实体

public class SortModel {
    private String index;
    private String name;

    public SortModel(String index, String name) {
        this.index = index;
        this.name = name;
    }

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

4.Acitiviy 实例

public class CityActivity extends BaseActivity {

    public static final Integer REQUEST_CODE = 9000;
    public static final String RESULT = "result";

    @Bind(R.id.sdrFilter)
    Sidebar sdrFilter;

    private String[] data = {};
    private List<SortModel> sortModelList = new ArrayList<>();

    private RecyclerView rvSortModel;
    private CityAdapter cityAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cm_acti_city);
        ButterKnife.bind(this);

        data = getResources().getStringArray(R.array.city_list);

        for (int i = 0; i < data.length; i++) {
            String name = PinyinParser .getInstance().getSelling(data[i]);
            String sortString = name.substring(0, 1).toUpperCase();
            sortModelList.add(new SortModel(sortString, data[i]));
        }
        Collections.sort(sortModelList, new Comparator<SortModel>() {
            @Override
            public int compare(SortModel lhs, SortModel rhs) {
                return lhs.getIndex().compareTo(rhs.getIndex());
            }
        });

        // init list
        cityAdapter = new CityAdapter(sortModelList, R.layout.view_city_item);
        rvSortModel = (RecyclerView) findViewById(R.id.lv_city);
        rvSortModel.setLayoutManager(new LinearLayoutManager(this));
        rvSortModel.setAdapter(cityAdapter);
        // init adapter click
        cityAdapter.setOnItemClickLitener(new OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                String strResult = data[position].toString();
                Intent intent = new Intent();
                intent.putExtra(RESULT, strResult);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        sdrFilter.setOnSelectIndexItemListener(new Sidebar.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                for (int i = 0; i < sortModelList.size(); i++) {
                    if (sortModelList.get(i).getIndex().equals(index)) {
                        ((LinearLayoutManager) rvSortModel.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
        }
        return true;
    }

    private class CityAdapter extends RecyclerView.Adapter<CityAdapter.CityViewHolder> {

        private List<SortModel> sortModel;
        private int layoutId;

        public CityAdapter(List<SortModel> sortModel, int layoutId) {
            this.sortModel = sortModel;
            this.layoutId = layoutId;
        }

        @Override
        public CityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View view = inflater.inflate(layoutId, null);
            return new CityViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final CityViewHolder holder, final int position) {
            SortModel model = sortModel.get(position);
            if (position == 0 || !sortModel.get(position - 1).getIndex().equals(model.getIndex())) {
                holder.tvIndex.setVisibility(View.VISIBLE);
                holder.tvIndex.setText(model.getIndex());
                holder.tvIndex.setPadding(0, 15, 0, 15);
            } else {
                holder.tvIndex.setVisibility(View.GONE);
            }
            String val[] = model.getName().split("\\*");
            holder.tvCity.setText(val[0]);
            holder.tvValue.setText(val[1]);

            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickLitener.onItemClick(v, position);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return sortModel.size();
        }

        class CityViewHolder extends RecyclerView.ViewHolder {
            public TextView tvIndex;
            public TextView tvValue;
            public TextView tvCity;

            public CityViewHolder(View itemView) {
                super(itemView);
                tvIndex = (TextView) itemView.findViewById(R.id.tv_index);
                tvCity = (TextView) itemView.findViewById(R.id.tv_city);
                tvValue = (TextView) itemView.findViewById(R.id.tv_value);
            }
        }

        private OnItemClickLitener mOnItemClickLitener;

        public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
            this.mOnItemClickLitener = mOnItemClickLitener;
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

5.cm_acti_city.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#f7ffff"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/lv_city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="2dp"
        android:fadingEdge="none" />

    <com.xx.sort.Sidebar
        android:id="@+id/sdrFilter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingRight="8dp"
        android:paddingLeft="8dp"
        app:sidebar_text_color="?attr/primary"/>
</RelativeLayout>

view_city_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/third"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_index"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:text="A"
                android:textColor="?attr/txt_primary"
                android:textSize="16dp" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#701f11" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:gravity="center"
        android:paddingLeft="18dp"
        android:paddingRight="18dp">

        <RelativeLayout
            android:id="@+id/rlay_titleimg"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:visibility="gone"
                android:id="@+id/img_titleimg"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:src="@drawable/lc_ic_loading3" />

            <TextView
                android:id="@+id/tv_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="@string/log_city"
                android:textSize="18dp"
                android:textColor="?attr/txt_primary" />

            <TextView
                android:id="@+id/tv_value"
                android:layout_width="wrap_content"
                android:layout_marginRight="@dimen/margin_10"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:text="+00"
                android:textSize="18dp"
                android:textColor="?attr/txt_second" />

        </RelativeLayout>


    </RelativeLayout>

</LinearLayout>

array.xml 数据集

<string-array name="city_list"  >
    <item>安哥拉             *+244  </item>
    <item>阿富汗             *+93   </item>
    <item>阿尔巴尼亚         *+355  </item>
    <item>阿尔及利亚         *+213  </item>
    <item>安道尔共和国       *+376  </item>
    <item>安圭拉岛           *+1264 </item>
    <item>安提瓜和巴布达     *+1268 </item>
    <item>阿根廷             *+54   </item>
    <item>亚美尼亚           *+374  </item>
    <item>阿森松             *+247  </item>
    <item>澳大利亚           *+61   </item>
    <item>奥地利             *+43   </item>
    <item>阿塞拜疆           *+994  </item>
    <item>巴哈马             *+1242 </item>
    <item>巴林               *+973  </item>
    <item>孟加拉国           *+880  </item>
    <item>巴巴多斯           *+1246 </item>
    <item>白俄罗斯           *+375  </item>
    <item>比利时             *+32   </item>
    <item>伯利兹             *+501  </item>
    <item>贝宁               *+229  </item>
    <item>百慕大群岛         *+1441 </item>
    <item>玻利维亚           *+591  </item>
    <item>博茨瓦纳           *+267  </item>
    <item>巴西               *+55   </item>
    <item>文莱               *+673  </item>
    <item>保加利亚           *+359  </item>
    <item>布基纳法索         *+226  </item>
    <item>缅甸               *+95   </item>
    <item>布隆迪             *+257  </item>
    <item>喀麦隆             *+237  </item>
    <item>加拿大             *+1    </item>
    <item>开曼群岛           *+1345 </item>
    <item>中非共和国         *+236  </item>
    <item>乍得               *+235  </item>
    <item>智利               *+56   </item>
    <item>中国               *+86   </item>
    <item>哥伦比亚           *+57   </item>
    <item>刚果               *+242  </item>
    <item>库克群岛           *+682  </item>
    <item>哥斯达黎加         *+506  </item>
    <item>古巴               *+53   </item>
    <item>塞浦路斯           *+357  </item>
    <item>捷克               *+420  </item>
    <item>丹麦               *+45   </item>
    <item>吉布提             *+253  </item>
    <item>多米尼加共和国     *+1890 </item>
    <item>厄瓜多尔           *+593  </item>
    <item>埃及               *+20   </item>
    <item>萨尔瓦多           *+503  </item>
    <item>爱沙尼亚           *+372  </item>
    <item>埃塞俄比亚         *+251  </item>
    <item>斐济               *+679  </item>
    <item>芬兰               *+358  </item>
    <item>法国               *+33   </item>
    <item>法属圭亚那         *+594  </item>
    <item>加蓬               *+241  </item>
    <item>冈比亚             *+220  </item>
    <item>格鲁吉亚           *+995  </item>
    <item>德国               *+49   </item>
    <item>加纳               *+233  </item>
    <item>直布罗陀           *+350  </item>
    <item>希腊               *+30   </item>
    <item>格林纳达           *+1809 </item>
    <item>关岛               *+1671 </item>
    <item>危地马拉           *+502  </item>
    <item>几内亚             *+224  </item>
    <item>圭亚那             *+592  </item>
    <item>海地               *+509  </item>
    <item>洪都拉斯           *+504  </item>
    <item>香港               *+852  </item>
    <item>匈牙利             *+36   </item>
    <item>冰岛               *+354  </item>
    <item>印度               *+91   </item>
    <item>印度尼西亚         *+62   </item>
    <item>伊朗               *+98   </item>
    <item>伊拉克             *+964  </item>
    <item>爱尔兰             *+353  </item>
    <item>以色列             *+972  </item>
    <item>意大利             *+39   </item>
    <item>科特迪瓦           *+225  </item>
    <item>牙买加             *+1876 </item>
    <item>日本               *+81   </item>
    <item>约旦               *+962  </item>
    <item>柬埔寨             *+855  </item>
    <item>哈萨克斯坦         *+327  </item>
    <item>肯尼亚             *+254  </item>
    <item>韩国               *+82   </item>
    <item>科威特             *+965  </item>
    <item>吉尔吉斯坦         *+331  </item>
    <item>老挝               *+856  </item>
    <item>拉脱维亚           *+371  </item>
    <item>黎巴嫩             *+961  </item>
    <item>莱索托             *+266  </item>
    <item>利比里亚           *+231  </item>
    <item>利比亚             *+218  </item>
    <item>列支敦士登         *+423  </item>
    <item>立陶宛             *+370  </item>
    <item>卢森堡             *+352  </item>
    <item>澳门               *+853  </item>
    <item>马达加斯加         *+261  </item>
    <item>马拉维             *+265  </item>
    <item>马来西亚           *+60   </item>
    <item>马尔代夫           *+960  </item>
    <item>马里               *+223  </item>
    <item>马耳他             *+356  </item>
    <item>马里亚那群岛       *+1670 </item>
    <item>马提尼克           *+596  </item>
    <item>毛里求斯           *+230  </item>
    <item>墨西哥             *+52   </item>
    <item>摩尔多瓦           *+373  </item>
    <item>摩纳哥             *+377  </item>
    <item>蒙古               *+976  </item>
    <item>蒙特塞拉特岛       *+1664 </item>
    <item>摩洛哥             *+212  </item>
    <item>莫桑比克           *+258  </item>
    <item>纳米比亚           *+264  </item>
    <item>瑙鲁               *+674  </item>
    <item>尼泊尔             *+977  </item>
    <item>荷属安的列斯       *+599  </item>
    <item>荷兰               *+31   </item>
    <item>新西兰             *+64   </item>
    <item>尼加拉瓜           *+505  </item>
    <item>尼日尔             *+227  </item>
    <item>尼日利亚           *+234  </item>
    <item>朝鲜               *+850  </item>
    <item>挪威               *+47   </item>
    <item>阿曼               *+968  </item>
    <item>巴基斯坦           *+92   </item>
    <item>巴拿马             *+507  </item>
    <item>巴布亚新几内亚     *+675  </item>
    <item>巴拉圭             *+595  </item>
    <item>秘鲁               *+51   </item>
    <item>菲律宾             *+63   </item>
    <item>波兰               *+48   </item>
    <item>法属玻利尼西亚     *+689  </item>
    <item>葡萄牙             *+351  </item>
    <item>波多黎各           *+1787 </item>
    <item>卡塔尔             *+974  </item>
    <item>留尼旺             *+262  </item>
    <item>罗马尼亚           *+40   </item>
    <item>俄罗斯             *+7    </item>
    <item>圣卢西亚           *+1758 </item>
    <item>圣文森特岛         *+1784 </item>
    <item>东萨摩亚(美)       *+684  </item>
    <item>西萨摩亚           *+685  </item>
    <item>圣马力诺           *+378  </item>
    <item>圣多美和普林西比   *+239  </item>
    <item>沙特阿拉伯         *+966  </item>
    <item>塞内加尔           *+221  </item>
    <item>塞舌尔             *+248  </item>
    <item>塞拉利昂           *+232  </item>
    <item>新加坡             *+65   </item>
    <item>斯洛伐克           *+421  </item>
    <item>斯洛文尼亚         *+386  </item>
    <item>所罗门群岛         *+677  </item>
    <item>索马里             *+252  </item>
    <item>南非               *+27   </item>
    <item>西班牙             *+34   </item>
    <item>斯里兰卡           *+94   </item>
    <item>圣卢西亚           *+1758 </item>
    <item>圣文森特           *+1784 </item>
    <item>苏丹               *+249  </item>
    <item>苏里南             *+597  </item>
    <item>斯威士兰           *+268  </item>
    <item>瑞典               *+46   </item>
    <item>瑞士               *+41   </item>
    <item>叙利亚             *+963  </item>
    <item>台湾省             *+886  </item>
    <item>塔吉克斯坦         *+992  </item>
    <item>坦桑尼亚           *+255  </item>
    <item>泰国               *+66   </item>
    <item>多哥               *+228  </item>
    <item>汤加               *+676  </item>
    <item>特立尼达和多巴哥   *+1809 </item>
    <item>突尼斯             *+216  </item>
    <item>土耳其             *+90   </item>
    <item>土库曼斯坦         *+993  </item>
    <item>乌干达             *+256  </item>
    <item>乌克兰             *+380  </item>
    <item>阿拉伯联合酋长国   *+971  </item>
    <item>英国               *+44   </item>
    <item>美国               *+1    </item>
    <item>乌拉圭             *+598  </item>
    <item>乌兹别克斯坦       *+233  </item>
    <item>委内瑞拉           *+58   </item>
    <item>越南               *+84   </item>
    <item>也门               *+967  </item>
    <item>南斯拉夫           *+381  </item>
    <item>南非               *+27   </item>
    <item>津巴布韦           *+263  </item>
    <item>扎伊尔             *+243  </item>
    <item>赞比亚             *+260  </item>
</string-array>
标签: 侧边栏 siderbar
共有 人打赏支持
粉丝 4
博文 29
码字总数 12439
×
你不懂的神__经
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: