安卓列表字母索引

首先看一下效果图
这里写图片描述
旁边是有一行的字母索引,当点击字母的时候同时列表也会随之改变
这用到了自定义View
下面看一下我的代码。



public class ContactListView extends RelativeLayout {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    public ContactListView(Context context) {
        this(context,null);
    }

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

    public ContactListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.contact_list_layout, this, true);
        recyclerView=  findViewById(R.id.contact_recyclerView);
        swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ContactListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);

    }
    public void setAdapter(RecyclerView.Adapter adapter){
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
    }
    public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener){
        swipeRefreshLayout.setOnRefreshListener(onRefreshListener);
    }

    public void setRefre(boolean b) {
        if (swipeRefreshLayout!=null){
            swipeRefreshLayout.setRefreshing(b);
        }
    }

这里是封装了RecyclerView的刷新事件,下面才是重要的东西

public class Slidebar extends View {
    private TextView tvFloat;
    private RecyclerView contactRecyclerView;
    private Paint paint;
    private float avgHeight;
    private static final String[] SECTIONS = {"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 measuredWidth;
    private int measuredHeight;

    public Slidebar(Context context) {
        super(context,null);
    }

    public Slidebar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Slidebar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public Slidebar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                showToastAndScroll(event.getY());
            break;
            case MotionEvent.ACTION_UP:
                if (tvFloat!=null){
                    tvFloat.setVisibility(GONE);
                }
                setBackgroundColor(Color.TRANSPARENT);
                break;

        }
        return true;
    }

    private void showToastAndScroll(float y) {
        if (tvFloat==null){
            ViewGroup parent = (ViewGroup) getParent();
            tvFloat = parent.findViewById(R.id.tv_float);
             contactRecyclerView = parent.findViewById(R.id.contact_recyclerView);
        }
        tvFloat.setVisibility(VISIBLE);
        setBackgroundResource(R.drawable.slidebar_bk);
        int index= (int) (y/avgHeight);
        if (index<0){
            index=0;
        }else if (index>SECTIONS.length-1){
            index=SECTIONS.length-1;
        }
        String slideBarSection =SECTIONS[index];
        tvFloat.setText(slideBarSection);
//        ContactAdapter contactAdapter = (ContactAdapter) contactRecyclerView.getAdapter();
//        List<String> data = contactAdapter.getDate();
//        for (int i = 0; i < data.size() ; i++) {
//            if (StringUtils.getInital(data.get(i)).equals(slideBarSection)){
//                contactRecyclerView.smoothScrollToPosition(i);
//            return;}
//        }
        RecyclerView.Adapter adapter=contactRecyclerView.getAdapter();
        if (!(adapter instanceof SectionIndexer)){
            return;
        }
        SectionIndexer sectionIndexer= (SectionIndexer) adapter;
        //获取真实中的所有的分区
        String[] sections= (String[]) sectionIndexer.getSections();
        //当前用户点击的是J,然后需要找到J在真实分区占的角脚标
        int sectionIndex= Arrays.binarySearch(sections,slideBarSection);
        //根据sections的脚表找到条目的脚标
        if (sectionIndex<0){
            return;
        }
        int positionForSection=sectionIndexer.getPositionForSection(sectionIndex);
        //根据条目的脚标让RecyclerView定位到该位置
        contactRecyclerView.scrollToPosition(positionForSection);
    }

    private void initView(Context context) {
        paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#8c8c8c"));
        paint.setTextSize(DensityUtil.sp2px(getContext(),10));
        paint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         measuredWidth = getMeasuredWidth();
         measuredHeight = getMeasuredHeight()-getPaddingBottom();
        avgHeight = (measuredHeight + 0.f) / SECTIONS.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x=measuredWidth/2;
        for (int i = 0; i <SECTIONS.length ; i++) {
            float y=avgHeight*(i+1);
            canvas.drawText(SECTIONS[i],x,y,paint);
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值