• 冲刺十四天(实现好友页的排序展示)


    实现好友页的字母排序展示

      采用的一个汉字转汉语拼音的第三方库

    implementation 'com.belerweb:pinyin4j:2.5.1'//转拼音

      自定义一个sidebar布局文件来展示拼音

    package com.example.newbsh.UI.friends.list;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.TextView;
    
    import com.example.newbsh.R;
    
    public class SideBar extends androidx.appcompat.widget.AppCompatTextView {
        private String[] letters = new String[]{"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 Paint textPaint;
        private Paint bigTextPaint;
        private Paint scaleTextPaint;
    
        private Canvas canvas;
        private int itemH;
        private int w;
        private int h;
        /**
         * 普通情况下字体大小
         */
        float singleTextH;
        /**
         * 缩放离原始的宽度
         */
        private float scaleWidth;
        /**
         * 滑动的Y
         */
        private float eventY = 0;
        /**
         * 缩放的倍数
         */
        private int scaleSize = 1;
        /**
         * 缩放个数item,即开口大小
         */
        private int scaleItemCount = 6;
        private ISideBarSelectCallBack callBack;
    
        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);
            init(attrs);
        }
    
        private void init(AttributeSet attrs) {
            if (attrs != null) {
                TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.SideBar);
                scaleSize = ta.getInteger(R.styleable.SideBar_scaleSize, 1);
                scaleItemCount = ta.getInteger(R.styleable.SideBar_scaleItemCount, 6);
                scaleWidth = ta.getDimensionPixelSize(R.styleable.SideBar_scaleWidth, dp(100));
                ta.recycle();
            }
            textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            textPaint.setColor(getCurrentTextColor());
            textPaint.setTextSize(getTextSize());
            textPaint.setTextAlign(Paint.Align.CENTER);
            bigTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            bigTextPaint.setColor(getCurrentTextColor());
            bigTextPaint.setTextSize(getTextSize() * (scaleSize + 3));
            bigTextPaint.setTextAlign(Paint.Align.CENTER);
            scaleTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            scaleTextPaint.setColor(getCurrentTextColor());
            scaleTextPaint.setTextSize(getTextSize() * (scaleSize + 1));
            scaleTextPaint.setTextAlign(Paint.Align.CENTER);
        }
    
        public void setDataResource(String[] data) {
            letters = data;
            invalidate();
        }
    
        public void setOnStrSelectCallBack(ISideBarSelectCallBack callBack) {
            this.callBack = callBack;
        }
    
        /**
         * 设置字体缩放比例
         *
         * @param scale
         */
        public void setScaleSize(int scale) {
            scaleSize = scale;
            invalidate();
        }
    
        /**
         * 设置缩放字体的个数,即开口大小
         *
         * @param scaleItemCount
         */
        public void setScaleItemCount(int scaleItemCount) {
            this.scaleItemCount = scaleItemCount;
            invalidate();
        }
    
        private int dp(int px) {
            final float scale = getContext().getResources().getDisplayMetrics().density;
            return (int) (px * scale + 0.5f);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    if (event.getX() > (w - getPaddingRight() - singleTextH - 10)) {
                        eventY = event.getY();
                        invalidate();
                        return true;
                    } else {
                        eventY = 0;
                        invalidate();
                        break;
                    }
                case MotionEvent.ACTION_CANCEL:
                    eventY = 0;
                    invalidate();
                    return true;
                case MotionEvent.ACTION_UP:
                    if (event.getX() > (w - getPaddingRight() - singleTextH - 10)) {
                        eventY = 0;
                        invalidate();
                        return true;
                    } else
                        break;
            }
            return super.onTouchEvent(event);
        }
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            this.canvas = canvas;
            DrawView(eventY);
        }
    
        private void DrawView(float y) {
            int currentSelectIndex = -1;
            if (y != 0) {
                for (int i = 0; i < letters.length; i++) {
                    float currentItemY = itemH * i;
                    float nextItemY = itemH * (i + 1);
                    if (y >= currentItemY && y < nextItemY) {
                        currentSelectIndex = i;
                        if (callBack != null) {
                            callBack.onSelectStr(currentSelectIndex, letters[i]);
                        }
                        //画大的字母
                        Paint.FontMetrics fontMetrics = bigTextPaint.getFontMetrics();
                        float bigTextSize = fontMetrics.descent - fontMetrics.ascent;
                        canvas.drawText(letters[i], w - getPaddingRight() - scaleWidth - bigTextSize, singleTextH + itemH * i, bigTextPaint);
                    }
                }
            }
            drawLetters(y, currentSelectIndex);
        }
    
        private void drawLetters(float y, int index) {
            //第一次进来没有缩放情况,默认画原图
            if (index == -1) {
                w = getMeasuredWidth();
                h = getMeasuredHeight();
                itemH = h / letters.length;
                Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
                singleTextH = fontMetrics.descent - fontMetrics.ascent;
                for (int i = 0; i < letters.length; i++) {
                    canvas.drawText(letters[i], w - getPaddingRight(), singleTextH + itemH * i, textPaint);
                }
                //触摸的时候画缩放图
            } else {
                //遍历所有字母
                for (int i = 0; i < letters.length; i++) {
                    //要画的字母的起始Y坐标
                    float currentItemToDrawY = singleTextH + itemH * i;
                    float centerItemToDrawY;
                    if (index < i)
                        centerItemToDrawY = singleTextH + itemH * (index + scaleItemCount);
                    else
                        centerItemToDrawY = singleTextH + itemH * (index - scaleItemCount);
                    float delta = 1 - Math.abs((y - currentItemToDrawY) / (centerItemToDrawY - currentItemToDrawY));
                    float maxRightX = w - getPaddingRight();
                    //如果大于0,表明在y坐标上方
                    scaleTextPaint.setTextSize(getTextSize() + getTextSize() * delta);
                    float drawX = maxRightX - scaleWidth * delta;
                    //超出边界直接花在边界上
                    if (drawX > maxRightX)
                        canvas.drawText(letters[i], maxRightX, singleTextH + itemH * i, textPaint);
                    else
                        canvas.drawText(letters[i], drawX, singleTextH + itemH * i, scaleTextPaint);
                }
            }
        }
    
        public interface ISideBarSelectCallBack {
            void onSelectStr(int index, String selectStr);
        }
    
    }

      创建汉字转换位汉语拼音,英文字符不变的一个工具类

    package com.example.newbsh.UI.friends.list;
    
    import net.sourceforge.pinyin4j.PinyinHelper;
    import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
    
    /**
     * 汉字转换位汉语拼音,英文字符不变
     */
    public class Cn2Spell {
    
        public static StringBuffer sb = new StringBuffer();
    
        /**
         * 获取汉字字符串的首字母,英文字符不变
         * 例如:阿飞→af
         */
        public static String getPinYinHeadChar(String chines) {
            sb.setLength(0);
            char[] chars = chines.toCharArray();
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] > 128) {
                    try {
                        sb.append(PinyinHelper.toHanyuPinyinStringArray(chars[i], defaultFormat)[0].charAt(0));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    sb.append(chars[i]);
                }
            }
            return sb.toString();
        }
    
        /**
         * 获取汉字字符串的第一个字母
         */
        public static String getPinYinFirstLetter(String str) {
            sb.setLength(0);
            char c = str.charAt(0);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null) {
                sb.append(pinyinArray[0].charAt(0));
            } else {
                sb.append(c);
            }
            return sb.toString();
        }
    
        /**
         * 获取汉字字符串的汉语拼音,英文字符不变
         */
        public static String getPinYin(String chines) {
            sb.setLength(0);
            char[] nameChar = chines.toCharArray();
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (int i = 0; i < nameChar.length; i++) {
                if (nameChar[i] > 128) {
                    try {
                        sb.append(PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    sb.append(nameChar[i]);
                }
            }
            return sb.toString();
        }
    
    }

      一个排序的adapter,来展示好友列表

    package com.example.newbsh.UI.friends.list;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.example.newbsh.R;
    
    import java.util.List;
    
    public class SortAdapter extends BaseAdapter {
    
        private List<User> list = null;
        private Context mContext;
    
        public SortAdapter(Context mContext, List<User> list) {
            this.mContext = mContext;
            this.list = list;
        }
    
        public int getCount() {
            return this.list.size();
        }
    
        public Object getItem(int position) {
            return list.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(final int position, View view, ViewGroup arg2) {
            ViewHolder viewHolder;
            final User user = list.get(position);
            if (view == null) {
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(mContext).inflate(R.layout.friend_item, null);
                viewHolder.name = (TextView) view.findViewById(R.id.name);
                viewHolder.catalog = (TextView) view.findViewById(R.id.catalog);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
    
            //根据position获取首字母作为目录catalog
            String catalog = list.get(position).getFirstLetter();
    
            //如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现
            if(position == getPositionForSection(catalog)){
                viewHolder.catalog.setVisibility(View.VISIBLE);
                viewHolder.catalog.setText(user.getFirstLetter().toUpperCase());
            }else{
                viewHolder.catalog.setVisibility(View.GONE);
            }
    
            viewHolder.name.setText(this.list.get(position).getName());
            return view;
    
        }
    
        final static class ViewHolder {
            TextView catalog;
            TextView name;
        }
    
        /**
         * 获取catalog首次出现位置
         */
        public int getPositionForSection(String catalog) {
            for (int i = 0; i < getCount(); i++) {
                String sortStr = list.get(i).getFirstLetter();
                if (catalog.equalsIgnoreCase(sortStr)) {
                    return i;
                }
            }
            return -1;
        }
    
    }

       FriendFragment的设置,装配好友信息(没有写添加好友,目前是静态数据)

    package com.example.newbsh.UI.friends;
    
    import androidx.lifecycle.ViewModelProviders;
    
    import android.os.Bundle;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import com.example.newbsh.MainActivity;
    import com.example.newbsh.R;
    import com.example.newbsh.UI.friends.list.SideBar;
    import com.example.newbsh.UI.friends.list.SortAdapter;
    import com.example.newbsh.UI.friends.list.User;
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class FriendFragment extends Fragment {
    
        private FriendViewModel mViewModel;
        private View view;
        private ListView listView;
        private SideBar sideBar;
        private ArrayList<User> list;
        private String getUsername(){
            return MainActivity.getUserName();
        }
        public static FriendFragment newInstance() {
            return new FriendFragment();
        }
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            view=inflater.inflate(R.layout.friend_fragment, container, false);
            return view;
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mViewModel = ViewModelProviders.of(this).get(FriendViewModel.class);
            // TODO: Use the ViewModel
            initView();
            initData();
        }
        private void initView() {
            listView = (ListView) view.findViewById(R.id.listView);
            sideBar = (SideBar) view.findViewById(R.id.side_bar);
            sideBar.setOnStrSelectCallBack(new SideBar.ISideBarSelectCallBack() {
                @Override
                public void onSelectStr(int index, String selectStr) {
                    for (int i = 0; i < list.size(); i++) {
                        if (selectStr.equalsIgnoreCase(list.get(i).getFirstLetter())) {
                            listView.setSelection(i); // 选择到首字母出现的位置
                            return;
                        }
                    }
                }
            });
        }
    
        private void initData() {
            list = new ArrayList<>();
            list.add(new User("亳州")); // 亳[bó]属于不常见的二级汉字
            list.add(new User("大娃"));
            list.add(new User("二娃"));
            list.add(new User("三娃"));
            list.add(new User("四娃"));
            list.add(new User("五娃"));
            list.add(new User("六娃"));
            list.add(new User("七娃"));
            list.add(new User("喜羊羊"));
            list.add(new User("美羊羊"));
            list.add(new User("懒羊羊"));
            list.add(new User("沸羊羊"));
            list.add(new User("暖羊羊"));
            list.add(new User("慢羊羊"));
            list.add(new User("灰太狼"));
            list.add(new User("红太狼"));
            list.add(new User("孙悟空"));
            list.add(new User("黑猫警长"));
            list.add(new User("舒克"));
            list.add(new User("贝塔"));
            list.add(new User("海尔"));
            list.add(new User("阿凡提"));
            list.add(new User("邋遢大王"));
            list.add(new User("哪吒"));
            list.add(new User("没头脑"));
            list.add(new User("不高兴"));
            list.add(new User("蓝皮鼠"));
            list.add(new User("大脸猫"));
            list.add(new User("大头儿子"));
            list.add(new User("小头爸爸"));
            list.add(new User("蓝猫"));
            list.add(new User("淘气"));
            list.add(new User("叶峰"));
            list.add(new User("楚天歌"));
            list.add(new User("江流儿"));
            list.add(new User("Tom"));
            list.add(new User("Jerry"));
            list.add(new User("12345"));
            list.add(new User("54321"));
            list.add(new User("_(:з」∠)_"));
            list.add(new User("……%¥#¥%#"));
            Collections.sort(list); // 对list进行排序,需要让User实现Comparable接口重写compareTo方法
            SortAdapter adapter = new SortAdapter(view.getContext(), list);
            listView.setAdapter(adapter);
        }
    }

      最终展示

     点击右侧会有大波浪来展示当前的字母排序的好友

     

  • 相关阅读:
    syslog-ng:retint_careful导致机器hang
    [kernel学习]----好文章梳理
    ext4的一些特性
    [转载]查看Linux系统硬件信息实例详解
    dmidecode输出详解
    [Kernel参数]----/etc/sysctl.conf
    大数据竞赛平台Kaggle案例实战
    Python zip()函数的使用
    TensorFlow softmax的互熵损失
    TensorFlow 的softmax实例理解
  • 原文地址:https://www.cnblogs.com/xiaofengzai/p/12846458.html
Copyright © 2020-2023  润新知