Android開原始碼解讀のOnScrollListener實現ListActivity滾屏首字母提示

來源:互聯網
上載者:User

OnScrollListener介面是定義在AbsListView中的,而AbsListView的直接子類有GridView和ListView,非直接子類有ExpandableListView。OnScrollListener的完整路徑是frameworks\base\core\java\android\widget\AbsListView.java,代碼如下:

/**<br /> * Interface definition for a callback to be invoked when the list or grid<br /> * has been scrolled.<br /> */<br />public interface OnScrollListener {</p><p> /**<br /> * The view is not scrolling. Note navigating the list using the trackball counts as<br /> * being in the idle state since these transitions are not animated.<br /> */<br /> public static int SCROLL_STATE_IDLE = 0;</p><p> /**<br /> * The user is scrolling using touch, and their finger is still on the screen<br /> */<br /> public static int SCROLL_STATE_TOUCH_SCROLL = 1;</p><p> /**<br /> * The user had previously been scrolling using touch and had performed a fling. The<br /> * animation is now coasting to a stop<br /> */<br /> public static int SCROLL_STATE_FLING = 2;</p><p> /**<br /> * Callback method to be invoked while the list view or grid view is being scrolled. If the<br /> * view is being scrolled, this method will be called before the next frame of the scroll is<br /> * rendered. In particular, it will be called before any calls to<br /> * {@link Adapter#getView(int, View, ViewGroup)}.<br /> *<br /> * @param view The view whose scroll state is being reported<br /> *<br /> * @param scrollState The current scroll state. One of {@link #SCROLL_STATE_IDLE},<br /> * {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.<br /> */<br /> public void onScrollStateChanged(AbsListView view, int scrollState);</p><p> /**<br /> * Callback method to be invoked when the list or grid has been scrolled. This will be<br /> * called after the scroll has completed<br /> * @param view The view whose scroll state is being reported<br /> * @param firstVisibleItem the index of the first visible cell (ignore if<br /> * visibleItemCount == 0)<br /> * @param visibleItemCount the number of visible cells<br /> * @param totalItemCount the number of items in the list adaptor<br /> */<br /> public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,<br /> int totalItemCount);<br />}該介面定義了三種滾動狀態和兩個回呼函數,下面的代碼使用onScroll來實現滾屏時提示內容首字母功能,當然,重點在例子的實現分析上面,並不局限於onScroll的使用。直接先看MainActivity的實現吧:(代碼來自APIDemos,有修改)

package hust.iprai.asce1885;</p><p>import android.app.ListActivity;<br />import android.content.Context;<br />import android.graphics.PixelFormat;<br />import android.os.Bundle;<br />import android.os.Handler;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.WindowManager;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.AbsListView;<br />import android.widget.AbsListView.OnScrollListener;<br />import android.widget.ArrayAdapter;<br />import android.widget.TextView;</p><p>/**<br /> * 直接繼承ListActivity,免去了ListView的繼承,同時實現OnScrollListener介面<br /> *<br /> */<br />public class MainActivity extends ListActivity implements OnScrollListener {</p><p>private final class RemoveWindow implements Runnable {</p><p>public void run() {<br />removeWindow();<br />}</p><p>}</p><p>private RemoveWindow mRemoveWindow = new RemoveWindow();<br />Handler mHandler = new Handler();<br />private WindowManager mWindowManager;<br />private TextView mDialogText;<br />private boolean mShowing; //是否顯示mDialogText<br />private boolean mReady; //mDialogText是否已準備好<br />private char mPrevLetter = Character.MIN_VALUE;</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> //擷取Window視窗管理服務<br /> mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);</p><p> //使用ArrayAdapter將字串數組中的資料對應到TextView中<br /> setListAdapter(new ArrayAdapter<String>(this,<br /> android.R.layout.simple_list_item_1, mStrings));</p><p> //設定滾動監聽器ScrossListener<br /> getListView().setOnScrollListener(this);</p><p> //擷取LayoutInflater服務<br /> LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);</p><p> //從xml檔案產生TextView對象<br /> mDialogText = (TextView) inflate.inflate(R.layout.list_position, null);<br /> mDialogText.setVisibility(View.INVISIBLE);</p><p> //將Runnable添加到訊息佇列中<br /> mHandler.post(new Runnable() {</p><p>public void run() {<br />mReady = true;<br />//設定mDialogText的WindowManager.LayoutParams參數<br />WindowManager.LayoutParams lp = new WindowManager.LayoutParams(<br />LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,<br />WindowManager.LayoutParams.TYPE_APPLICATION,<br />WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE<br />| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,<br />PixelFormat.TRANSLUCENT);<br />mWindowManager.addView(mDialogText, lp);<br />}</p><p> });</p><p> }</p><p> @Override<br />protected void onDestroy() {<br />super.onDestroy();<br />mWindowManager.removeView(mDialogText);<br />mReady = false;<br />}</p><p>@Override<br />protected void onPause() {<br />super.onPause();<br />removeWindow();<br />mReady = false;<br />}</p><p>@Override<br />protected void onResume() {<br />super.onResume();<br />mReady = true;<br />}</p><p> //OnScrollListener的回呼函數<br />public void onScroll(AbsListView view, int firstVisibleItem,<br />int visibleItemCount, int totalItemCount) {<br />if (mReady) {<br />//取得當前ListView中第一項內容的首字母<br />char firstLetter = mStrings[firstVisibleItem].charAt(0);</p><p>//當mDialogText還不可見且當前首字母於前次滾動後首字母不一致時<br />//將mDialogText設定為可見<br />if (!mShowing && firstLetter != mPrevLetter) {<br />mShowing = true;<br />mDialogText.setVisibility(View.VISIBLE);<br />}</p><p>//將首字母顯示出來<br />mDialogText.setText(((Character)firstLetter).toString());<br />//將訊息佇列中還在等待post的mRemoveWindow清除<br />mHandler.removeCallbacks(mRemoveWindow);<br />//將Runnable mRemoveWindow添加到訊息佇列中,並延遲3s後運行<br />//實現的功能就是在mDialogText顯示3s後,將它設定為不可見<br />mHandler.postDelayed(mRemoveWindow, 3000);<br />mPrevLetter = firstLetter;<br />}<br />}</p><p>//OnScrollListener的回呼函數<br />public void onScrollStateChanged(AbsListView view, int scrollState) {</p><p>}</p><p>//將mDialogText設定為不可見<br />private void removeWindow() {<br />if (mShowing) {<br />mShowing = false;<br />mDialogText.setVisibility(View.INVISIBLE);<br />}<br />}</p><p>//用於顯示的類比資料<br />private String[] mStrings = Cheeses.sCheeseStrings;<br />}mDialogText的布局檔案list_position.xml如下:<?xml version="1.0" encoding="utf-8"?><br /><TextView xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:textSize="50sp"<br /> android:textColor="#99FFFFFF"<br /> android:typeface="monospace"<br /> android:background="#BB000000"<br /> android:minWidth="70dip"<br /> android:maxWidth="70dip"<br /> android:padding="10dip"<br /> android:gravity="center"<br />/>程式運行結果如所示:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.