滚动时只使用动画一次

时间:2016-07-05 09:11:52

标签: android android-fragments animation android-recyclerview

我在Recycler视图下面有一个片段,当向下滚动时,片段应该消失,当向上滚动时,片段应该重新出现

我实现了它但问题是,当我向下滚动一点时,片段消失的动画发生,当我进一步滚动时,它再次出现并且向下滑动动画,但我希望它保持隐藏状态直到下一个向上滚动

这是我的代码:

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                Log.d("newstate",newState+"");


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
               Log.d("dx",dx+"");
                Log.d("dy",dy+"");

                FragmentTransaction ft1 = getFragmentManager().beginTransaction();
                ft1.setCustomAnimations(R.anim.entry, R.anim.exit);
                ft1.show(bmfragment);
                ft1.commit();
                    visibleItemCount = recyclerView.getChildCount();
                    totalItemCount = mLinearLayoutManager.getItemCount();
                    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();


                if(dy<0) {

                        FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.setCustomAnimations(R.anim.entry, R.anim.exit);
                        ft.show(bmfragment);
                        ft.commit();


                }else if(firstVisibleItem>=1)
                {

                        FragmentTransaction ft = getFragmentManager().beginTransaction();

                        ft.setCustomAnimations(0, R.anim.exit);
                        ft.hide(bmfragment);
                        ft.commit();


                }




            }
        });

1 个答案:

答案 0 :(得分:0)

我明白了!我刚刚使用了一个标志变量

@Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
               Log.d("dx",dx+"");
                Log.d("dy",dy+"");

                if(flag==false) {
                    FragmentTransaction ft1 = getFragmentManager().beginTransaction();
                    ft1.setCustomAnimations(R.anim.entry, R.anim.exit);
                    ft1.show(bmfragment);
                    ft1.commit();
                    visibleItemCount = recyclerView.getChildCount();
                    totalItemCount = mLinearLayoutManager.getItemCount();
                    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
                }


                if(dy<0) {

                        FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.setCustomAnimations(R.anim.entry, R.anim.exit);
                        ft.show(bmfragment);
                        ft.commit();
                        flag = false;

                }else if(firstVisibleItem>=1)
                {
                    if(flag==false) {

                        FragmentTransaction ft = getFragmentManager().beginTransaction();

                        ft.setCustomAnimations(0, R.anim.exit);
                        ft.hide(bmfragment);
                        ft.commit();
                        flag=true;
                    }


                }
                Log.d("visibleItemCount",visibleItemCount+"");
                Log.d("firstVisibleItem",firstVisibleItem+"");




            }
        });