Android: CollapsingToolbarLayout and SwipeRefreshLayout get stuck

前端 未结 5 1883
别跟我提以往
别跟我提以往 2020-11-27 10:42

I use CollapsingToolbarLayout, RecyclerView and SwipeRefreshLayout together:

Xml:



        
5条回答
  •  借酒劲吻你
    2020-11-27 11:14

    Update: This issue has been resolved in the latest version of the support library (23.1.1+). If you are using an older version of the support library either upgrade or continue reading.

    If you're using an older version of the support library, add an offset change listener to your AppBarLayout to enable or disable your swipe to refresh layout accordingly. Additional code available here:

    https://gist.github.com/blackcj/001a90c7775765ad5212

    Relevant changes:

    public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
        ...
    
        private AppBarLayout appBarLayout;
        private SwipeRefreshLayout mSwipeRefreshLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ...
            mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.contentView);
            appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
    
        }
    
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
            //The Refresh must be only active when the offset is zero :
            mSwipeRefreshLayout.setEnabled(i == 0);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            appBarLayout.addOnOffsetChangedListener(this);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            appBarLayout.removeOnOffsetChangedListener(this);
        }
    }
    

提交回复
热议问题