RecyclerView inside ScrollView is not working

前端 未结 26 2215
梦如初夏
梦如初夏 2020-11-22 05:37

I\'m trying to implement a layout which contains RecyclerView and ScrollView at the same layout.

Layout template:


    

        
26条回答
  •  感动是毒
    2020-11-22 06:25

    I used CustomLayoutManager to disable RecyclerView Scrolling. Also don't use Recycler View as WrapContent, use it as 0dp, Weight=1

    public class CustomLayoutManager extends LinearLayoutManager {
        private boolean isScrollEnabled;
    
        // orientation should be LinearLayoutManager.VERTICAL or HORIZONTAL
        public CustomLayoutManager(Context context, int orientation, boolean isScrollEnabled) {
            super(context, orientation, false);
            this.isScrollEnabled = isScrollEnabled;
        }
    
        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }
    

    Use CustomLayoutManager in RecyclerView:

    CustomLayoutManager mLayoutManager = new CustomLayoutManager(getBaseActivity(), CustomLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(mLayoutManager);
            ((DefaultItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 
            recyclerView.setAdapter(statsAdapter);
    

    UI XML:

    
    
    
    
        
    
            
    
                
    
                
    
    
            
    
            
    
    
            
    
    
        
    
    
    
    

提交回复
热议问题