android 按字母搜尋

來源:互聯網
上載者:User

在看Oplayer的時候看見滑動字母來實現listView的內容搜尋,所以就把裡面的核心的函數扣除來做了一個demo,分為兩部分一個是布局,另一個就是代碼了,具體的如下: 布局:  [html] <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:layout_weight="1.0"      android:orientation="horizontal" >        <FrameLayout          android:layout_width="0dip"          android:layout_height="fill_parent"          android:layout_weight="1.0"          android:orientation="vertical" >            <ListView              android:id="@+id/list"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:cacheColorHint="@android:color/transparent"              android:divider="@drawable/ic_line"              android:drawSelectorOnTop="false"              android:fadingEdge="none"              android:listSelector="@android:color/transparent"              android:scrollbars="none" />            <TextView              android:id="@+id/first_letter_overlay"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_gravity="right"              android:layout_marginRight="50.0dip"              android:layout_marginTop="49.0dip"              android:background="@drawable/fast_scroller_overlay"              android:gravity="center"              android:paddingBottom="5.0dip"              android:scaleType="center"              android:textSize="43.0dip"              android:visibility="gone" />      </FrameLayout>        <ImageView          android:id="@+id/alphabet_scroller"          android:layout_width="wrap_content"          android:layout_height="match_parent"          android:layout_gravity="center"          android:paddingRight="0dip"          android:scaleType="fitXY"          android:src="@drawable/alphabet_scroller_bg" />    </LinearLayout>   <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_weight="1.0"    android:orientation="horizontal" >     <FrameLayout        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1.0"        android:orientation="vertical" >         <ListView            android:id="@+id/list"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:cacheColorHint="@android:color/transparent"            android:divider="@drawable/ic_line"            android:drawSelectorOnTop="false"            android:fadingEdge="none"            android:listSelector="@android:color/transparent"            android:scrollbars="none" />         <TextView            android:id="@+id/first_letter_overlay"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:layout_marginRight="50.0dip"            android:layout_marginTop="49.0dip"            android:background="@drawable/fast_scroller_overlay"            android:gravity="center"            android:paddingBottom="5.0dip"            android:scaleType="center"            android:textSize="43.0dip"            android:visibility="gone" />    </FrameLayout>     <ImageView        android:id="@+id/alphabet_scroller"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_gravity="center"        android:paddingRight="0dip"        android:scaleType="fitXY"        android:src="@drawable/alphabet_scroller_bg" /> </LinearLayout>   裡面涉及到了一個ImageView的圖片  [html]<?xml version="1.0" encoding="UTF-8"?>  <selector    xmlns:android="http://schemas.android.com/apk/res/android">      <item android:state_pressed="true" android:drawable="@drawable/contact_list_scroll_pressed" />      <item android:drawable="@drawable/contact_list_scroll_normal" />  </selector>   <?xml version="1.0" encoding="UTF-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@drawable/contact_list_scroll_pressed" />    <item android:drawable="@drawable/contact_list_scroll_normal" /></selector>具體的代碼如下:  [java] package com.example.test;    import java.util.ArrayList;  import java.util.List;    import android.app.Activity;  import android.os.Bundle;  import android.util.Log;  import android.view.MotionEvent;  import android.view.View;  import android.view.ViewGroup;  import android.view.View.OnTouchListener;  import android.widget.BaseAdapter;  import android.widget.ImageView;  import android.widget.ListView;  import android.widget.TextView;    public class Main6 extends Activity{        ListView iListView;      TextView showChar;      ImageView myChar;      List<String> iList;      MAdapter mAdapter;      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main5);          iListView = (ListView)findViewById(R.id.list);          showChar = (TextView)findViewById(R.id.first_letter_overlay);          myChar = (ImageView)findViewById(R.id.alphabet_scroller);          iList = initChar();          Log.e("t", " "+iList.size());          mAdapter = new MAdapter();          iListView.setAdapter(mAdapter);          myChar.setClickable(true);          myChar.setOnTouchListener(asOnTouch);      }            /**      * A-Z      */      private OnTouchListener asOnTouch = new OnTouchListener() {            @Override          public boolean onTouch(View v, MotionEvent event) {              switch (event.getAction()) {              case MotionEvent.ACTION_DOWN:// 0                   myChar.setPressed(true);                  showChar.setVisibility(View.VISIBLE);                  mathScrollerPosition(event.getY());                  break;              case MotionEvent.ACTION_UP:// 1                   myChar.setPressed(false);                  showChar.setVisibility(View.GONE);                  break;              case MotionEvent.ACTION_MOVE:                  mathScrollerPosition(event.getY());                  break;              }              return false;          }      };      /**      * 顯示字元      *       * @param y      */      private void mathScrollerPosition(float y) {          int height = myChar.getHeight();          float charHeight = height / 28.0f;          char c = 'A';          if (y < 0)              y = 0;          else if (y > height)              y = height;            int index = (int) (y / charHeight) - 1;          if (index < 0)              index = 0;          else if (index > 25)              index = 25;            String key = String.valueOf((char) (c + index));          showChar.setText(key);            int position = 0;          if (index == 0)              iListView.setSelection(0);          else if (index == 25)              iListView.setSelection(mAdapter.getCount() - 1);          else {              if (mAdapter != null) {                  for (int i = 0; i < iList.size(); i++) {                      if (iList.get(i).startsWith(key)) {                          iListView.setSelection(position);                          break;                      }                      position++;                  }              }          }      }      /**      * ListView的資料      * @return      */      public List<String> initChar(){          List<String> list = new ArrayList<String>();          char c = 'A';          for (int i = 0; i < 26; i++) {              list.add(String.valueOf((char) (c + i)));              list.add(String.valueOf((char) (c + i)));              list.add(String.valueOf((char) (c + i)));          }          return list;      }      class MAdapter extends BaseAdapter{            @Override          public int getCount() {              return iList.size();          }            @Override          public Object getItem(int position) {              return iList.get(position);          }            @Override          public long getItemId(int position) {              return position;          }            @Override          public View getView(int position, View convertView, ViewGroup parent) {              TextView text = new TextView(getApplicationContext());              text.setText(iList.get(position));              return text;          }                }  }   package com.example.test; import java.util.ArrayList;import java.util.List; import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.View.OnTouchListener;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView; public class Main6 extends Activity{ ListView iListView;TextView showChar;ImageView myChar;List<String> iList;MAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main5);iListView = (ListView)findViewById(R.id.list);showChar = (TextView)findViewById(R.id.first_letter_overlay);myChar = (ImageView)findViewById(R.id.alphabet_scroller);iList = initChar();Log.e("t", " "+iList.size());mAdapter = new MAdapter();iListView.setAdapter(mAdapter);myChar.setClickable(true);myChar.setOnTouchListener(asOnTouch);} /*** A-Z*/private OnTouchListener asOnTouch = new OnTouchListener() { @Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 0myChar.setPressed(true);showChar.setVisibility(View.VISIBLE);mathScrollerPosition(event.getY());break;case MotionEvent.ACTION_UP:// 1myChar.setPressed(false);showChar.setVisibility(View.GONE);break;case MotionEvent.ACTION_MOVE:mathScrollerPosition(event.getY());break;}return false;}};/*** 顯示字元* * @param y*/private void mathScrollerPosition(float y) {int height = myChar.getHeight();float charHeight = height / 28.0f;char c = 'A';if (y < 0)y = 0;else if (y > height)y = height; int index = (int) (y / charHeight) - 1;if (index < 0)index = 0;else if (index > 25)index = 25; String key = String.valueOf((char) (c + index));showChar.setText(key); int position = 0;if (index == 0)iListView.setSelection(0);else if (index == 25)iListView.setSelection(mAdapter.getCount() - 1);else {if (mAdapter != null) {for (int i = 0; i < iList.size(); i++) {if (iList.get(i).startsWith(key)) {iListView.setSelection(position);break;}position++;}}}}/*** ListView的資料* @return*/public List<String> initChar(){List<String> list = new ArrayList<String>();char c = 'A';for (int i = 0; i < 26; i++) {list.add(String.valueOf((char) (c + i)));list.add(String.valueOf((char) (c + i)));list.add(String.valueOf((char) (c + i)));}return list;}class MAdapter extends BaseAdapter{ @Overridepublic int getCount() {return iList.size();} @Overridepublic Object getItem(int position) {return iList.get(position);} @Overridepublic long getItemId(int position) {return position;} @Overridepublic View getView(int position, View convertView, ViewGroup parent) {TextView text = new TextView(getApplicationContext());text.setText(iList.get(position));return text;} }} 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.