如何在一个活动中将 addOnScrollListener 用于不同的列表

在我的应用程序中,有两种方法和.每个列表都通过改造回调方法获得不同的列表,我使用导航抽屉方法,以便每当用户单击特定项目时,都会在 .getDatagetItemsByLabelonNavigationItemSelectedRecyclerView

问题是,我使用一种方法来检测任何列表中的滚动行为,这些列表导致显示列表中的项目重叠。addOnScrollListenerRecyclerView

因此,问题是当向下滚动时,主列表中的项目和所选类别/项目的列表是重叠的。

这是我的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Fabric.with(this, new Crashlytics());

    // Crashlytics.logException(new Exception("My first Android non-fatal error"));
    // I'm also creating a log message, which we'll look at in more detail later
    // Crashlytics.log("MainActivity started");

    swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
    recyclerView = findViewById(R.id.recyclerView);
    manager = new LinearLayoutManager(this);
    emptyView = (TextView) findViewById(R.id.empty_view);
    progressBar = findViewById(R.id.spin_kit);
    adapter = new PostAdapter(this, items);
    recyclerView.setLayoutManager(manager);
    recyclerView.setAdapter(adapter);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle(R.string.home);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.getMenu().getItem(0).setChecked(true);
    navigationView.setNavigationItemSelectedListener(this);
    swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimaryGreen));
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if (navigationView.getMenu().getItem(0).isChecked()) {
                if (Utils.hasNetworkAccess(MainActivity.this)) {
                    getData();
                } else {
                    Toast.makeText
                            (MainActivity.this, "You must connect to the Internet to update the list"
                                    , Toast.LENGTH_LONG).show();
                }
            } else {
                for (int i = 1; i < 7; i++) {
                    if (navigationView.getMenu().getItem(i).isChecked()) {
                        getItemsByLabel(navigationView.getMenu().getItem(i).getTitle().toString());
                    }
                }
            }

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }, 3000);
        }

    });

    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;
                    getData(); // This is where I call getData <--
                }
            }
        }
    });

    if (Utils.hasNetworkAccess(this)) {
        getData();
    } else {
        if (runtimeExceptionDaoItems == null || runtimeExceptionDaoItems.queryForAll().isEmpty()) {
            Toast.makeText(this, "There's no data", Toast.LENGTH_LONG).show();
        } else {
            items.addAll(runtimeExceptionDaoItems.queryForAll());
            Toast.makeText(this, "From Database", Toast.LENGTH_LONG).show();
        }
    }
}

long lastPress;
Toast backpressToast;

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastPress > 5000) {
            backpressToast = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
            backpressToast.show();
            lastPress = currentTime;
        } else {
            if (backpressToast != null) backpressToast.cancel();
            super.onBackPressed();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setQueryHint(getResources().getString(R.string.searchForPosts));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String keyword) {
            getItemsBySearch(keyword);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String keyword) {
            return false;
        }
    });

    searchView.setOnCloseListener(() -> {
        emptyView.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
        getData();
        return false;
    });

    return true;
}

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

    switch (item.getItemId()) {
        case R.id.home:
            getData();
            break;
        case R.id.accessory:
            toolbar.setTitle(R.string.accessory);
            getItemsByLabel("Accessory");
            break;
        case R.id.arcade:
            toolbar.setTitle(R.string.arcade);
            getItemsByLabel("Arcade");
            break;
        case R.id.fashion:
            toolbar.setTitle(R.string.fashion);
            getItemsByLabel("Fashion");
            break;
        case R.id.food:
            toolbar.setTitle(R.string.food);
            getItemsByLabel("Food");
            break;
        case R.id.heath:
            toolbar.setTitle(R.string.heath);
            getItemsByLabel("Heath");
            break;
        case R.id.lifeStyle:
            toolbar.setTitle(R.string.lifestyle);
            getItemsByLabel("Lifestyle");
            break;
        case R.id.sports:
            toolbar.setTitle(R.string.sports);
            getItemsByLabel("Sports");
            break;
        case R.id.settings:
            break;
    }

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

private void getData() {
    progressBar.setVisibility(View.VISIBLE);
    String url = BloggerAPI.BASE_URL + "?key=" + BloggerAPI.KEY;

    if (token != "") {
        url = url + "&pageToken=" + token;
    }
    if (token == null) {
        return;
    }

    final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
    postList.enqueue(new Callback<PostList>() {
        @Override
        public void onResponse(@NonNull Call<PostList> call, @NonNull Response<PostList> response) {
            if (response.isSuccessful()) {
                progressBar.setVisibility(View.GONE);
                PostList list = response.body();
                if (list != null) {
                    token = list.getNextPageToken();
                    items.addAll(list.getItems());
                    adapter.notifyDataSetChanged();
            } else {
                progressBar.setVisibility(View.GONE);
                recyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);

                int sc = response.code();
                switch (sc) {
                    case 400:
                        Log.e("Error 400", "Bad Request");
                        break;
                    case 404:
                        Log.e("Error 404", "Not Found");
                        break;
                    default:
                        Log.e("Error", "Generic Error");
                }
            }
        }

        @Override
        public void onFailure(@NonNull Call<PostList> call, @NonNull Throwable t) {
            Toast.makeText(MainActivity.this, "Error occured", Toast.LENGTH_LONG).show();
            Log.e(TAG, "onFailure: " + t.toString());
            Log.e(TAG, "onFailure: " + t.getCause());
            progressBar.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    });
}

//=============================================================================================

public void getItemsByLabel(String label) {
    progressBar.setVisibility(View.VISIBLE);
    String url = BloggerAPI.BASE_URL + "search?q=label:" + label + "&key=" + BloggerAPI.KEY;

    Log.e("Label :", url);

    if (token != "") {
        url = url + "&pageToken=" + token;
    }
    if (token == null) {
        return;
    }

    final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
    postList.enqueue(new Callback<PostList>() {
        @Override
        public void onResponse(@NonNull Call<PostList> call, @NonNull Response<PostList> response) {
            if (response.isSuccessful()) {
                progressBar.setVisibility(View.GONE);
                items.clear();
                recyclerView.swapAdapter(adapter, false);
                PostList list = response.body();
                if (list != null) {
                    token = list.getNextPageToken();
                    items.addAll(list.getItems());
                    adapter.notifyDataSetChanged();
                }
            } else {
                progressBar.setVisibility(View.GONE);
                recyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
                int sc = response.code();
                switch (sc) {
                    case 400:
                        Log.e("Error 400", "Bad Request");
                        break;
                    case 404:
                        Log.e("Error 404", "Not Found");
                        break;
                    default:
                        Log.e("Error", "Generic Error");
                }
            }
        }

        @Override
        public void onFailure(@NonNull Call<PostList> call, @NonNull Throwable t) {
            Toast.makeText(MainActivity.this, "Error occured", Toast.LENGTH_LONG).show();
            Log.e(TAG, "onFailure: " + t.toString());
            Log.e(TAG, "onFailure: " + t.getCause());
            progressBar.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    });
}

我试图解决这个问题的方法:

  1. 在每个方法中实现,并且ScrollListenergetDatagetItemsByLabel
  2. 在导航抽屉菜单上为每个项目创建一个片段,并在其上实现。ScrollListener
  3. 最后,我放置 for 循环 方法来检测抽屉菜单上的哪个项目被检查以获取它自己的列表。但不幸的是,没有一个是有效的。onScrolled

答案 1

我不认为将通用添加到您的.我认为这个问题是由于控制流的实现以及使用API调用获取列表时线程之间的争用条件而发生的。让我简要描述一下我的想法。addOnScrollListenerRecyclerView

传入的 API 和函数都是异步的,因此无法保证 API 调用何时随数据一起返回。因此,让我们考虑以下场景:getDatagetItemsByLabel

  1. getItemsByLabel当您单击导航抽屉中的项目时调用。
  2. 与此同时,函数以某种方式被调用,API立即触发。onScrolledgetData
  3. getItemsByLabel函数随数据一起返回,清除了列表并开始向 中插入新数据。itemsitems
  4. 在 列表中的数据被清除并开始在 中添加元素后,API 同时返回。getDataitemsgetItemsByLabelitems
  5. 因此,该列表同时具有 和 中的元素。itemsgetDatagetItemsByLabel
  6. 该实现不是线程安全的,因此它很容易从两个API响应中混合数据。ArrayList

我希望这能解释你遇到的问题。若要避免此问题,可以考虑使用一个类似于默认情况下可能具有值的变量。在导航抽屉中单击项目时,将 值设置为 。然后在以下案例中将元素添加到列表中之前检查此值。booleangetItemsByLabelCalledfalsegetItemsByLabelCalled = trueitems

  1. 函数内部。getDataif(getItemsByLabelCalled) return
  2. 在函数中,设置并重置 的值。请检查更新部分中的以下代码。getItemsByLabelgetItemsByLabelCalled

希望有所帮助!

更新

为了向您演示应进行哪些更改的示例,请执行以下操作:

采取 用于在调用函数时进行跟踪。boolean

private boolean getItemsByLabelCalled = false; 

现在,按如下所示修改函数。getData

private void getData() {
    if(getItemsByLabelCalled) return;

    // Other statements are the same as before
}

现在修改函数以设置变量。getItemsByLabelgetItemsByLabelCalled

public void getItemsByLabel(String label) {

    // Here is the change
    if(getItemsByLabelCalled) return; 
    else getItemsByLabelCalled = true;

    progressBar.setVisibility(View.VISIBLE);
    String url = BloggerAPI.BASE_URL + "search?q=label:" + label + "&key=" + BloggerAPI.KEY;

    Log.e("Label :", url);

    if (token != "") {
        url = url + "&pageToken=" + token;
    }
    if (token == null) {
        return;
    }

    final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
    postList.enqueue(new Callback<PostList>() {
        @Override
        public void onResponse(@NonNull Call<PostList> call, @NonNull Response<PostList> response) {
            if (response.isSuccessful()) {
                progressBar.setVisibility(View.GONE);
                items.clear();
                recyclerView.swapAdapter(adapter, false);
                PostList list = response.body();
                if (list != null) {
                    token = list.getNextPageToken();
                    items.addAll(list.getItems());
                    adapter.notifyDataSetChanged();
                }

                // Reset again here
                getItemsByLabelCalled = false;
            } else {
                // Reset again here
                getItemsByLabelCalled = false;

                progressBar.setVisibility(View.GONE);
                recyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
                int sc = response.code();
                switch (sc) {
                    case 400:
                        Log.e("Error 400", "Bad Request");
                        break;
                    case 404:
                        Log.e("Error 404", "Not Found");
                        break;
                    default:
                        Log.e("Error", "Generic Error");
                }
            }
        }

        @Override
        public void onFailure(@NonNull Call<PostList> call, @NonNull Throwable t) {
            Toast.makeText(MainActivity.this, "Error occured", Toast.LENGTH_LONG).show();
            Log.e(TAG, "onFailure: " + t.toString());
            Log.e(TAG, "onFailure: " + t.getCause());
            progressBar.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);

            // Reset again here
            getItemsByLabelCalled = false;
        }
    });
}

我认为不需要更多的改变,尽管我不确定。让我知道这是否有效。


答案 2

我认为问题的发生是由于recyclerView.swapAdapter(adapter, false);

swapAdapter 用于用新适配器交换适配器。您传递相同的适配器,因此此调用是无用的。即使您要设置新的适配器,布尔参数也应该为 true,以便 RecyclerView 回收所有现有视图。


推荐