JsBoon
JsBoon

Reputation: 45

Recycler View Does not works in Fragment

I have been making an app that uses a recyclerView in a Fragment but the contents of the recyclerView have not been showing up. It shows:

 'No adapter attached; skipping layout'.

Featured Helper Class:

public class FeaturedHelperClass {

    int image;
    String title;

    public FeaturedHelperClass(int image, String title) {
        this.image = image;
        this.title = title;
    }

    public int getImage() {
        return image;
    }

    public String getTitle() {
        return title;
    }
}

Fragment:

RecyclerView.Adapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        featuredRecycle = inflater.inflate(R.layout.fragment_dashboard, container, false).findViewById(R.id.explore_recycle);
        recycle();
        return inflater.inflate(R.layout.fragment_dashboard, container, false);
    }

private void recycle(){

        featuredRecycle.setHasFixedSize(true);
        featuredRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));

        ArrayList<FeaturedHelperClass> featuredLocations = new ArrayList<>();

        featuredLocations.add(new FeaturedHelperClass(R.drawable.mcdonald, "Mcdonald's"));
        featuredLocations.add(new FeaturedHelperClass(R.drawable.kfc, "KFC"));
        featuredLocations.add(new FeaturedHelperClass(R.drawable.starbucks, "Starbucks"));

        adapter = new FeaturedAdapter(featuredLocations);
        featuredRecycle.setAdapter(adapter);
    }


Upvotes: 1

Views: 104

Answers (3)

rahat
rahat

Reputation: 2056

The below will work for you, but it is not a good approach to delay the inflation of view in the fragment. Since you are doing some tasks before returning the view, it delays view inflation.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
         featuredRecycle = view.findViewById(R.id.explore_recycle);
         recycle()
         return view;
   }

Doing the following would be an optimal approach,

//inflate the view and return
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
         return view;
   }

//this callback is just after the view inflation, you set your ui here
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
             featuredRecycle = view.findViewById(R.id.explore_recycle);
             recycle()
   }

Upvotes: 1

Guhanmuthu Selvaraj
Guhanmuthu Selvaraj

Reputation: 253

Reason for the 'No adapter attached; skipping layout'. after the view inflation recycler view adapter is not attached.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        featuredRecycle = inflater.inflate(R.layout.fragment_dashboard, container, false).findViewById(R.id.explore_recycle);
        recycle();
        return inflater.inflate(R.layout.fragment_dashboard, container, false);
    }

because recycle() method will be executed before the inflation of the view.

we can change this into

//inflate the view and return
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
         return view;
   }

//this callback is just after once the view get inflated in the screen
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
             featuredRecycle = view.findViewById(R.id.explore_recycle);
             recycle();
   }

now we get the featuredRecycle view and recycle() logic will be executed recycler will be drawn.

Upvotes: 0

Dhanuesh
Dhanuesh

Reputation: 1596

Your inflation logic seems to be flawed, Try changing the following in your onCreateView

featuredRecycle = inflater.inflate(R.layout.fragment_dashboard, container, false).findViewById(R.id.explore_recycle);
recycle();
return inflater.inflate(R.layout.fragment_dashboard, container, false);

into

View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
featuredRecycle = view.findViewById(R.id.explore_recycle);
recycle();
return view;

Upvotes: 1

Related Questions