自动显示和隐藏带有动画的 gridview 索引器

auto show & hide gridview indexer with animation

我有一个右侧带有字母索引器的网格视图,默认情况下将其可见性设置为 view.gone。当我开始滚动我的网格视图时,我希望索引器显示动画,当我停止滚动时,索引器也会自动隐藏

 gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

                    mLetter.setVisibility(View.VISIBLE);

            }

            public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Animation animation = new TranslateAnimation(0, 200, 0, 0);
                                animation.setDuration(500);
                                mLetter.startAnimation(animation);
                                mLetter.setVisibility(View.GONE);
                            }
                        }, 5000);

                    }

            }
        });

我的问题是:

  1. 我还没有成功地让索引器显示动画。在上面的代码中,它仅将可见性更改为 view.visible.
  2. 当我多次滚动网格视图时,它会检测到所有触摸并且隐藏动画会 运行 与检测到的触摸一样多。我的意思是如果我 scroll/touch 它 3 次,动画将 运行 3 次。如何避免这种情况?

在网格视图上使用 TouchListener:

  boolean isLetterShowing = false;
  gridView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action){
                case MotionEvent.ACTION_DOWN:
                    if(!isLetterShowing){
                        isLetterShowing = true;
                    }
                    break;
                case MotionEvent.ACTION_SCROLL:
                    if(isLetterShowing){
                        mLetter.setVisibility(View.VISIBLE);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(isLetterShowing){
                        isLetterShowing = false;
                        mLetter.setVisibility(View.INVISIBLE);
                    }
                    break;
            }
            return false;
        }
    });

几小时后我终于找到了自己的答案,其实很简单

gridview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    //when first touch
                    case MotionEvent.ACTION_DOWN:
                        if(mLetter.getVisibility() == View.GONE){ //make sure indexer doesn't exist
                            Animation animation = new TranslateAnimation(100, 0, 0, 0);
                            animation.setDuration(500);
                            mLetter.startAnimation(animation);
                            mLetter.setVisibility(View.VISIBLE);
                        }
                        break;

                    case MotionEvent.ACTION_MOVE:
                        mLetter.setVisibility(View.VISIBLE);
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;
            }
        });



        gridview.setOnScrollListener(new AbsListView.OnScrollListener() {
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

            }

            public void onScrollStateChanged(AbsListView view, int scrollState) {
                state = scrollState;
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    final Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (state == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                                //make sure indexer is exist AND is not currently touched
                                if (mLetter.getVisibility() == View.VISIBLE && !mLetter.getBool()) {
                                    Animation animation = new TranslateAnimation(0, 200, 0, 0);
                                    animation.setDuration(500);
                                    mLetter.startAnimation(animation);
                                    mLetter.setVisibility(View.GONE);

                                }
                            }

                        }
                    }, 5000);


                }

            }
        });

这成功地防止了动画复制。希望这对某人有帮助