High Level ViewPager

Having understand the baisc knowledge of the viewpager, we will go deeper about the viewpager in this post.

Firstly, we should develop the conclusion that: we don't need to update the viewpager, we only need to develop the mechanism to update the fragment in the viewpager

For example the following swipe to refresh is a scheme to update the recycler view

https://guides.codepath.com/android/Implementing-Pull-to-Refresh-Guide

back to the view pager, we can think several questions to make us understand all the staff in this area

  1. Firstly, how to show the tab in our toolbar
    we need use the TabLayout inside our android widget toobar layout

  2. Secondly, how to make fragment into our viewpager
    viewpager like the recyclerview is just a view, so we need to manage it using the data structure — adapter

  3. thirdly, how to show the title in our TabLayout, so during our scroll, we can know clearly which page we are currently at.

The solution for the previous three question is:

use a self defined viewpager, which can store the pager and the title, especially use the getTitle() method to return the title string and put them in the tablayout

continue our question, we have talked that we use the viewpageradapter to delegate for all the tasks to the viewpageradapter, but how could we delegate the work

The previous image has shown how we delegate all the work to the adapter

we have already talked about the delegation and callback, then we need to go deeper about the callback mechanism design

IT IS THE DESIGN PATTERN

  1. Since we still based all the operation logic on the recycler view and the recycler view was put in a fragment, so our fragment also want to delegate all the operation of click behavior on the list item to the adapter.

we all know that the viewholder is necessary in the recycler view, and each item (holder) will get interaction at the onBindViewHolder phase. So this is why we put the interaction in the previous function.

the fragment and the adapter should be in the same level, but the dummy and dummy content should be put in the inner package

Then it is defined in the fragment class

Another big point in this section is we have merged the handlerthread into this part to do some time consuming work (to simulate the swipe to refresh)

to make the handler thread work, or in other words, to make the swipe operation possible, we should find the swipe layout firstly

the adapter view is generally special, although it has the work to request for the view component, BUT THEY REALLY DO IS CLOSELY TO THE HODLER IT SELF, SO WITHOUT THE ADAPTER, EACH ITEM CAN BE SHOWN CLEARLY, THIS IS WHY HE ADAPTER IS REALLY IMPORTANT TO REQUEST FOR THE VIEW COMPONENT

But the layout file is inflated by the fragment part, so we should visit or access the view component in the fragment part [NOT THE VIEW PAGER ACTIVITY PART, SINCE WE ALREADY HAVE TALKED THAT THE VIEW PAGER SHOULD NOW BE UPDATED]

TO update the recycler view is really easy, we only need the following two things


The notifyDataSetChanged() at this level never fail, it is much easy than the viewpager's notifyDataSetChanged()

Back to our handler thread, in our fragment, we have created the following class to derive from the handerthread to do the task sequentially

it also related to some design patten staff, we have already talked about the handler, so we will not talk about it.

similar to we create the workerhandler object at general activity where we pass the new Handler() as the parameter, since this is the phase where the ui thread is created, so the looper can be set automatically

and our WokerHandlerThread looks like the following

what does the worker do: DISPATCH THE MESSAGE

I JUST DISPATCH IT, I HOPE SOMEONE ELSE HAVE THE ABILITY TO PROCESS IT
SO I CREATE A CALLBACK TYPE AND HOPE SOMEONE PROVIDE THE SCHEME TO PROCESS IT

who have me? Current fragment, so current fragment should implement this interface

and we have a really bad time to recycler the message, the exception is: the message is still used while being recycled

since don't use message.recycle() by yourself, the looper will handle it

other function to implement the real function of some behavior is:

And toward those action function we have the following very important conclusion

Finally, emphasis it again, if we really want a function in our data structure, for example the fragment's list's interaction, workerhandler's after dispatched's behavior

WE WANT SOME SPECIFIC OPERATION MODE, WE CREATE THE LISTENER AND WE HOPE SOMEONE ELSE PASS THE LISTENER TO ME, AND THE LISTENER DO WHAT IT SHOULD DO IN THE CASE

the adapter is the recycler view's delegate, so it should implement fragment needs interaction operation

the fragment itself want to use the handler thread, so it self should implement the handler's callback

thats it

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s