2012-09-22 31 views
3

我已經看到了Android API在下面的例子:谷歌字母滾動

/* 
    * Copyright (C) 2007 The Android Open Source Project 
    * 
    * Licensed under the Apache License, Version 2.0 (the "License"); 
    * you may not use this file except in compliance with the License. 
    * You may obtain a copy of the License at 
    * 
    *  http://www.apache.org/licenses/LICENSE-2.0 
    * 
    * Unless required by applicable law or agreed to in writing, software 
    * distributed under the License is distributed on an "AS IS" BASIS, 
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    * See the License for the specific language governing permissions and 
    * limitations under the License. 
    */ 

    package com.example.android.apis.view; 

    import com.example.android.apis.R; 

    import android.app.ListActivity; 
    import android.content.Context; 
    import android.graphics.PixelFormat; 
    import android.os.Bundle; 
    import android.os.Handler; 
    import android.view.View; 
    import android.view.LayoutInflater; 
    import android.view.WindowManager; 
    import android.view.WindowManager.LayoutParams; 
    import android.widget.AbsListView; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.TextView; 

/** 
* Another variation of the list of cheeses. In this case, we use 
* {@link AbsListView#setOnScrollListener(AbsListView.OnScrollListener) 
* AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the 
* first letter of the visible range of cheeses. 
*/ 
public class List9 extends ListActivity implements ListView.OnScrollListener { 

    private final class RemoveWindow implements Runnable { 
     public void run() { 
      removeWindow(); 
     } 
    } 

    private RemoveWindow mRemoveWindow = new RemoveWindow(); 
    Handler mHandler = new Handler(); 
    private WindowManager mWindowManager; 
    private TextView mDialogText; 
    private boolean mShowing; 
    private boolean mReady; 
    private char mPrevLetter = Character.MIN_VALUE; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE); 

     // Use an existing ListAdapter that will map an array 
     // of strings to TextViews 
     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, mStrings)); 

     getListView().setOnScrollListener(this); 

     LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     mDialogText = (TextView) inflate.inflate(R.layout.list_position, null); 
     mDialogText.setVisibility(View.INVISIBLE); 

     mHandler.post(new Runnable() { 

      public void run() { 
       mReady = true; 
       WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
         WindowManager.LayoutParams.TYPE_APPLICATION, 
         WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
           | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
         PixelFormat.TRANSLUCENT); 
       mWindowManager.addView(mDialogText, lp); 
      }}); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mReady = true; 
    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     removeWindow(); 
     mReady = false; 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     mWindowManager.removeView(mDialogText); 
     mReady = false; 
    } 




    public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 
     if (mReady) { 
      char firstLetter = mStrings[firstVisibleItem].charAt(0); 

      if (!mShowing && firstLetter != mPrevLetter) { 

       mShowing = true; 
       mDialogText.setVisibility(View.VISIBLE); 
      } 
      mDialogText.setText(((Character)firstLetter).toString()); 
      mHandler.removeCallbacks(mRemoveWindow); 
      mHandler.postDelayed(mRemoveWindow, 3000); 
      mPrevLetter = firstLetter; 
     } 
    } 


    public void onScrollStateChanged(AbsListView view, int scrollState) { 
    } 


    private void removeWindow() { 
     if (mShowing) { 
      mShowing = false; 
      mDialogText.setVisibility(View.INVISIBLE); 
     } 
    } 

    private String[] mStrings = Cheeses.sCheeseStrings; 
    } 

有誰知道如何使窗口顯示只出現當用戶滾動當前信?此時,活動開始後至少需要3-4秒才能消失。

回答

1

您是否嘗試過

mHandler.postDelayed(mRemoveWindow, 3000); 

行更改爲類似

mHandler.postDelayed(mRemoveWindow, 500); 

基本上,嘗試和玩的延遲時間。

+0

這改變了它出現的時間,但顯示在開始時仍然存在。無論如何,這將做。謝謝。 – user557240

+0

如果將其更改爲mHandler.postDelayed(mRemoveWindow,0);它在開始時消失。 – user557240