Display a RecyclerView in Fragment

后端 未结 4 2143
离开以前
离开以前 2020-12-04 06:25

I\'m trying out the new RecyclerView in Android Lollipop and I\'m stuck.

I\'m trying to receive a list, with an icon and a TextView to the

相关标签:
4条回答
  • 2020-12-04 06:49

    You should retrieve RecyclerView in a Fragment after inflating core View using that View. Perhaps it can't find your recycler because it's not part of Activity

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_artist_tracks, container, false);
        final FragmentActivity c = getActivity();
        final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(c);
        recyclerView.setLayoutManager(layoutManager);
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                final RecyclerAdapter adapter = new RecyclerAdapter(c);
                c.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(adapter);
                    }
                });
            }
        }).start();
    
        return view;
    }
    
    0 讨论(0)
  • 2020-12-04 06:51

    Make sure that you have the correct layout, and that the RecyclerView id is inside the layout. Otherwise, you will be getting this error. I had the same problem, then I noticed the layout was wrong.

        public class ColorsFragment extends Fragment {
    
             public ColorsFragment() {}
    
             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {
    
    ==> make sure you are getting the correct layout here. R.layout...
    
                 View rootView = inflater.inflate(R.layout.fragment_colors, container, false); 
    
    0 讨论(0)
  • 2020-12-04 07:00

    This was asked some time ago now, but based on the answer that @nacho_zona3 provided, and previous experience with fragments, the issue is that the views have not been created by the time you are trying to find them with the findViewById() method in onCreate() to fix this, move the following code:

    // 1. get a reference to recyclerView
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    
    // 2. set layoutManger
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
    // this is data fro recycler view
    ItemData itemsData[] = { new ItemData("Indigo",R.drawable.circle),
            new ItemData("Red",R.drawable.color_ic_launcher),
            new ItemData("Blue",R.drawable.indigo),
            new ItemData("Green",R.drawable.circle),
            new ItemData("Amber",R.drawable.color_ic_launcher),
            new ItemData("Deep Orange",R.drawable.indigo)};
    
    
    // 3. create an adapter
    MyAdapter mAdapter = new MyAdapter(itemsData);
    // 4. set adapter
    recyclerView.setAdapter(mAdapter);
    // 5. set item animator to DefaultAnimator
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    

    to your fragment's onCreateView() call. A small amount of refactoring is required because all variables and methods called from this method have to be static. The final code should look like:

     public class ColorsFragment extends Fragment {
    
         public ColorsFragment() {}
    
         @Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
    
             View rootView = inflater.inflate(R.layout.fragment_colors, container, false);
             // 1. get a reference to recyclerView
             RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.list);
    
             // 2. set layoutManger
             recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    
             // this is data fro recycler view
             ItemData itemsData[] = {
                 new ItemData("Indigo", R.drawable.circle),
                     new ItemData("Red", R.drawable.color_ic_launcher),
                     new ItemData("Blue", R.drawable.indigo),
                     new ItemData("Green", R.drawable.circle),
                     new ItemData("Amber", R.drawable.color_ic_launcher),
                     new ItemData("Deep Orange", R.drawable.indigo)
             };
    
    
             // 3. create an adapter
             MyAdapter mAdapter = new MyAdapter(itemsData);
             // 4. set adapter
             recyclerView.setAdapter(mAdapter);
             // 5. set item animator to DefaultAnimator
             recyclerView.setItemAnimator(new DefaultItemAnimator());
    
             return rootView;
         }
     }
    

    So the main thing here is that anywhere you call findViewById() you will need to use rootView.findViewById()

    0 讨论(0)
  • 2020-12-04 07:02

    I faced same problem. And got the solution when I use this code to call context. I use Grid Layout. If you use another one you can change.

       recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));
    

    if you have adapter to set. So you can follow this. Just call the getContext

      adapter = new Adapter(getContext(), myModelList);
    

    If you have Toast to show, use same thing above

       Toast.makeText(getContext(), "Error in "+e, Toast.LENGTH_SHORT).show();
    

    Hope this will work.

    HappyCoding

    0 讨论(0)
提交回复
热议问题