Office Hours — Today, April 28

Yesterday, April 27

Apr 28
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:30 PM
Lucy
has entered the room
Lucy
Hello!
Mark M.
hello, Lucy!
how can I help you today?
Lucy
View paste
I’m using a ViewPager with FragmentStatePagerAdapter for the nice UI for the user to go through a list of items.

The ViewPager takes one Fragment class where that Fragment receives a String arg and displays it in a TextView in the Fragment.  A TimePicker is also in the Fragment to pick a time and display it in a second TextView upon setOnTimeChangedListener.
So that's the setup of what I want. Here comes the problem:
I want to, upon swipe of the ViewPager, grab the time chosen and grab the text from the String arg and give both back to the Activity together. Then the Activity will “do something” with all the values collected. But I’m having difficultly how to extract the TextView and TimePicker values out of Fragment which is inside the ViewPager and its Adapter.
Mark M.
I am very much "not a fan" of having swiping of pagers do anything other than change the page that the user sees
that being said...
Lucy
I wondered about that...ViewPager seems nice for read-only.
7:35 PM
Lucy
I'm open to suggestions for a different UI approach.
Mark M.
I am not aware of any lifecycle-ish methods being called on a page as it is being swiped out from the visible area of the ViewPager
Lucy
I see.
Mark M.
and with FragmentPagerAdapter/FragmentStatePagerAdapter, you don't really have access to the fragments once your adapter hands them over to the superclass
with my ArrayPagerAdapter, this would be easier
you can find out your current position from the ViewPager once the swipe is finished, then ask ArrayPagerAdapter for the fragment at that position
Lucy
Excellent I will look at that now. I tried very hard over the weekend to make ViewPager work but I simply couldn't get those values! I worried about the Lifecycle as well.
Mark M.
then, it's merely a matter of the activity calling some method on that fragment
Lucy
Oh! I see. ViewPager with different adapter.
Mark M.
right
the stock PagerAdapter implementations work, and are fine for simple scenarios, but are not particularly flexible
Lucy
If nothing else, I learned how to use FragmentStatePagerAdapter. Your book was excellent.
Mark M.
ArrayPagerAdapter is a bit like a mashup of ArrayAdapter and FragmentPagerAdapter
glad you liked it!
Lucy
That's just the mashup I need.
Mark M.
there's a section on ArrayPagerAdapter in the Advanced ViewPager chapter
Lucy
Excellent. I'm opening it now.
7:40 PM
Lucy
I see the info in the book. I got so stuck into what I had written that I didn't get to this info.
7:50 PM
Mark M.
BTW, if you have other questions, go right ahead -- it's a quiet chat room tonight
Lucy
Thanks I did have one about support libraries. I wish to support only 4.1 and above. I'm using:
View paste
dependencies {
    compile 'com.android.support:support-v13:21.0.3'
}
7:55 PM
Lucy
That's so I can use ViewPager. I noticed I can declare Fragments for use with Activity and I don't have to declare FragmentActivity.
Mark M.
yes, support-v13 has Fragment*PagerAdapter implementations for both native fragments and the fragments backport
Lucy
OK, thanks. I realized after I started using it that I didn't need FragmentActivity.
Mark M.
note, though, that nested fragments is not supported by the native fragments implementation until API Level 17
so if your ViewPager has fragments for pages, and the ViewPager itself is *in* a fragment, you will need to stick to the backport
Lucy
I notice that Android Studio will create a Fragment inside the Activity file if I choose so. Is that how people typically organize their code? I usually have an Activity .java file and a Fragment .java file.
Oh! I see what you mean.
Mark M.
it depends on the complexity of the fragment
a small fragment class could work as a static class inside the activity
but bigger ones probably deserve their own Java source file
Lucy
But if its a static class inside the Activity then I can't use it in other Activities. Would that be another reason to keep them separate?
Mark M.
"But if its a static class inside the Activity then I can't use it in other Activities" -- well, not if it's public
8:00 PM
Mark M.
or even package-private, if the activities are in the same package
Lucy
OK! I may try reorganizing into that structure.
Mark M.
but that would be another reason to lean towards having the fragments be their own top-level Java classes
Lucy
So far I think my Fragments are being used only by one Activity. My whole app I'm trying to write isn't very big.
Android Studio also creates other helpful things like the OnFragmentInteractionListener in the Fragment. I assume that if I don't end up using them then I should delete them out of the file to make the program smaller in size.
Mark M.
off the cuff, I would agree
I very rarely use the Android Studio new-stuff wizards
I spend more time ripping out code, resources, Gradle entries, etc.
Lucy
Sure, thanks. I've just started using Android Studio so I'm taking the stuff for now...but I'll need to return and clean up.
It has been good for learning. Although I just want Gradle to work. :-)
Those were all my questions that I wrote down. Thank you!
Mark M.
you are very welcome
8:05 PM
Lucy
has left the room
8:20 PM
EGHDK
has entered the room
8:25 PM
Mark M.
hello, EGHDK!
the chat is nearly over -- do you have a quick question?
EGHDK
Hey Mark. Got one or two questions really quick. 1. I've decided on okhttp. But okhttp doesn't seem to have network utillity methods like OkHttp.isNetworkAvailable(); but I want to be able to do that. Are there any libs you know of for making that kind of stuff really straightforward?
Mark M.
sorry, you lost me
EGHDK
It's alright. Maybe it's a dumb question. poorly worded. I'll try later. second question.
I read about interfaces today, and it just seems like everything should be an interface. Am I just going crazy or is that a sort of logical thing to do?
Mark M.
that's kinda overkill
I mean, you're welcome to implement that if you want
EGHDK
Yeah, it just seems like for testing out stuff, it would be easy to use an interface to throw fake objects in there
Mark M.
that is one reason to use interfaces
however, not *everything* necessarily needs to be mocked that way
for example, suppose you are implementing a ListView or RecyclerView
and you are using the ViewHolder pattern
8:30 PM
Mark M.
it's unlikely that you need to create a mock ("fake") ViewHolder
and hence it may not be necessary to create an interface just for that ViewHolder
EGHDK
Yeah, I'm just having trouble understanding why the tutorial for unit testing support on tools.android.com shows an example of importing junit and mockito... and I have no idea what mockito even is
And okay, yeah the viewholder example makes sense
Mark M.
it's a library designed to help you create "fake objects", to use your term :-)
the term for creating fake objects in testing is called "mocking"
EGHDK
Hm. Why can't I make them on my own?
A library seems like overkill. Sorry out of time. SOrry for being late. I'll be back with question next time.
Mark M.
and that's a wrap for today's chat
the next one is Friday at 9am US Eastern
have a pleasant day!
EGHDK
thanks mark
EGHDK
has left the room
Mark M.
turned off guest access

Yesterday, April 27

 

Office Hours

People in this transcript

  • EGHDK
  • Lucy
  • Mark Murphy