0

Я разрабатываю Android приложение для Android Tv. У меня есть RecyclerView в котором есть элементы которые можно перебирать с помощью пульта (D-pad). Когда я дохожу до конца RecyclerView то фокус (обводка вокруг элемента в RecyclerView) перепрыгивает или слетает с RecyclerView на панель навигации. Я хочу чтобы фокус останавливался в конце RecyclerView. Заранее спасибо

Мой fragment.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TV.TvPopularFragment">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminateDrawable="@drawable/progress_circle_style"/>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/moviesRecyclerView"
        android:layout_width="match_parent"
        android:nextFocusDown="@+id/moviesRecyclerView"
        android:descendantFocusability="afterDescendants"
        android:layout_height="match_parent"/>



</RelativeLayout>

Мой fragment.java

public class TvPopularFragment extends Fragment {


    FragmentTvPopularBinding binding;

    static public boolean isPopularOnTop = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentTvPopularBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        RecyclerView moviesRecycerView = binding.moviesRecyclerView;
        ProgressBar progressBar = binding.progressBar;


        Display display = getActivity().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        float density  = getResources().getDisplayMetrics().density;
        float dpWidth  = outMetrics.widthPixels / density;
        int columns = Math.round(dpWidth/133);
        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(),columns);



        moviesRecycerView.setLayoutManager(mLayoutManager);
        PopularAdapter popularAdapter = new PopularAdapter(getActivity(),popular_posters,
                popular_title, popular_id, popular_categories, popular_votes_pos, popular_votes_neg,
                popular_last_episode);



        new CountDownTimer(10000, 1000) {

            public void onTick(long millisUntilFinished) {

                if(AppActivity.isPopularCategoryLoaded) {

                    moviesRecycerView.setAdapter(popularAdapter);
                    progressBar.setVisibility(View.GONE);
                    cancel();

                }

            }

            public void onFinish() {
            }
        }.start();




        moviesRecycerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                int totalItemCount = mLayoutManager.getItemCount();
                int lastVisible = mLayoutManager.findLastVisibleItemPosition();

                boolean endHasBeenReached = lastVisible + 15 >= totalItemCount;

                isPopularOnTop = dy == 0;

                if (totalItemCount > 0 && endHasBeenReached) {


                    if(AppActivity.popularHasNextPage) {


                        AppActivity.popularPage += 1;
                        AppActivity.loadPopularCategory();


                        new CountDownTimer(5000, 1000) {

                            public void onTick(long millisUntilFinished) {

                                if(AppActivity.isPopularCategoryLoaded) {

                                    progressBar.setVisibility(View.GONE);
                                    popularAdapter.notifyDataSetChanged();

                                }

                            }

                            public void onFinish() {
                            }
                        }.start();

                        AppActivity.popularHasNextPage = false;

                    }

                }
            }
        });


        return root;
    }
}

Мой adapter.java:

public class PopularAdapter extends RecyclerView.Adapter<PopularViewHolder> {


    static public String current_id;
    static public String current_category;


    ArrayList<String> movies_posters;
    ArrayList<String> movies_title;
    ArrayList<String> movies_id;
    ArrayList<String> movies_categories;
    ArrayList<String> movies_votes_pos;
    ArrayList<String> movies_votes_neg;
    ArrayList<String> movies_last_episode;


    LayoutInflater inflater;
    Context context;


    public PopularAdapter(Context context, ArrayList<String> movies_posters,
                          ArrayList<String> movies_title,
                          ArrayList<String> movies_id,
                          ArrayList<String> movies_categories,
                          ArrayList<String> movies_votes_pos,
                          ArrayList<String> movies_votes_neg,
                          ArrayList<String> movies_last_episode){

        this.movies_posters = movies_posters;
        this.movies_title = movies_title;
        this.movies_id = movies_id;
        this.movies_categories = movies_categories;
        this.movies_votes_pos = movies_votes_pos;
        this.movies_votes_neg = movies_votes_neg;
        this.movies_last_episode = movies_last_episode;

        this.context = context;
        this.inflater = LayoutInflater.from(context);

    }


    @NonNull
    @Override
    public PopularViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_movie, parent, false);
        return new PopularViewHolder(view);

    }

    @SuppressLint({"SetTextI18n", "NotifyDataSetChanged"})
    @Override
    public void onBindViewHolder(@NonNull PopularViewHolder holder, @SuppressLint("RecyclerView") int position) {
        

        if(movies_categories.get(position).equals("s7")||
                movies_categories.get(position).equals("s93")) {

            holder.movie_last_episode.setVisibility(View.VISIBLE);
            holder.movie_last_episode.setText(movies_last_episode.get(position));

        } else {
            holder.movie_last_episode.setVisibility(View.GONE);
        }



        Picasso.with(context).load(movies_posters.get(position))
                .into(holder.movie_poster, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                        holder.movie_icon.setVisibility(View.VISIBLE);
                    }
                });

        holder.movie_title.setText(Html.fromHtml(movies_title.get(position)));

        holder.movie_btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

                holder.movie_title.setSelected(b);

            }
        });

        holder.movie_votes_pos.setText(movies_votes_pos.get(position));
        holder.movie_votes_neg.setText(movies_votes_neg.get(position));



        holder.movie_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                current_id = movies_id.get(position);
                current_category = movies_categories.get(position);

                AppActivity.current_id = current_id;

                MovieVideosFragment.category = current_category;
                VideosAdapter.category = current_category;


                if (current_category.equals("s7") || current_category.equals("s93"))
                    AppActivity.loadSeriesAudio(current_id);
                else
                    AppActivity.loadMoviesAudio(current_id);

                AppActivity.isMoviesLoaded = false;
                AppActivity.isSeriesLoaded = false;

                MovieFragment.year = null;
                MovieFragment.actors = null;
                MovieFragment.genres = null;
                MovieFragment.countries = null;
                MovieFragment.short_story = null;


                AppActivity.loadMovieDescription(current_id);
                AppActivity.loadSimilar(current_id);


                MovieFragment.poster = movies_posters.get(position);
                MovieFragment.title = movies_title.get(position);

                context.startActivity(new Intent(context, MovieTab.class));


            }
        });


    }

    @Override
    public int getItemCount() {
        return movies_posters.size();
    }
}
7
  • Пожалуйста, уточните вашу конкретную проблему или приведите более подробную информацию о том, что именно вам нужно. В текущем виде сложно понять, что именно вы спрашиваете. 11 янв 2022 в 19:13
  • Что значит "в конце"?
    – Style-7
    11 янв 2022 в 19:39
  • Куда "перепрыгивает или слетает"? 11 янв 2022 в 19:41
  • @Эникейщик Оно перепрыгивает на панель навигации приложения. 12 янв 2022 в 6:58
  • @Style-7 В конце RecyclerView 12 янв 2022 в 6:59

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.