字母索引查詢ListView元件
當點選或者在字母上上下移動時的畫面
package com.alphabet.widget;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* 右邊帶有字母查詢的ListView
* @author Davee
*/
public class AlphabetListView extends FrameLayout {
private Context mContext;
private ListView mListView;
private LinearLayout alphabetLayout;
private TextView mTextView;
private AlphabetPositionListener positionListener;
private float screenDensity;
private Handler mHandler;
private HideIndicator mHideIndicator = new HideIndicator();
private int indicatorDuration = 1000;
public void setIndicatorDuration(int duration) {
this.indicatorDuration = duration;
}
private final class HideIndicator implements Runnable {
@Override
public void run() {
mTextView.setVisibility(View.INVISIBLE);
}
}
public AlphabetListView(Context context) {
super(context);
init(context);
}
public AlphabetListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mContext = context;
screenDensity = context.getResources().getDisplayMetrics().density;
mHandler = new Handler();
mListView = new ListView(mContext);
initAlphabetLayout(mContext);
mTextView = new TextView(mContext);
mTextView.setTextSize(convertDIP2PX(50));
mTextView.setTextColor(Color.argb(150, 255, 255, 255));
mTextView.setBackgroundColor(Color.argb(200, 0, 0, 0));
mTextView.setMinWidth(convertDIP2PX(70));
mTextView.setMinHeight(convertDIP2PX(70));
int pixels = convertDIP2PX(10);
mTextView.setPadding(pixels, pixels, pixels, pixels);
mTextView.setGravity(Gravity.CENTER);
mTextView.setVisibility(View.INVISIBLE);
FrameLayout.LayoutParams textLayoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
textLayoutParams.gravity = Gravity.CENTER;
// textLayoutParams.rightMargin = convertDIP2PX(10);
mTextView.setLayoutParams(textLayoutParams);
}
public void setAdapter(ListAdapter adapter, AlphabetPositionListener positionListener) {
if (positionListener == null)
throw new IllegalArgumentException("AlphabetPositionListener is required");
mListView.setAdapter(adapter);
this.positionListener = positionListener;
this.addView(mListView);
this.addView(alphabetLayout);
this.addView(mTextView);
}
private void initAlphabetLayout(Context context) {
//建立字母佈局
alphabetLayout = new LinearLayout(context);
alphabetLayout.setOrientation(LinearLayout.VERTICAL);
FrameLayout.LayoutParams alphabetLayoutParams = new FrameLayout.LayoutParams(
convertDIP2PX(30),
ViewGroup.LayoutParams.FILL_PARENT);
alphabetLayoutParams.gravity = Gravity.RIGHT|Gravity.CENTER_VERTICAL;
alphabetLayoutParams.rightMargin = convertDIP2PX(10);
alphabetLayout.setLayoutParams(alphabetLayoutParams);
final String[] alphabet = new String[]{"A","B","C","D","E","F","G","H","I","J"
,"K","L","M","N","O","P","Q","L","S","T","U","V","W","X","Y","Z"};
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT
,LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1;
params.gravity = Gravity.CENTER_HORIZONTAL;
for (int i=0, count=alphabet.length; i < count; i++) {
TextView textView = new TextView(context);
textView.setTextColor(Color.argb(150, 150, 150, 150));
textView.setTextSize(convertDIP2PX(10));
textView.setText(alphabet[i]);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(params);
textView.setTag(i+1);
alphabetLayout.addView(textView);
}
alphabetLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
alphabetLayout.setBackgroundColor(Color.argb(50, 100, 200, 100));
int l = (int)(event.getY()/(alphabetLayout.getHeight()/26));
if (l > 25) l = 25;
else if (l < 0) l = 0;
int pos = positionListener.getPosition(alphabet[l]);
if (pos != -1) {
mTextView.setText(alphabet[l]);
mTextView.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(mHideIndicator);
mHandler.postDelayed(mHideIndicator, indicatorDuration);
//mListView.requestFocusFromTouch();
mListView.setSelection(pos);
}
break;
case MotionEvent.ACTION_MOVE:
l = (int)((event.getY()+alphabetLayout.getHeight()/26/2)/(alphabetLayout.getHeight()/26));
if (l > 25) l = 25;
else if (l < 0) l = 0;
pos = positionListener.getPosition(alphabet[l]);
if (pos != -1) {
mTextView.setText(alphabet[l]);
mTextView.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(mHideIndicator);
mHandler.postDelayed(mHideIndicator, indicatorDuration);
//mListView.requestFocusFromTouch();
mListView.setSelection(pos);
}
break;
case MotionEvent.ACTION_UP:
alphabetLayout.setBackgroundResource(0);
break;
}
return true;
}
});
}
public int convertDIP2PX(float dip) {
return (int)(dip*screenDensity + 0.5f*(dip>=0?1:-1));
}
public static interface AlphabetPositionListener {
public static final int UNKNOW = -1;
public int getPosition(String letter);
}
}
相關文章
- 【索引】Oracle查詢指定索引提高查詢效率索引Oracle
- 字母表;及查詢提示分析
- elasticsearch之多索引查詢Elasticsearch索引
- Elasticsearch(三):索引查詢Elasticsearch索引
- 查詢索引 常用SQL索引SQL
- 查詢相似的索引索引
- 【索引】反向索引--條件 範圍查詢索引
- MongoDB慢查詢與索引MongoDB索引
- mysql查詢索引結構MySql索引
- 反向索引與模糊查詢索引
- 【索引】反向索引--條件 範圍查詢(二)索引
- MySQL索引與查詢優化MySql索引優化
- (利用索引)大資料查詢索引大資料
- 根據表查詢索引資訊索引
- cassandra的索引查詢和排序索引排序
- 全文索引和查詢概念索引
- 表和索引並行查詢索引並行
- 理解索引(中):MySQL查詢過程和高階查詢索引MySql
- 【索引】oracle查詢使用索引和不使用索引的比較索引Oracle
- 索引監控-查詢從未被使用過的索引索引
- ElasticSearch分片互動過程(建立索引、刪除索引、查詢索引)Elasticsearch索引
- indexedDB 通過索引查詢資料Index索引
- mongodb索引及查詢優化分析MongoDB索引優化
- 走索引掃描的慢查詢索引
- 查詢某個表的索引資訊索引
- 為何在查詢中索引未被使用索引
- MySQL 覆蓋索引、回表查詢MySql索引
- pga/sga及元件值查詢元件
- 查詢oracle各元件的版本Oracle元件
- MySQL查詢某個欄位含有字母數字的值MySql
- MySQL 索引及查詢優化總結MySql索引優化
- MySQL 學習之索引篇和查詢MySql索引
- 索引為什麼能提供查詢效能...索引
- SQLServer查詢哪些索引利用率低SQLServer索引
- 【最佳化】模糊查詢索引問題索引
- 一個查詢不走索引的例子索引
- MySQL索引原理及慢查詢優化MySql索引優化
- 【oracle 效能優化】組合索引查詢。Oracle優化索引