Search code examples
androidlistviewandroid-scrollview

Scrolling down leads to activate the pull down refresh option


i have attached the Swipe Refresh Layout as a top parent node to offer pull down refresh option to user.

So when i scroll down, i cant able to scroll, automatically the pull down option starts loading.

i have searched some stack overflow question regarding the same issue. they are saying listen the scrolling and disable the pull down option based on the list view current index.

i have tried to listen it. but i cant able listen any event when scrolling down.

my list view object:

    mListView = (ListView) getView().findViewById(android.R.id.list);

can anyone please help me. any other interesting solution or work around.


Solution

  • I did it this way and it's working perfectly:

    class YourActivity extends AppCompatActivity implements ViewTreeObserver.OnScrollChangedListener { 
    @Override
    protected void onStart() {
        super.onStart();
        mListView.getViewTreeObserver().addOnScrollChangedListener(this);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
          mListView.getViewTreeObserver().removeOnScrollChangedListener(this);
    }
    
    
    @Override
    public void onScrollChanged() {
        if (mListView.getFirstVisiblePosition() == 0) {
            swipeRefresh.setEnabled(true);
        } else {
            swipeRefresh.setEnabled(false);
        }
    }
    }
    

    Hope this will help.