91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android仿微信通訊錄滑動快速定位功能

發布時間:2020-10-10 12:17:10 來源:腳本之家 閱讀:232 作者:ChayWong 欄目:移動開發

先給大家展示下效果圖:

Android仿微信通訊錄滑動快速定位功能

實現代碼如下:

下面簡單說下實現原理。

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 List<CompanyEntity> mCompanyList = 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個字母索引TextView,當手指滑動時通知Activity更新界面。

核心是OnTouchListener,手指滑動的時候根據當前Y坐標計算出手指所在的索引位置,要注意臨界值。

以上所述是小編給大家介紹的Android仿微信通訊錄滑動快速定位功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阿拉善左旗| 兴城市| 土默特左旗| 罗江县| 甘洛县| 新安县| 闽侯县| 砚山县| 无为县| 潞西市| 彰化县| 崇阳县| 边坝县| 永年县| 苏尼特左旗| 聂荣县| 噶尔县| 百色市| 泌阳县| 泸州市| 新化县| 星子县| 开化县| 新巴尔虎右旗| 荔浦县| 文登市| 铅山县| 屏南县| 凌海市| 革吉县| 宜昌市| 乌鲁木齐县| 都兰县| 全椒县| 陵川县| 社旗县| 灌南县| 巫山县| 宝坻区| 上蔡县| 正安县|