Dr *_*ido 6 android mvvm infinite-scroll retrofit android-recyclerview
我在我的应用程序中使用博主 API、改造和 MVVM,我尝试在用户滚动时使用分页加载更多帖子,这里发生的问题是响应正在加载它自己“相同的列表/相同的十个帖子正在再次加载”
这是我的代码
PostsClient 类
public class PostsClient {
private static final String TAG = "PostsClient";
private static final String KEY = "XYZ sensitive key!";
private static final String BASE_URL = "https://www.googleapis.com/blogger/v3/blogs/4294497614198718393/";
private PostInterface postInterface;
private static PostsClient INSTANCE;
public PostsClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
postInterface = retrofit.create(PostInterface.class);
}
public static PostsClient getINSTANCE() {
if(INSTANCE == null){
INSTANCE = new PostsClient();
}
return INSTANCE;
}
public Call<PostList> getPostList(){
return postInterface.getPostList(KEY);
}
}
Run Code Online (Sandbox Code Playgroud)
[ 后视图模型]
public class PostViewModel extends ViewModel {
public static final String TAG = "PostViewModel";
public MutableLiveData<PostList> postListMutableLiveData = new MutableLiveData<>();
public MutableLiveData<PostList> postListByLabelMutableLiveData = new MutableLiveData<>();
public MutableLiveData<String> finalURL = new MutableLiveData<>();
public MutableLiveData<String> token = new MutableLiveData<>();
public void getPosts(){
if (token.getValue() != "") {
finalURL.setValue(finalURL.getValue() + "&pageToken=" + token.getValue());
}
if (token == null) {
return;
}
PostsClient.getINSTANCE().getPostList().enqueue(new Callback<PostList>() {
@Override
public void onResponse(@NotNull Call<PostList> call, @NotNull Response<PostList> response) {
PostList list = response.body();
if (list.getItems() != null) {
token.setValue(list.getNextPageToken());
postListMutableLiveData.setValue(list);
}
Log.i(TAG,response.body().getItems().toString());
}
@Override
public void onFailure(Call<PostList> call, Throwable t) {
Log.e(TAG,t.getMessage());
}
});
}
public void getPostListByLabel(){
PostsByLabelClient.getINSTANCE().getPostListByLabel(finalURL.getValue()).enqueue(new Callback<PostList>() {
@Override
public void onResponse(Call<PostList> call, Response<PostList> response) {
postListByLabelMutableLiveData.setValue(response.body());
}
@Override
public void onFailure(Call<PostList> call, Throwable t) {
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
HomeFragment 类“主页”
public class HomeFragment extends Fragment {
private PostViewModel postViewModel;
public static final String TAG = "HomeFragment";
private RecyclerView recyclerView;
private PostAdapter postAdapter;
private List<Item> itemArrayList;
private boolean isScrolling = false;
private int currentItems, totalItems, scrollOutItems, selectedIndex;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
postViewModel = new ViewModelProvider(this).get(PostViewModel.class);
postViewModel.getPosts();
View root = inflater.inflate(R.layout.fragment_home, container, false);
itemArrayList = new ArrayList<>();
recyclerView = root.findViewById(R.id.homeRecyclerView);
postAdapter = new PostAdapter(getContext(),itemArrayList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext()
, linearLayoutManager.getOrientation());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setAdapter(postAdapter);
// textView.setText(s);
postViewModel.postListMutableLiveData.observe(HomeFragment.this, new Observer<PostList>() {
@Override
public void onChanged(PostList postList) {
itemArrayList.addAll(postList.getItems());
postAdapter.notifyDataSetChanged();
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
isScrolling = true;
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
currentItems = linearLayoutManager.getChildCount();
totalItems = linearLayoutManager.getItemCount();
scrollOutItems = linearLayoutManager.findFirstVisibleItemPosition();
if (isScrolling && (currentItems + scrollOutItems == totalItems)) {
isScrolling = false;
postViewModel.getPosts();
postAdapter.notifyDataSetChanged();
}
}
}
});
return root;
}
}
Run Code Online (Sandbox Code Playgroud)
'更多解释
在 PostViewModel 我创建了一个变量
public MutableLiveData<String> token = new MutableLiveData<>();
这个代表新页面/响应的令牌将带有“每个页面都有一个列表/十个新帖子”
在 HomeFragment 上
我创建了三个整数值
private int currentItems, totalItems, scrollOutItems, selectedIndex;
和一个布尔值
private boolean isScrolling = false;
然后我用 recyclerView.addOnScrollListener
用这种方式加载接下来的十篇文章,但它不像我之前说的那样工作,它加载了相同的结果/列表
imgur.com 上的结果| 归档时间: |
|
| 查看次数: |
618 次 |
| 最近记录: |