3

I am creating an app where on scrolling more data is received from the server using RecyclerView and CardView.

I set min sdk to 19, and i need it to be 19 only, but when in my main class, i am using --

public class FunnyMessageList extends AppCompatActivity implements RecyclerView.OnScrollChangeListener{


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mlistview);

recyclerView.setOnScrollChangeListener(this);
}
}

Here i am getting error in these 2 lines --

implements RecyclerView.OnScrollChangeListener
recyclerView.setOnScrollChangeListener(this);

The error is -- Call requires API level 23 (current min is 19): android.view.View#setOnScrollChangeListener

How to resolve, any hint.

Nayan Das
  • 75
  • 7

4 Answers4

1

The method setOnScrollChangeListener is introduce in API 23. So you can only access it with Build version check.

To load more data you can use addOnScrollListener() method instead .

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

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    });
ADM
  • 20,406
  • 11
  • 52
  • 83
1

Try like this,

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int totalItemCount = layoutManager.getItemCount();
            int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            if (!isLoading && totalItemCount <= lastVisibleItem + 1/*When to load*/) {
                // End has been reached

                    // put your logic here
                    isLoading = true;

            }
        }
    })
Bhavesh Desai
  • 753
  • 4
  • 20
0

The method View.OnScrollChangeListener has been added on API 23. There is no way to access it before that. You could check if you are on API 23 and above before setting the scroll change listener to avoid crashes.

Something like this:

if (android.os.Build.VERSION.SDK_INT >= 23) {
    recyclerView.setOnScrollChangeListener(this);
}
else {
    // handle API bellow 23
}

Still I don't think you need to use the method above to reach your goal. You have some other options:

Option 1:

You could use addOnScrollListener. Like explained in this answer.

Option 2:

If you want to use load more data on demand, have a look at Paging Library. It's a new component that makes easy loading data on demand.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Natan
  • 1,867
  • 13
  • 24
0

Do this-:

Change your target sdkVersion to 23 if you want to do this task.

targetSdkVersion 23
compileSdkVersion 23

in your build.gradle
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18