This app is designed to demonstrate and reinforce the use of fragments in apps.
Start with the provided project. The main activity has sample code on how to use the api tools provided.
I didn't have much time for this code so I just used something that I already had and converted it to work in kotlin for this project. A few of the classes are poorly put together for kotlin and most of them don't use the new tools provided that we'll be teaching you. Don't use it as a reference.
We'll now create a fragment to list our data.
- In your project tab on the left, right click the package name and go to new -> fragment -> Fragment(list)
- There will be a popup window. In that window, replace the value for "Object Kind" with the name of your data object (
CongresspersonOverview
). Note the names of the files it will be creating. Click finish. - You'll see that this will now generate a number of classes and files. In your project tab there is now a new package called
dummy
delete that package and the files it contains. - First, open the New fragment file. You'll see some red error messages.
- In the generated fragment class, you'll see a constructor for the generated list adapter class. You'll need to change the
DummyItem
to your data class. Once that is done follow the constructor to the RecyclerViewAdapter class - Adjust this class and the corresponding layout to work with your data object.
- You'll see a call to
onListFragmentInteraction
along with another error. You'll need to change the signature of that method (found in the new fragment class) to accept your data object
- In your MainActivity layout replace the
TextView
with afragment
view add aname
attribute and set the value as the full package name for your fragment - In the kotlin file. Implement the
OnListFragmentInteractionListener
from your ListFragment and implement the method. This is where you'll build and attach your second fragment. You can use the passed in data object to pass along to the details page
- Create a new
activity_main
layout for displays with thesmallest screen width
of 600
- Copy your old layout to this one. Add a
FrameLayout
for your details fragment, be sure to include an id and size it how you want both fragments to appear - Create another new fragment, but this time select
Fragment (Blank)
DetailsFragment - In the new layout, add fields to display your data
- In the MainActivity's
OnListFragmentInteractionListener
Create a new instance of your detail fragment.
You'll also need to implement that fragment's
OnListFragmentInteractionListener
interface and method
- Create a new
Bundle
object and put the id property for the selected congressperson into it (the one passed in the current method's signature) - Call
setArguments
on your fragment and pass it your bundle. - Start a fragment transaction with
getSupportFragmentManager().beginTransaction()
then use the builder pattern to callreplace
and pass it your Frame layout id, and the fragment object, then callcommit()
- In the detail fragment, call
getArguments
to get the bundle object and thengetString
to get the passed id - Use the provided api method to get that member's details
CongressDao.getMemberDetails(it)
- Use that object to populate your view
This is a bit different from what we did in class, but teaches you some useful tools and skills
- Create a new activity like the main activity with a
FrameLayout
view and in the new activity'sonCreate
method add the detail fragment programatically like you did in steps 5-8 in part 4 - Now we need to determine if the device is big enough for two panels
- Create a new
attr.xml
file. Add a new bool field calledis_tablet
set the value to false - Create another
attr.xml
file, but this time, create it with the same requirement you set in Step 4.1. Add the same bool field but set its value to true - In your main activity's
OnListFragmentInteractionListener
, callgetResources().getBoolean(R.bool.is_tablet)
to get that value. If it is true, add your fragment as in part 4. If not, create an intent and use it to start the second activity, passing your data object as well.
Change the detail activity to be a popup diaplog instead of a separate activity or screen.