车展&用动画隐藏gridview索引器

时间:2016-01-04 09:29:07

标签: android animation gridview onscrolllistener

我在右侧有一个带字母索引器的gridview,默认情况下将其可见性设置为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. 当我多次滚动网格视图时,它会检测所有触摸,并且隐藏动画将运行许多与检测到的触摸一样多的时间。我的意思是如果我滚动/触摸它3次,动画将运行3次。怎么避免这个?

2 个答案:

答案 0 :(得分:2)

在网格视图上使用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;
        }
    });

答案 1 :(得分:1)

下班后我终于找到了自己的答案,实际上非常简单

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);


                }

            }
        });

这可以成功阻止动画复制。希望这有帮助的人