在应用的这一部分中,我使用Blogger API和改造从服务器获取数据,当特定项单击方法onNavigationItemSelected
时,我使用了getItemsByLabel
方法,该方法称为哪种改造onResponse
方法将调用并获取所选类别的数据,问题是它是在我第一次单击项目时起作用的,此后视图没有改变,要进一步说明,请查看此gif
此getItemsByLabel方法
public void getItemsByLabel(String label) {
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;
}
progress.setVisibility(View.VISIBLE);
final Call<PostList> postList = BloggerAPI.getService().getPostList(url);
postList.enqueue(new Callback<PostList>() {
@Override
public void onResponse(Call<PostList> call, Response<PostList> response) {
items.clear();
recyclerView.swapAdapter(adapter, false);
PostList list = response.body();
token = list.getNextPageToken();
items.addAll(list.getItems());
adapter = new PostAdapter(MainActivity.this,items);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Sucess", Toast.LENGTH_LONG).show();
progress.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<PostList> call, Throwable t) {
Toast.makeText(MainActivity.this, "Error occured", Toast.LENGTH_LONG).show();
Log.i(TAG, "onFailure: " + t.toString());
}
});
}
我使用items.clear();
和recyclerView.swapAdapter(adapter, false);
来更改列表中的项目,这些项目由标签选择并在此处使用
onNavigationItemSelected方法
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.home:
getData();
break;
case R.id.accessory:
getItemsByLabel("Accessory");
break;
case R.id.arcade:
getItemsByLabel("Arcade");
break;
case R.id.fashion:
getItemsByLabel("Fashion");
break;
case R.id.food:
getItemsByLabel("Food");
break;
case R.id.heath:
getItemsByLabel("Heath");
break;
case R.id.lifeStyle:
getItemsByLabel("Lifestyle");
break;
case R.id.sports:
getItemsByLabel("Sports");
break;
case R.id.settings:
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}