先給大家展示下效果圖:
十年的海曙網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整海曙建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“海曙網(wǎng)站設(shè)計(jì)”,“海曙網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
實(shí)現(xiàn)代碼如下:
下面簡單說下實(shí)現(xiàn)原理。
public class IndexBar extends LinearLayout implements View.OnTouchListener { private static final String[] INDEXES = 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 static final int TOUCHED_BACKGROUND_COLOR = 0x40000000; private OnIndexChangedListener mListener; public void setOnIndexChangedListener(OnIndexChangedListener listener) { mListener = listener; } public IndexBar(Context context) { this(context, null); } public IndexBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public IndexBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.IndexBar); float indexTextSize = ta.getDimension(R.styleable.IndexBar_indexTextSize, Utils.sp2px(getContext(), 12)); int indexTextColor = ta.getColor(R.styleable.IndexBar_indexTextColor, 0xFF616161); ta.recycle(); setOrientation(VERTICAL); setOnTouchListener(this); for (String index : INDEXES) { TextView text = new TextView(getContext()); text.setText(index); text.setTextSize(TypedValue.COMPLEX_UNIT_PX, indexTextSize); text.setTextColor(indexTextColor); text.setGravity(Gravity.CENTER); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1); text.setLayoutParams(params); addView(text); } } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: setBackgroundColor(TOUCHED_BACKGROUND_COLOR); handle(v, event); return true; case MotionEvent.ACTION_MOVE: handle(v, event); return true; case MotionEvent.ACTION_UP: setBackgroundColor(Color.TRANSPARENT); handle(v, event); return true; } return super.onTouchEvent(event); } private void handle(View v, MotionEvent event) { int y = (int) event.getY(); int height = v.getHeight(); int position = INDEXES.length * y / height; if (position < 0) { position = 0; } else if (position >= INDEXES.length) { position = INDEXES.length - 1; } String index = INDEXES[position]; boolean showIndicator = event.getAction() != MotionEvent.ACTION_UP; if (mListener != null) { mListener.onIndexChanged(index, showIndicator); } } public interface OnIndexChangedListener { void onIndexChanged(String index, boolean showIndicator); } }
使用
public class CompanyActivity extends BaseActivity implements IndexBar.OnIndexChangedListener { @Bind(R.id.lv_company) ListView lvCompany; @Bind(R.id.ib_indicator) IndexBar ibIndicator; @Bind(R.id.tv_indicator) TextView tvIndicator; private ListmCompanyList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_company); // ... } @Override public void onIndexChanged(String index, boolean showIndicator) { int position = -1; for (CompanyEntity company : mCompanyList) { if (TextUtils.equals(company.getName(), index)) { position = mCompanyList.indexOf(company); break; } } if (position != -1) { lvCompany.setSelection(position); } tvIndicator.setText(index); tvIndicator.setVisibility(showIndicator ? View.VISIBLE : View.GONE); } }
繼承自LinearLayout,添加了26個(gè)字母索引TextView,當(dāng)手指滑動(dòng)時(shí)通知Activity更新界面。
核心是OnTouchListener,手指滑動(dòng)的時(shí)候根據(jù)當(dāng)前Y坐標(biāo)計(jì)算出手指所在的索引位置,要注意臨界值。
以上所述是小編給大家介紹的Android仿微信通訊錄滑動(dòng)快速定位功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!