OnBindViewholder滚动后未返回确切位置

时间:2019-12-10 18:27:25

标签: android android-recyclerview

我正在尝试复制YouTube的首页视图,并在顶部将水平滚动显示热门话题,然后复制一组名片视图以显示视频,并在页脚视图中显示首页,订阅和全部内容。

我正在使用带有页眉的Recyclerview,该页眉又具有RecyclerView的位置,顶部是趋势主题的滚动,底部是固定的LinearLayout。 (不是固定在底部,而是现在添加在最后一个位置。)

问题和初步发现

我想在第1个位置(数组索引-0)有一个自定义页眉视图,在第7个位置(数组索引-6)有一个自定义页脚。我的问题是,当我最初加载应用程序时,我可以看到onBindViewHolder正在调用从0到6的位置,并且当我一直向下滚动时,所有视图均显示正常,但是当我向上移动时,可以看到在1,2和3位置添加了3个空位置,并且显示的是纯白色屏幕,但我实际上可以看到在纯白色空间下方的1,2和3rd项(以及其余的项)这意味着滚动后会弹出一个新的1,2和3rd位置,并带有空白。这完全很奇怪,我也没有在任何RecyclerView项目中执行任何操作,也没有动态地进行更改。

任何人指出我可能会犯的愚蠢错误,我将不胜感激。

当我第一次打开该应用程序时,主屏幕显示了完整的视图

Main Screen With Fully Loaded Views

主屏幕,滚动后在位置1,2和3处具有空白视图

Main Screen With Empty Views

我有一个片段,我在其中设置了RecyclerView,具体代码如下(我不确定是否需要这个来发现问题,只是为了以防万一)

View rootView = inflater.inflate(R.layout.youtube_primary_screen, container, false);
recyclerView = rootView.findViewById(R.id.video_listview);
mainScrollAdapter = new MainScrollAdapter(context,repeaterClasses,top_scroll_item_array);
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mainScrollAdapter);
mainScrollAdapter.notifyDataSetChanged();
return rootView;

这是我的主RecyclerView的主适配器类,其中有页眉和页脚

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

        View v;
        this.VIEW_TYPE = viewType;

        switch (viewType)
        {
            case VIEW_TYPES.Header:
                Log.e("Inside","Header");
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view,parent,false);
                break;

            case VIEW_TYPES.Footer:
                Log.e("Inside","Footer");
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_view,parent,false);
                break;

            default:
                Log.e("Inside","Others");
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_video_item,parent,false);
                break;

        }

        BaseViewHolder myViewHolder = new BaseViewHolder(context,v,mYoutubeVideos,top_scroll_items);
        return myViewHolder;
    }



    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, final int position) {
        holder.onBind(position);
        Log.e("OnBindVIewHolder",""+holder.getAdapterPosition());

    }

    @Override
    public int getItemViewType(int position) {
        Log.e("getItemViewType",""+position);
        if(position==0)
            return VIEW_TYPES.Header;
        if(position==7)
            return VIEW_TYPES.Footer;
        else
            return position;
    }

    @Override
    public int getItemCount() {
       return 7;
    }


    private class VIEW_TYPES {

        public static final int Header = 0;
        public static final int Footer = 6;
    }

我的BaseViewHolder扩展了RecyclerView.ViewHolder方法如下的onBind

public void onBind(final int position) {
        switch(position) {

            case VIEW_TYPES.Header:
                top_scroll_recycler_view.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
                top_scroll_recycler_view.setItemAnimator(new DefaultItemAnimator());
                top_scroll_recycler_view.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL) {
                    @Override
                    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                        // Do not draw the divider
                    }
                });
                top_scroll_recycler_view.setAdapter(new TopScrollAdap(context,top_scroll_items));
                break;

            case VIEW_TYPES.Footer:
                break;

            default:
                Log.e("The current position is ",""+position);
                if(position<6)
                {

                Log.e("The current position is ",""+position);
                final YouTubeVideoRepeater youTubeVideoRepeater = youTubeVideoRepeaters.get(position);
                ((Activity)itemView.getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                int width = displayMetrics.widthPixels;

                if(youTubeVideoRepeater.getTitle()!=null)
                    textWaveTitle.setText(youTubeVideoRepeater.getTitle());
                else
                    textWaveTitle = null;

                if(youTubeVideoRepeater.getImage_url()!=null)
                    Glide.with(itemView.getContext()).load(youTubeVideoRepeater.getImage_url()).apply(new RequestOptions().override(width-36,200)).into(imageViewItems);
                else
                    Glide.with(imageViewItems).clear(imageViewItems);

                imageViewItems.setVisibility(View.VISIBLE);
                playButton.setVisibility(View.VISIBLE);
                youTubePlayerView.setVisibility(View.GONE);

                playButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        imageViewItems.setVisibility(View.GONE);
                        youTubePlayerView.setVisibility(View.VISIBLE);
                        playButton.setVisibility(View.GONE);
                        youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
                            @Override
                            public void onReady(YouTubePlayer youTubePlayer) {
                                super.onReady(youTubePlayer);
                                youTubePlayer.loadVideo(youTubeVideoRepeaters.get(position).getVideo_id(),0);
                            }
                        });

                    }
                });


                }

                break;
        }

    }


    private class VIEW_TYPES {

        public static final int Header = 0;
        public static final int Footer = 7;
    }

更新:禁用Headerview可以解决问题,但我不知道为什么会发生。

标题中RecyclerView的Adpater如下

TopScrollAdapter

 @NonNull
    @Override
    public TopScrollAdap.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view_item,parent,false);
        viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull TopScrollAdap.ViewHolder holder, int position) {
        viewHolder.top_scroll_list_item.setText(top_scroll_items.get(position));
    }


    @Override
    public int getItemCount() {
        return top_scroll_items.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder
    {

        TextView top_scroll_list_item;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            top_scroll_list_item = itemView.findViewById(R.id.top_scroll_list_item);
        }

    }

0 个答案:

没有答案