android - 谷歌字母滚动

标签 android

我在 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;
    }

有谁知道如何让显示当前字母的窗口只在用户滚动时出现?目前, Activity 开始后至少需要 3-4 秒才能消失。

最佳答案

你试过换行吗

mHandler.postDelayed(mRemoveWindow, 3000);

类似于

mHandler.postDelayed(mRemoveWindow, 500);

基本上,尝试调整延迟时间。

关于android - 谷歌字母滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545726/

相关文章:

Android O - 旧的启动前台服务仍在工作?

java - 如果soapAction 是URN :something?,我的URL 是什么

php - 推送数据到安卓应用

android - 在 imageview 中设置图像,使其看起来像进度条

android - 如何跟踪移动按钮(不是小部件按钮)点击事件

java - 带有 fragment 和 Activity 的 FragmentActivity TabHost

java - 当您跳转到下一个 Activity 时如何销毁先前的 Activity

android - android中的空指针异常

Android:如何根据屏幕设置标题的高度或宽度

Android studio 自动在 list 中添加代码,如何删除它?