Display a RecyclerView in Fragment

后端 未结 4 2162
离开以前
离开以前 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;
    }
    

提交回复
热议问题