首先不在Android的NextPageToken Youtube API3中加载最新视频

时间:2018-10-11 18:51:01

标签: java android xml youtube-api retrofit

我想通过YouTube API在Android应用中加载YouTube频道视频。因此我添加了带有分页功能的代码。代码可以正常工作,但问题并不在于首先加载最新视频。它们混杂不清。有些显示了多个。 这是为什么 ?请帮助我解决问题。我使用了翻新。

这是我的MainActivity.java

public class MainActivity extends AppCompatActivity {

        private static final String TAG = "MainActivity";
        private VideosAdapter adapter;
        //if you are using searchview to get search result then store searched query in lastSearched variable.
        //get latest token and store in lastToken variable.
        private String lastSearched = "", lastToken = "";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            RecyclerView recyclerView = findViewById(R.id.video_list);
            Button more = findViewById(R.id.more);

            adapter = new VideosAdapter();
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
            recyclerView.setAdapter(adapter);

            //load data from api.
            search("", false);

            more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //load more data
                    search("", true);
                }
            });
        }

        /**
         * call this method to get response from youtube API.
         *
         * @param query String value to search on google, Empty string means get all videos.
         * @param more  if you want to load next page then pass true, this means add new items at bottom of RecyclerView.
         */
        private void search(String query, final boolean more) {

            final ProgressDialog progressDialog = ProgressDialog.show(this, null, "Loading ...", true, false);
            String searchType = "video";
            if (query != null) {
                if (query.startsWith("#")) {
                    searchType = "video";
                    query = query.substring(1);
                } else if (query.startsWith("@")) {
                    searchType = "channel";
                    query = query.substring(1);
                }
            }
            if (!more) {
                lastSearched = query;
                lastToken = "";
            }

            Call<YoutubeResponse> youtubeResponseCall = APIService.youtubeApi.searchVideo(query, searchType, Constants.YOUTUBE_API_KEY, "snippet,id", "5","UCItoVC3HUBA6s9qOEVPILrA", lastToken);
            youtubeResponseCall.enqueue(new Callback<YoutubeResponse>() {
                @Override
                public void onResponse(@NonNull Call<YoutubeResponse> call, @NonNull Response<YoutubeResponse> response) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                    YoutubeResponse body = response.body();
                    if (body != null) {
                        List<YoutubeResponse.Item> items = body.getItems();
                        lastToken = body.getNextPageToken();
                        if (more) {
                            adapter.addAll(items);
                        } else {
                            adapter.replaceWith(items);
                        }
                    }
                }

                @Override
                public void onFailure(@NonNull Call<YoutubeResponse> call, @NonNull Throwable t) {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                    Log.e(TAG, "onFailure: ", t);
                }
            });
        }
    }

这是YoutubeApi.java

public interface YoutubeApi {
    @GET("/youtube/v3/search")
    Call<YoutubeResponse> searchVideo(@Query("q") String query,
                                  @Query("type") String type,
                                  @Query("key") String key,
                                  @Query("part") String part,
                                  @Query("maxResults") String maxResults,
                                  @Query("channelId") String channelId,
                                  @Query("pageToken") String pageToken);
}

谢谢。

0 个答案:

没有答案