Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
Setting Up NerdLauncher

Finally, add a new class named NerdLauncherFragment that extends from
android.support.v4.app.Fragment. Add a newInstance() method and override onCreateView(...)
to stash a reference to the RecyclerView object in a member variable. (You will hook data up to the
RecyclerView in just a bit.)


Listing 24.2  Basic NerdLauncherFragment implementation
(NerdLauncherFragment.java)


public class NerdLauncherFragment extends Fragment {


private RecyclerView mRecyclerView;


public static NerdLauncherFragment newInstance() {
return new NerdLauncherFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_nerd_launcher, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.app_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


return v;
}
}


Run your app to make sure everything is hooked up correctly to this point. If so, you will be the proud
owner of an app titled NerdLauncher, displaying an empty RecyclerView (Figure 24.3).


Figure 24.3  NerdLauncher beginnings

Free download pdf