Не загружать последние видео в первую очередь в NextPageToken Youtube API3 в Android - PullRequest
0 голосов
/ 11 октября 2018

Я хотел загрузить видео с канала YouTube в приложении Android через API YouTube. Так что я добавил код с разбивкой на страницы. Так что код работает, но проблема не в том, чтобы загружать последние видео в первую очередь. У них перепутано.Это почему ?Пожалуйста, помогите мне решить мою проблему. Я использовал Retrofit.

Вот мой 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);
}

Спасибо.

...