从后台恢复后开始滚动时,RecyclerView会冻结

时间:2015-09-01 20:50:40

标签: android android-recyclerview android-adapter android-viewholder

这是我的适配器类

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

    List<CategoryItem> mItems;

    public CategoryAdapter(Context context) {
        super();

        Resources res = context.getResources();
        mItems = new ArrayList<>();

        mItems.add(new CategoryItem(res.getString(R.string.category_book), R.drawable.books));
        // ... few items
        mItems.add(new CategoryItem(res.getString(R.string.category_clothing), R.drawable.clothes));
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_category_selection, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        CategoryItem category = mItems.get(i);
        viewHolder.title.setText(category.getTitle());
        viewHolder.thumbnail.setImageResource(category.getThumbnail());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public ImageView thumbnail;
        public CardView card;

        public ViewHolder(View itemView){
            super(itemView);

            card = (CardView)itemView.findViewById(R.id.card_view);
            title = (TextView)itemView.findViewById(R.id.category_title);
            thumbnail = (ImageView)itemView.findViewById(R.id.category_thumbnail);
        }
    }
}

我的片段

public class CategorySelectionFragment extends Fragment {

    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView.Adapter mAdapter;

    public CategorySelectionFragment() {  }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_category_selection, container, false);

        mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);    
        setUpGrid();

        return v;
    }

    private void setUpGrid() {
        mRecyclerView.setHasFixedSize(true);

    colNum = 3;
        mLayoutManager = new GridLayoutManager(getActivity(), colNum);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new CategoryAdapter(getActivity());
        mRecyclerView.setAdapter(mAdapter);

    }

当片段刚刚创建时(onCreateView调用),recyclerview工作正常,但当应用程序转到后台然后恢复滚动开始冻结前几个项目然后继续顺利工作。

当应用程序从后台恢复onCreateView方法时没有调用,所以我尝试在onStart()中为recyclerview设置适配器,如下所示:

  @Override
    public void onStart(){
        super.onStart();

        // Adapter setting here to avoid freezes, when start to scroll after switching to an app from background.
        mRecyclerView.setAdapter(mAdapter);
    } 

这种方法已经解决了这个问题,但似乎并不是解决这个问题的真正方法。例如,恢复片段导致重置最后滚动位置。

感谢您的帮助!

0 个答案:

没有答案