41右側字母索引欄的(de)編寫 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 技術分享 >

41右側字母索引欄的(de)編寫

發表時(shí)間:2020-10-19

發布人(rén):融晨科技

浏覽次數:38


實現的(de)效不(bù)雅圖:
[img]http://img.blog.csdn.net/20150104113150383?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZ1ZHVvX2xvdmVpdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
經由過程自定義View,然後在(zài)xml文件中引用可以(yǐ)達到(dào)需求。
public SiderBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	private String[] sections = 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 void init(){
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.DKGRAY);
		paint.setTextAlign(Align.CENTER);
		paint.setTextSize(DensityUtil.sp2px(context, 10));
	}

起首應用帶有兩個(gè)參數的(de)構造器,然後初始化數組和(hé / huò)初始化畫筆。
在(zài)onDraw辦法中,“寫出(chū)”每個(gè)字母的(de)值:
@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		float center = getWidth() / 2;
		height = getHeight() / sections.length;
		for (int i = sections.length - 1; i > -1; i--) {
			canvas.drawText(sections[i], center, height * (i+1), paint);
		}
	}

寬度是(shì)這(zhè)個(gè)View的(de)寬度的(de)一般,每個(gè)高度是(shì)View的(de)總高度除以(yǐ)數組的(de)長度(每個(gè)字母的(de)高度就(jiù)肯定了(le/liǎo)),如許就(jiù)“寫上(shàng)”了(le/liǎo)每個(gè)字母。
接下來(lái)的(de)sectionForPoint實現每個(gè)數組元素的(de)索引,如下:
private int sectionForPoint(float y) {
		
		
		
		LogUtil.d(TAG, "y :" + y + "");
		LogUtil.d(TAG, "height :" + height + "");
		
		
		int index = (int) (y / height);
		LogUtil.d(TAG, "index :" + index + "");
		if(index < 0) {
			index = 0;
		}
		if(index > sections.length - 1){
			LogUtil.d(TAG, "sections.length - 1:" + (sections.length - 1) + "");
			index = sections.length - 1;
		}
		return index;
	}

全部View的(de)高度不(bù)變:
[img]http://img.blog.csdn.net/20150104114350387?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmZ1ZHVvX2xvdmVpdA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
須要(yào / yāo)推敲下界線的(de)問題。
接着在(zài)onTouchEvent處理響應的(de)四個(gè)事宜,按下,擡起,滑動,撤消。
@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:{
			if(header == null){
				header = (TextView) ((View)getParent()).findViewById(R.id.floating_header);
			}
			setHeaderTextAndscroll(event);
			header.setVisibility(View.VISIBLE);
			setBackgroundResource(R.drawable.sidebar_background_pressed);
			return true;
		}
		case MotionEvent.ACTION_MOVE:{
			setHeaderTextAndscroll(event);
			return true;
		}
		case MotionEvent.ACTION_UP:
			header.setVisibility(View.INVISIBLE);
			setBackgroundColor(Color.TRANSPARENT);
			return true;
		case MotionEvent.ACTION_CANCEL:
			header.setVisibility(View.INVISIBLE);
			setBackgroundColor(Color.TRANSPARENT);
			return true;
		}
		return super.onTouchEvent(event);
	}

private void setHeaderTextAndscroll(MotionEvent event){
		 if (mListView == null) {
		        //check the mListView to avoid NPE. but the mListView shouldn't be null
		        //need to check the call stack later
		        return;
		    }
		String headerString = sections[sectionForPoint(event.getY())];
		LogUtil.d(TAG, " event.getY():" + event.getY());
		header.setText(headerString);
		ContactAdapter adapter = (ContactAdapter) mListView.getAdapter();
		String[] adapterSections = (String[]) adapter.getSections();
		try {
			for (int i = adapterSections.length - 1; i > -1; i--) {
				if(adapterSections[i].equals(headerString)){
					mListView.setSelection(adapter.getPositionForSection(i));
					break;
				}
			}
		} catch (Exception e) {
			LogUtil.d(TAG, e.getMessage());
		}
		
	}

相關案例查看更多