简体   繁体   中英

Reference a Recyclerview that was in a fragment in Appcompat activity?

Hey so I have a recyclerview and a recyclerview adapter as shown below. However, this recyclerview adapter was inflated inside an activity that extends fragment. This time I want to use it in an activity that extends AppCompatActivity, How do I do that?

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view=inflater.inflate(R.layout.fragment_dashboard,container,false);
    mRecyclerView=(RecyclerView)view.findViewById(R.id.ID);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mAdapterDashBoard=new AdapterDashBoard(this);
    mRecyclerView.setAdapter(mAdapterDashBoard);
    return view;

just use the same code inside your onCreate() method of the Activity

// Initialize recycler view
mRecyclerView = (RecyclerView) findViewById(R.id.ID);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapterDashBoard=new AdapterDashBoard(this);
mRecyclerView.setAdapter(mAdapterDashBoard);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM