3

下面我有回收站視圖中的片段,當向下滾動,該片段應消失,當向上滾動,該片段應重新出現使用動畫只有一次上滾動

我實現它,但問題是,當我向下滾動很少,碎片消失的動畫發生,當我進一步滾動時,它會重新出現,並且會出現向下滑動動畫,但是我希望它會保持隱藏狀態,直到下一個向上滾動。

這裏是我的代碼:

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


       } 




      } 
     }); 

回答

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




      } 
     });