Displaying RecyclerView into fragment - android

I've got methods:
private void initImageBitmaps() {
mImageUrls.add("imageUrl");
mNames.add("abcd");
initRecyclerView();
}
private void initRecyclerView() {
Log.d(TAG, "initRecyclerView: init recyclerview.");
RecyclerView recyclerView = findViewById(R.id.recyclerv_view);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames, mImageUrls);
recyclerView.setAdapter(adapter);
//recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
}
Which add images to my recyclerview.
If it was in activity I would call initImageBitmaps() inside OnCreate. Unfortunately I have to use it in fragment and the question is: how to implement these methods to class which extends Fragment? Of course everything will be displayed into RecyclerView with recycler_view id in xml.
EDIT:
My Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mImageNames = new ArrayList<>();
private ArrayList<String> mImages = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(WallpapersFragment context, ArrayList<String> imageNames, ArrayList<String> images ) {
mImageNames = imageNames;
mImages = images;
mContext = context;
//Rquired is android.content.context and found is WallpapersFragment from my project
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
Glide.with(mContext)
.asBitmap()
.load(mImages.get(position))
.into(holder.image);
holder.imageName.setText(mImageNames.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext, GalleryActivity.class);
intent.putExtra("image_url", mImages.get(position));
intent.putExtra("image_name", mImageNames.get(position));
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mImageNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView imageName;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
imageName = itemView.findViewById(R.id.image_name);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
It compiles only when I comment mContext = context; in adapter. Then the error is:
java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).

There is an onCreate() method in the Fragment class, but that method is different from activity's onCreate().
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
initRecyclerView();
}
}
Read more about fragments here: Fragments

initRecyclerView(view);
And then:
recyclerView = view.findViewById(R.id.recyclerv_view);

You can initialize your recycler view in following lifecycle callback:
onViewCreated()
and can also populate data method here like
initRecyclerView()
initImageBitmaps()
and for data refresh you can call notifyDatasetChanged() on adapter.

try this...
public class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.example_fragment, container, false);
initImageBitmaps(view);
return view;
}
private void initImageBitmaps(View view) {
mImageUrls.add("imageUrl");
mNames.add("abcd");
initRecyclerView(View view);
}
private void initRecyclerView(View view) {
Log.d(TAG, "initRecyclerView: init recyclerview.");
RecyclerView recyclerView = view.findViewById(R.id.recyclerv_view); // init recyclerview
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);// declare layoutmanager
recyclerView.setLayoutManager(layoutManager); // set layoutmanager
RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), mNames, mImageUrls); // init adapter
recyclerView.setAdapter(adapter); // set adapter in recyclerveiw
}
EDIT
Replace WallpapersFragment with Context in adapter
public RecyclerViewAdapter(Context context, ArrayList<String> imageNames, ArrayList<String> images ) {
mImageNames = imageNames;
mImages = images;
mContext = context;
//Rquired is android.content.context and found is WallpapersFragment from my project
}

Related

How to set RecyclerView OnClickListener an OnLongClickListener in Fragments?

I'm trying to start a new Fragment that shows the details of the item that was clicked on in the RecyclerView. The RecyclerView is on a Fragments. I've tried this in Activities and it worked perfectly but I'm trying to convert it to use on Fragments. And Also, how to detect other gestures in the RecyclerView (swipe, etc...)
My Adapter :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private final ArrayList<Tip> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
TextView textViewType;
//TextView textViewDetail;
ImageView imageViewImage;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewTitle = itemView.findViewById(R.id.title);
this.textViewType = itemView.findViewById(R.id.type);
//this.textViewDetail = itemView.findViewById(R.id.detail);
this.imageViewImage = itemView.findViewById(R.id.itemImage);
}
}
public MyAdapter(ArrayList<Tip> data) {
this.dataSet = data;
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.item, parent, false);
view.setOnClickListener(HomeFragment.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int listPosition) {
TextView textViewTitle = holder.textViewTitle;
TextView textViewType = holder.textViewType;
//TextView textViewDetail = holder.textViewDetail;
ImageView imageViewImage = holder.imageViewImage;
textViewTitle.setText( dataSet.get(listPosition).getTitle() );
textViewType.setText( dataSet.get(listPosition).getType() );
//textViewDetail.setText( dataSet.get(listPosition).getDetail() );
imageViewImage.setImageResource( dataSet.get(listPosition).getImage() );
}
#Override
public int getItemCount() {
return dataSet.size();
}
}
My Fragment :
public class HomeFragment extends Fragment {
public static RecyclerView.Adapter myAdapter;
private static RecyclerView.LayoutManager myLayoutManager;
private static RecyclerView myRecyclerView;
public static View.OnClickListener myOnClickListener;
private static ArrayList<Tip> myData;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Get the integer from the integers.xml resource file
Resources res = getResources();
int gridColumnCount = res.getInteger(R.integer.grid_column_count);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
// Add the following lines to create RecyclerView
myRecyclerView = view.findViewById(R.id.my_recycler_view);
myRecyclerView.setHasFixedSize(true);
//myLayoutManager = new LinearLayoutManager( view.getContext() );
myLayoutManager = new GridLayoutManager( view.getContext(),gridColumnCount );
myRecyclerView.setLayoutManager( myLayoutManager );
myRecyclerView.setItemAnimator( new DefaultItemAnimator() );
myData = new ArrayList<Tip>();
for (int i = 0; i < MyData.tipTitleArray.length; i++) {
myData.add(new Tip( MyData.tipTitleArray[i], MyData.tipTypeArray[i], MyData.tipDetailArray[i], MyData.id_[i], MyData.tipImageArray[i] ));
}
myAdapter = new MyAdapter(myData);
myRecyclerView.setAdapter(myAdapter);
//On retourne la View
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
fr.cnam.smb116.mimi_tips_navdraw.databinding.FragmentHomeBinding binding = null;
}
}
Don't use static listener and recyclerview instance.
Better to add callBack function to handle this.
e.g
interface RecyclerViewItemClickListener<T> {
void onClick(int position, T data);
}
init property in your adapter
RecyclerViewItemClickListener<Tip> listener
constructor of your Adapter
MyAdapter(RecyclerViewItemClickListener<Tip> listener) {
this. listener = listener
}
in
onBindViewHolder() {
holder.setOnClickListener(v -> {
listener.onClick(position, dataSet.get(listPosition));
})
}
In Your Fragment
Implement this interface
public class HomeFragment extends Fragment implements RecyclerViewItemClickListener<Tip> {
}
your adapter instance
myAdapter = new MyAdapter(myData,this);
implement onClick Method in your fragment
void onClick(int position, Tip tip){
// your listener
}

In Recylerview Adapter Class I am adding item in arraylist and want to use it in another recylerview adapter class

I am adding data into an addToSepetims arraylist and I want to use it in another fragment but I cant get this arraylist
public class productAdapter extends RecyclerView.Adapter<productAdapter.ViewHolder> {
ArrayList<product_bilgileri> product_bilgileris = new ArrayList<product_bilgileri>();
LayoutInflater layoutInflater;
Context context;
ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
public productAdapter(ArrayList<product_bilgileri> product_bilgileris, Context context) {
this.product_bilgileris = product_bilgileris;
this.context = context;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.productName.setText(product_bilgileris.get(position).getProductname());
holder.description.setText(product_bilgileris.get(position).getDescription());
holder.imageView.setImageDrawable(context.getResources().getDrawable(product_bilgileris.get(position).getImage()));
holder.ratingBar.setRating((product_bilgileris.get(position).getRating()));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a =holder.productName.getText().toString();
String b =holder.description.getText().toString();
int c = R.drawable.iphone5;
//Toast.makeText(context, ""+a, Toast.LENGTH_SHORT).show();
addToSepetims.add(new AddToSepetim(a,b,c));
}
});
}
This is Where I want to use addToSepetims arraylist :
public class SepetimFragment extends Fragment {
RecyclerView recyclerView;
ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
public SepetimFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_sepetim, container, false);
recyclerView = view.findViewById(R.id.products_recylerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
SepetimAdapter sepetimAdapter = new SepetimAdapter(addToSepetims,getActivity());
recyclerView.setAdapter(sepetimAdapter);
return view;
}
}
I have also SepetimAdapter Class
public class SepetimAdapter extends RecyclerView.Adapter<SepetimAdapter.ViewHolder> {
ArrayList<AddToSepetim> addToSepetims = new ArrayList<AddToSepetim>();
LayoutInflater layoutInflater;
public SepetimAdapter(ArrayList<AddToSepetim> addToSepetims, Context context) {
this.addToSepetims = addToSepetims;
this.context = context;
}
Context context;
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.row_product_sepetim,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.productName.setText(addToSepetims.get(position).getProductname());
holder.description.setText(addToSepetims.get(position).getDescription());
holder.imageView.setImageDrawable(context.getResources().getDrawable(addToSepetims.get(position).getImage()));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// addToSepetims.remove(addToSepetims.get(position));
}
});
}
#Override
public int getItemCount() {
return addToSepetims.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView productName,description;
ImageView imageView;
Button button;
public ViewHolder(#NonNull View itemView) {
super(itemView);
productName = itemView.findViewById(R.id.product_Name);
description = itemView.findViewById(R.id.descripton);
imageView = itemView.findViewById(R.id.product_pic);
button = itemView.findViewById(R.id.deleteFromCard);
}
}
}
Second QUESTION :
Product adapter class in onBindViewHolder class :
int c = R.drawable.iphone5;
in this line I want to get holder.imageView instead of "R.drawable.iphone5"
How can I do that ?
You can share data between fragments through a ViewModel:
Add dependency in build.gradle (Module:app) level.
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
implementation "android.arch.lifecycle:runtime:2.2.0"
Create a custom ViewModel class that extends from AndroidViewModel
Here added the list you want to be shared between fragments.
public class MainViewModel extends AndroidViewModel {
private ArrayList<product_bilgileri> products;
public TempViewModel(#NonNull Application application) {
super(application);
}
public ArrayList<product_bilgileri> getProducts() {
return products;
}
public void setProducts(ArrayList<product_bilgileri> products) {
this.products = products;
}
}
Instantiate the ViewModel instance in activity/fragment
// In activity
MainViewModel mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
// In fragment
MainViewModel mViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
Then set the list of data with mViewModel.setProducts(myList) and retrieve it with mViewModel.getProducts()
For more info, check the documentation. Also check this article.

Recycle view not showing items when I start the app but after going to another fragment and back it works

I have to set up a recycle view inside a fragment. The problem is that when I start the app it shows nothing but if i switch to another fragment and go back all items are there. If i start the app and try to scroll through a the recycle view the first item and the last one will load. I tried searching for an answer but there is nothing useful. I mention i just started working in Android Studio.
That's the fragment code
public class Main_fragment extends Fragment {
TextView titleText;
ArrayList<String> titles = new ArrayList<>();
// Access it from anywhere
static String FoodChoiceVar = "All food"; // Value set for test only. Maybe make an ENUM instead?
public Main_fragment() {
// Needed empty constructor.
}
// Don't edit this method as you won't be able tu use "R".
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titles.add("Breakfast");titles.add("Morning Snack");titles.add("Lunch");titles.add("AfterNoon Snack");titles.add("Dinner"); titles.add("Night Snack");
}
//Use this instead.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, container, false);
final FragmentActivity c = getActivity();
titleText = view.findViewById(R.id.textViewFoodType);
titleText.setText(FoodChoiceVar + "| MENU");
RecyclerView recyclerView = view.findViewById(R.id.fragmentRecycleView);
LinearLayoutManager layoutManager = new LinearLayoutManager(c);
recyclerView.setLayoutManager(layoutManager);
ViewAdapter adapter = new ViewAdapter(c,titles);
recyclerView.setHasFixedSize(false);
recyclerView.setAdapter(adapter);
return view;
}
The ViewAdapter
public class ViewAdapter extends RecyclerView.Adapter<FoodMenuViewHolder> {
private LayoutInflater layoutInflater;
private List<String> titleData;
public ViewAdapter (Context context,List<String> titleData){
this.layoutInflater = LayoutInflater.from(context);
this.titleData = titleData;
}
#NonNull
#Override
public FoodMenuViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = layoutInflater.inflate(R.layout.layout_menu_item,viewGroup,false);
FoodMenuViewHolder holder = new FoodMenuViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull FoodMenuViewHolder foodMenuViewHolder, int i) {
String title = titleData.get(i);
foodMenuViewHolder.titleText.setText(title);
}
#Override
public int getItemCount() {
return titleData.size();
}
}
The view holder
public class FoodMenuViewHolder extends RecyclerView.ViewHolder {
public TextView titleText;
public FoodMenuViewHolder(#NonNull View itemView) {
super(itemView);
titleText = itemView.findViewById(R.id.ItemText);
}
}
I have captured 2 short videos to show what is happening
https://drive.google.com/file/d/17wvlhPUT5R0SN38C2TbgBv_AP8awQUI3/view?usp=sharing
https://drive.google.com/file/d/1LJ0Xg4p8Q4a4nyGPSHdVQhBlYOYYi1Pv/view?usp=sharing
I think that your adapter it's losing itself, so try to get the context for layout inflater from viewgroup in onCreateViewHolder.
this:
public FoodMenuViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new FoodMenuViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_menu_item, viewGroup, false);
}
And to avoid anoying error, put a execption in getItemCount().
This:
#Override
public int getItemCount() {
if(titleData != null) {
return titleData.size();
} else {
return 0;
}
}
And try to use context instead of Fragment Activity
final FragmentActivity c = getActivity();
to
final Context c = getContext();

RecyclerView: No adapter attached; skipping layout Error

I have referred quite few posts regarding this error but none of them could able to solve my issue. I have a fragment where I get my values using retrofit which I have shown below:
public class Tab extends Fragment
{
public String nlink;
public RecyclerView recycler;
public List<Initialise> item;
public Adapter adapter;
public Context mContext;
public Tab(Context mContext)
{
this.mContext=mContext;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View v= inflater.inflate(R.layout.tab_layout,container,false);
item = new ArrayList<>();
recycler=(RecyclerView)v.findViewById(R.id.recycler);
recycler.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recycler.setLayoutManager(llm);
recycler.setItemAnimator(new DefaultItemAnimator());
adapter = new Adapter(item,mContext);
recycler.setAdapter(adapter);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
Bundle bundle=getArguments();
nlink=bundle.getString("link");
Retrofit adapter = new Retrofit.Builder()
.baseUrl(ItemAdapter.ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Service Serviceapi = adapter.create(Service.class);
Serviceapi.getitem(nlink).enqueue(new Callback<Item>()
{
#Override
public void onResponse(Call<Item> call, Response<Item> response)
{
for(int i=0;i<response.body().getitems().size();i++)
{
item.add(new Initialise(response.body().getitems().get(i).getTitle(),response.body().getitems().get(i).getDate(),response.body().getitems().get(i).getcount(),response.body().getitems().get(i).getImage()));
}
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<Item> call, Throwable t)
{
}
});
}
}
And the following is my RecyclerView Adapter class:
public class Adapter extends RecyclerView.Adapter<Adapter.Holder>
{
static class Holder extends RecyclerView.ViewHolder
{
CardView cv;
ImageView thum_img;
TextView thum_time;
TextView thum_count;
TextView thum_text;
Holder(View itemView)
{
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
thum_img = (ImageView)itemView.findViewById(R.id.thum_img);
thum_time = (TextView)itemView.findViewById(R.id.thum_time);
thum_count = (TextView)itemView.findViewById(R.id.thum_scount);
thum_text = (TextView)itemView.findViewById(R.id.thum_text);
}
}
private List<Initialise> item;
private Context mContext;
public Adapter(List<Initialise> item, Context mContext)
{
this.item=item;
this.mContext=mContext;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_layout, parent, false);
return new Holder(v);
}
#Override
public void onBindViewHolder(Holder holder, int position)
{
holder.thum_text.setText(item.get(position).gettitle());
holder.thum_time.setText(item.get(position).gettime());
holder.thum_count.setText(item.get(position).getcount());
Picasso.with(mContext).load(item.get(position).getstoryImage()).resize(120, 60).into(holder.thum_img);
}
#Override
public int getItemCount()
{
return item.size();
}
}
I'm getting the following error:
E/RecyclerView: No adapter attached; skipping layout
My cardview is not getting displayed and the viewpager is empty. Where am I going wrong ?
Try to edit onCreateViewHolder() method
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab_layout,parent,false);
Holder rcv = new Holder(layoutView,mContext);
return rcv;

RecyclerView freezes when start to scroll after restoring from background

Here is my adapter class
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);
}
}
}
My fragment
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);
}
When fragment just created (onCreateView called) recyclerview works perfectly, but when app goes to background and then restore scroll start to freeze on the first few items and then continue to work smoothly.
When app restore from background onCreateView method is not called so I try to set up adapter for recyclerview in onStart(), like this:
#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);
}
This approach has solved the problem, but it seems like not realy clear way to solve that issue. Since for example, restoring the fragment leads to reset last scrolled position.
Thanks for your help!

Categories

Resources