Friday, 22 February 2019

How to use addOnScrollListener with different lists in one activity

In my app there's two methods getData and getItemsByLabel each one is getting different list on other, and I used navigation drawer method onNavigationItemSelected so whenever a user clicks on a particular item, a different list is displayed in RecyclerView, the problem is that I use a method addOnScrollListener to detected scroll behavior from any list on recyclerView which causes the items overlapping in the displayed list

onNavigationItemSelected Method

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.

    switch (item.getItemId()) {
        case R.id.home:
            toolbar.setTitle(R.string.home);
            if (!items.isEmpty()) {
                items.clear();
            }
            getData();
            break;
        case R.id.articles:
            toolbar.setTitle(R.string.articles);
            getItemsByLabel("مقالات");
            break;
        case R.id.windows:
            toolbar.setTitle(R.string.windows);
            getItemsByLabel("ويندوز");
            break;
        case R.id.linux:
            toolbar.setTitle(R.string.linux);
            getItemsByLabel("لينكس");
            break;
        case R.id.android:
            toolbar.setTitle(R.string.android);
            getItemsByLabel("أندرويد");
            break;
        case R.id.miscellaneous_devices:
            toolbar.setTitle(R.string.miscellaneous_devices);
            getItemsByLabel("\"أجهزة متنوعة\"");
            break;
        case R.id.information_security:
            toolbar.setTitle(R.string.information_security);
            getItemsByLabel("\"أمن المعلومات\"");
            break;
        case R.id.facebook:
            toolbar.setTitle(R.string.facebook);
            getItemsByLabel("فيسبوك");
            break;
    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

recyclerView.addOnScrollListener method

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                isScrolling = true;
                if (!recyclerView.canScrollVertically(1)) {
                    progressBar.setVisibility(View.GONE);
                }
            }

        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                currentItems = manager.getChildCount();
                totalItems = manager.getItemCount();
                scrollOutItems = manager.findFirstVisibleItemPosition();
                if (isScrolling && (currentItems + scrollOutItems == totalItems)) {
                    isScrolling = false;
                    for (int i = 1; i < 7; i++) {
                        if (navigationView.getMenu().getItem(i).isChecked()) {
                            getItemsByLabel(navigationView.getMenu().getItem(i).getTitle().toString());
                        } else {
                            getData();
                        }
                    }
                }
            }
        }
    });

Methods that I tried to solve this problem :
1. implementing ScrollListener inside each method getData and getItemsByLabel
2. creating fragment for each items on navigation drawer menu and implement ScrollListener on it
3. and the last I put for loop on onScrolled method to detect which item on drawer menu is checked to get the list it's own, but unfortunately no one is working.



from How to use addOnScrollListener with different lists in one activity

No comments:

Post a Comment