如何在一个活动中将 addOnScrollListener 用于不同的列表
在我的应用程序中,有两种方法和.每个列表都通过改造回调方法获得不同的列表,我使用导航抽屉方法,以便每当用户单击特定项目时,都会在 .getData
getItemsByLabel
onNavigationItemSelected
RecyclerView
问题是,我使用一种方法来检测任何列表中的滚动行为,这些列表导致显示列表中的项目重叠。addOnScrollListener
RecyclerView
因此,问题是当向下滚动时,主列表中的项目和所选类别/项目的列表是重叠的。
这是我的代码。
@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);
}
});
}
我试图解决这个问题的方法:
- 在每个方法中实现,并且
ScrollListener
getData
getItemsByLabel
- 在导航抽屉菜单上为每个项目创建一个片段,并在其上实现。
ScrollListener
- 最后,我放置 for 循环 方法来检测抽屉菜单上的哪个项目被检查以获取它自己的列表。但不幸的是,没有一个是有效的。
onScrolled