好友列表按字母排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40391500/article/details/87912581
	//按字母a~z排序
	Collections.sort(mList, new Comparator<FriendInfo>() {
	    @Override
	        public int compare(FriendInfo o1, FriendInfo o2) {
	            char str1 = PinYinUtils.getPinYinFirstChar(o1.getName());
	            char str2 = PinYinUtils.getPinYinFirstChar(o2.getName());
	            if (str1 < str2){
	                return -1;
	            }else {
	                return 1;
	            }
	        }
	    });
	adapter.notifyDataSetChanged(mList);

	//适配器adapter
	public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder> {
	    private Context mContext;
	    private List<FriendInfo> mList = new ArrayList<>();
	    private LayoutInflater inflater;
	
	    public FriendAdapter(Context context, List<FriendInfo> list){
	        this.mContext = context;
	        this.mList = list;
	        initPinYin(mList);
	        this.inflater = LayoutInflater.from(mContext);
	    }
	
	    @NonNull
	    @Override
	    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
	        View view = inflater.inflate(R.layout.friend_item, viewGroup, false);
	        ViewHolder holder = new ViewHolder(view);
	        return holder;
	    }
	
	    @Override
	    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
	
	        String currentStr = PinYinUtils.getPinYinFirstLetter(mList.get(i).getName());
	        String previewStr = (i - 1)>= 0? PinYinUtils.getPinYinFirstLetter(mList.get(i - 1).getName()): "";
	        if (!currentStr.equals(previewStr)){
	            viewHolder.tv_pinyin.setVisibility(View.VISIBLE);
	            viewHolder.tv_pinyin.setText(currentStr.toUpperCase());
	        }else {
	            viewHolder.tv_pinyin.setVisibility(View.GONE);
	        }
	        Glide.with(mContext).load(mList.get(i).getFaddr()).into(viewHolder.iv_icon);
	        viewHolder.tv_name.setText(mList.get(i).getName());
	    }
	
	    @Override
	    public int getItemCount() {
	        return mList.size();
	    }
	
	    public static class ViewHolder extends RecyclerView.ViewHolder{
	        private TextView tv_pinyin;
	        private ImageView iv_icon;
	        private TextView tv_name;
	
	        public ViewHolder(@NonNull View itemView) {
	            super(itemView);
	            tv_pinyin = itemView.findViewById(R.id.tv_pinyin);
	            iv_icon = itemView.findViewById(R.id.iv_icon);
	            tv_name = itemView.findViewById(R.id.tv_name);
	        }
	    }
	
	    public void notifyDataSetChanged(List<FriendInfo> list){
	        mList = list;
	        initPinYin(mList);
	        notifyDataSetChanged();
	    }
	
	    private HashMap<String, Integer> pyMap = new HashMap<>();
	
	    /***
	     * 获取好友对应首字母下标排序
	     * @param list
	     */
	    private void initPinYin(List<FriendInfo> list){
	        for (int i = 0; i < list.size(); i++) {
	            String currentStr = PinYinUtils.getPinYinFirstLetter(list.get(i).getName());
	            String previewStr = (i - 1)>= 0? PinYinUtils.getPinYinFirstLetter(list.get(i - 1).getName()): "";
	            if (!currentStr.equals(previewStr)){
	                pyMap.put(currentStr, i);
	            }
	        }
	    }
	}

	//xml
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center"
	    android:orientation="vertical">
	
	    <TextView
	        android:id="@+id/tv_pinyin"
	        android:layout_width="match_parent"
	        android:layout_height="45dp"
	        android:background="@color/pinyin1"
	        android:paddingLeft="20dp"
	        android:textSize="18sp"
	        android:gravity="center_vertical"
	        android:text="A"/>
	    
	     <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:text="阿牛"
            android:textColor="@color/text_color"
            android:textSize="15sp" />
	
	</LinearLayout>
	
	/**
     * 获取汉字字符串的第一个字母
     */
    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 char getPinYinFirstChar(String str) {
        char c = str.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
        if (pinyinArray != null) {
            c = pinyinArray[0].charAt(0);
        }
        return c;
    }

	/***
	 * 右侧字母自定义view
	 *
	 */
	public class QuickLocationBar extends View {
	    private String characters[] = { "#", "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 int choose = -1;
	    private Paint paint = new Paint();
	    private OnTouchLetterChangedListener mOnTouchLetterChangedListener;
	    private TextView mTextDialog;
	
	    public QuickLocationBar(Context context, AttributeSet attrs, int defStyleAttr) {
	        super(context, attrs, defStyleAttr);
	    }
	
	    public QuickLocationBar(Context context, AttributeSet attrs) {
	        super(context, attrs);
	    }
	
	    public QuickLocationBar(Context context) {
	        super(context);
	    }
	
	    public void setOnTouchLitterChangedListener(
	            OnTouchLetterChangedListener onTouchLetterChangedListener) {
	        this.mOnTouchLetterChangedListener = onTouchLetterChangedListener;
	    }
	
	    public void setTextDialog(TextView dialog) {
	        this.mTextDialog = dialog;
	    }
	
	    @Override
	    protected void onDraw(Canvas canvas) {
	        // TODO Auto-generated method stub
	        super.onDraw(canvas);
	        int width = getWidth();
	        int height = getHeight();
	        int singleHeight = height / characters.length;
	        for (int i = 0; i < characters.length; i++) {
	            // 对paint进行相关的参数设置
	            paint.setColor(getResources().getColor(R.color.huise));
	            paint.setTypeface(Typeface.DEFAULT_BOLD);
	            paint.setAntiAlias(true);
	            paint.setTextSize(150*(float) width/320);
	            if (i == choose) {// choose变量表示当前显示的字符位置,若没有触摸则为-1
	                paint.setColor(getResources().getColor(R.color.huise));
	                paint.setFakeBoldText(true);
	            }
	            // 计算字符的绘制的位置
	            float xPos = width / 2 - paint.measureText(characters[i]) / 2;
	            float yPos = singleHeight * i + singleHeight;
	            // 在画布上绘制字符
	            canvas.drawText(characters[i], xPos, yPos, paint);
	            paint.reset();// 每次绘制完成后不要忘记重制Paint
	        }
	    }
	
	
	
	    @Override
	    public boolean dispatchTouchEvent(MotionEvent event) {
	        int action = event.getAction();
	        float y = event.getY();
	        int c = (int) (y / getHeight() * characters.length);
	
	        switch (action) {
	            case MotionEvent.ACTION_UP:
	                choose = -1;//
	                setBackgroundColor(0x0000);
	                invalidate();
	                if (mTextDialog != null) {
	                    mTextDialog.setVisibility(View.GONE);
	                }
	                break;
	
	            case MotionEvent.ACTION_DOWN:
	            case MotionEvent.ACTION_MOVE:
	                //setBackgroundColor(getResources().getColor(R.color.bg_653fac));
	                if (choose != c) {
	                    if (c >= 0 && c < characters.length) {
	                        if (mOnTouchLetterChangedListener != null) {
	                            mOnTouchLetterChangedListener
	                                    .touchLetterChanged(characters[c]);
	                        }
	                        if (mTextDialog != null) {
	                            mTextDialog.setText(characters[c]);
	                            mTextDialog.setVisibility(View.VISIBLE);
	                        }
	                        choose = c;
	                        invalidate();
	                    }
	                }
	                break;
	        }
	        return true;
	    }
	
	    public interface OnTouchLetterChangedListener {
	        public void touchLetterChanged(String s);
	    }
	
	}

猜你喜欢

转载自blog.csdn.net/weixin_40391500/article/details/87912581
0条评论
添加一条新回复