RecyclerView在滚动时已删除项目,而不是集中在整个第一个项目上

时间:2019-04-10 07:52:18

标签: android android-recyclerview

我正在尝试使我的家像Google Play Home一样,将主RecyclerView用作垂直对象,并且在其中放置另一个水平RecyclerView,效果很好,但是当我向下滚动水平RecyclerView时,这些物品就会消失,如下图所示

这里是第一次加载时的房屋(我不知道为什么水平的第一项从一开始就没有出现,我需要向左滚动才能正确看到它) here the home on first loading 在这里,其他项目在向下滚动(它与第一个项目面临相同的问题) here the other items on scrolling down 在这里我向下滚动并向上滚动后的第一个屏幕消失了水平项目 here the first screen after i scroll down and back with scroll up 在这里第二个屏幕,如果我再次向下滚动,奇怪的是有一个垂直项目工作正常,并且项目没有消失 here the second screen if i scroll down again

我尝试了一些解决方法

  • 首先,我添加了addOnScrollListener并使用adpter.notifyDataSetChanged();在滚动时,这可以防止项目消失,但是在向下滚动时它并没有保存我的最后一次水平滚动,因此总是使我回到第一个项目。

  • 第二秒钟,我尝试了此答案中的解决方案,但对Why RecyclerView items disappear with scrolling

  • 没有帮助

这是我第一个解决方案的代码

 my_recycler_view.getRecycledViewPool().setMaxRecycledViews(0, 0);
    my_recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                // Scrolling up
                adapter.notifyDataSetChanged();
            } else {
                adapter.notifyDataSetChanged();
                adapter.stopHandler();

            }
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (newState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Do something
            } else if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                // Do something
            } else {
                // Do something
            }
        }
    });

这是我的第一个适配器代码

public class HomeRecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<NewHomeItemsModel> dataList;
private Context mContext;
ViewPager viewPager;
HeaderPagerAdapter headerPagerAdapter;
int page = 0;


private Handler handler;
private int delay = 5000; //milliseconds
Runnable runnable = new Runnable() {
    public void run() {
        if (headerPagerAdapter.getCount() == page) {
            page = 0;
        } else {
            page++;
        }
        viewPager.setCurrentItem(page, true);
        handler.postDelayed(this, delay);
    }
};

public HomeRecyclerViewDataAdapter(Context context, ArrayList<NewHomeItemsModel> dataList) {
    this.dataList = dataList;
    this.mContext = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view;
    switch (i) {
        case Constants.type_header:
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_header_item, null);
            HeaderHolder mh = new HeaderHolder(view);
            return mh;
        case Constants.type_categories:
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_vertical_item, null);
            ItemRowHolder mho = new ItemRowHolder(view);
            return mho;
        case Constants.type_suggestedProducts:
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_vertical_item, null);
            ItemRowHolder pro = new ItemRowHolder(view);
            return pro;

    }

    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder itemRowHolder, int i) {

    NewHomeItemsModel newHomeItemsModel = dataList.get(i);
    Log.d("itemType",String.valueOf(newHomeItemsModel.getType()));
    Activity activity = (Activity) mContext;
    switch (newHomeItemsModel.getType()) {

        case Constants.type_header:
            headerPagerAdapter = new HeaderPagerAdapter(activity, newHomeItemsModel.getHeaderImages());
            viewPager.setAdapter(headerPagerAdapter);
            if (headerPagerAdapter.getCount() == page) {
                page = 0;
            }
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    page = position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            startHandler();

            if (Constants.auto_header.equals("start")){
                //startHandler();
            }else{
                //stopHandler();
            }
            break;
        case Constants.type_categories:

            final String sectionName = dataList.get(i).getTitle();
            ArrayList singleSectionItems = dataList.get(i).getCategories();
            ((ItemRowHolder) itemRowHolder).itemTitle.setText(sectionName);
            final CategoriesRecyAdapter itemListDataAdapter = new CategoriesRecyAdapter(mContext, singleSectionItems);
           // ((ItemRowHolder) itemRowHolder).recycler_view_list.setHasFixedSize(true);
            final GridLayoutManager manager = new GridLayoutManager(activity, 2 ,
                    GridLayoutManager.HORIZONTAL, false);
            //((ItemRowHolder) itemRowHolder).recycler_view_list.smoothScrollToPosition(0);
            ((ItemRowHolder) itemRowHolder).recycler_view_list.setLayoutManager(manager);
            ((ItemRowHolder) itemRowHolder).recycler_view_list.setAdapter(itemListDataAdapter);
            ((ItemRowHolder) itemRowHolder).recycler_view_list.getRecycledViewPool().setMaxRecycledViews(0, 0);
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return itemListDataAdapter.isHeader(position) ? manager.getSpanCount() : 1;
                }
            });
           // itemListDataAdapter.notifyDataSetChanged();
            ((ItemRowHolder) itemRowHolder).actionMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), sectionName, Toast.LENGTH_SHORT).show();
                }
            });
            break;
              case Constants.type_suggestedProducts:

            final String sectionNameProduct = dataList.get(i).getTitle();
            ArrayList singleSectionItemsProducts = dataList.get(i).getProducts();
            ((ItemRowHolder) itemRowHolder).itemTitle.setText(sectionNameProduct);
            final HomeActivityRecyAdpater productsListDataAdapter = new HomeActivityRecyAdpater( singleSectionItemsProducts
                    ,mContext);
            final LinearLayoutManager LManager = new LinearLayoutManager(activity,
                    LinearLayoutManager.HORIZONTAL, false);
            ((ItemRowHolder) itemRowHolder).recycler_view_list.setLayoutManager(LManager);
            ((ItemRowHolder) itemRowHolder).recycler_view_list.setAdapter(productsListDataAdapter);
            ((ItemRowHolder) itemRowHolder).recycler_view_list.getRecycledViewPool().setMaxRecycledViews(0, 0);
            productsListDataAdapter.notifyDataSetChanged();
            ((ItemRowHolder) itemRowHolder).actionMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), sectionNameProduct, Toast.LENGTH_SHORT).show();
                }
            });
            break;
    }


}

@Override
public int getItemViewType(int position) {
    NewHomeItemsModel newHomeItemsModel = dataList.get(position);
    if (newHomeItemsModel != null) {
        return newHomeItemsModel.getType();
    }
    // return super.getItemViewType(position);
    return 0;
}

@Override
public int getItemCount() {
    return (null != dataList ? dataList.size() : 0);
}

public class ItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView itemTitle;
    protected RecyclerView recycler_view_list;
    protected TextView actionMore;

    public ItemRowHolder(View view) {
        super(view);
        itemTitle = (TextView) view.findViewById(R.id.best_offers_title);
        recycler_view_list = (RecyclerView) view.findViewById(R.id.offers_rv);
        actionMore = (TextView) view.findViewById(R.id.best_offers_more_action);


    }
}

public class HeaderHolder extends RecyclerView.ViewHolder {

    protected ImageView headerImg;

    public HeaderHolder(View view) {
        super(view);
        viewPager = view.findViewById(R.id.Header_view_pager_item);
        // this.headerImg = (ImageView) view.findViewById(R.id.header_pager_image);
    }
}

public class ProductsHolder extends RecyclerView.ViewHolder {

    protected TextView itemTitleProduct;
    protected RecyclerView recycler_view_list;
    protected TextView actionMore;

    public ProductsHolder(View view) {
        super(view);


        this.itemTitleProduct = (TextView) view.findViewById(R.id.best_offers_title);
        this.recycler_view_list = (RecyclerView) view.findViewById(R.id.offers_rv);
        this.actionMore = (TextView) view.findViewById(R.id.best_offers_more_action);


    }
}

public void stopHandler(){
    if (handler!=null){
        handler.removeCallbacks(runnable);
        handler=null;
    }
}

public void startHandler(){
    if (handler==null){
        handler = new Handler();
        handler.postDelayed(runnable, delay);
    }
}

}

所以希望有人能帮助解决这个问题以及第一次加载水平列表而不显示整个第一项的问题

0 个答案:

没有答案