Binding fragments in ViewPager

1,426 views
Skip to first unread message

Csaba Troják

unread,
Jul 17, 2013, 1:31:24 PM7/17/13
to android...@googlegroups.com
Dear Andy,

I'd like to implement an activity which contains a viewpager and other controls (eg. displaying the currently displayed view-index by the viewpager).

I followed this article to implement such feature:


Then I wanted to incorporate AndroidBinding's MVVM to both my main-activity and the displayed subviews (as fragments), though I read that you decided not to support AndroidBinding with Fragments, but in this forum I've found some advices you gave to others how to use binding inside fragments.

I would like an opinion from you whether my approach is the way to go to implement such scenario, or whether you recommend another approach (again, the viewpager should not be fullscreen).

- the MainActivity extends FragmentActivity (coming from the dev.android.com example) - can't change this to any BindingActivity, because the ViewPager setup needs getSupportFragmentManager()
- in the MainActivity's onCreate I can't call the default "setContentView(R.layout.activity_main);" because I want binding, neither can I call 'setAndBindRootView(R.layout.activity_main, new TestVM(this));' because my main Activity is not a BindingActivity, but FragmentActivity
- so I use the following code:

Binder.InflateResult result =  Binder.inflateView(this, R.layout.activity_main, null, false);
Binder.bindView(this, result, _pagerVM);
setContentView(result.rootView);

- In my fragments' onCreateView I do this:

Binder.InflateResult result = Binder.inflateView(container.getContext(), R.layout.childview, null, false);
return Binder.bindView(container.getContext(), result, _vm);

Also, another question would be, if in the future I'd need to use BindingABSAcitivity (etc), what are my options? BindingABSActivity is not subclass of FragmentActivity, so.... ?!

I checked the ICSDemo's ViewPager layout demo-screen but something might be wrong here, because the whole behavior is a little awkward, not whole pages are scrolled/paged, but many times the screen displayes pages next to each other separated by a divider, etc...

Thanks a lot for your time and waiting for your your kind answer,

Csabi

Andy Tsui

unread,
Jul 18, 2013, 5:02:06 AM7/18/13
to android...@googlegroups.com
Hi,

The best way I suggest is still use no Fragment, and the best way I can imagine is the one in ICSDemo.

> not whole pages are scrolled/paged, but many times the screen displayes pages next to each other separated by a divider, etc...

It's an expected behavior (if I don't get you wrong), look at this:

https://github.com/gueei/AndroidBinding/blob/master/Demo/MarkupDemoICS/res/layout/showdemo.xml

PAGERADAPTER({ source=Demo.Raws, template=@layout/showdemo_xml, titleField='Title', width=0.7})

The width=0.7 makes it not occupying the whole screen, this refers to:


So, you can just remove "width" from the adapter and it will occupies the whole screen

Andy
> --
> You received this message because you are subscribed to the Google Groups
> "AndroidBinding" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to androidbindin...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

Csaba Troják

unread,
Jul 18, 2013, 5:07:13 AM7/18/13
to android...@googlegroups.com
Dear Andy,

1. What's the problem with my approach?
2. Why are you against using fragments and binding to them?

(The answer don't necessarily has the be common ;) ).

Binding to fragments might cause stuff remain in memory forever, or any other drawbacks?

Bye,
Csabi

Csaba Troják

unread,
Jul 18, 2013, 5:36:52 AM7/18/13
to android...@googlegroups.com
Dear Andy,

One more question (besides the one below ;)):

If I want to 
- would like to display my own custom PagerTabStrip-functionality-achieving control (eg. list of small buttons somewhere on the page and if a specific button is clicked the specific page is scrolled in)
- would like to have the possibility for providing buttons to add new pages (and their underlying VMs) also as removing pages (in this case, properly display and scroll the subsequent page)
, is it possible to do with your approach?

With my (more complicated) approach I have access to the pageradapter and viewpager so I can add/remove items and call notifyDataSetChanged, also can scroll to the selected view if needed (eg. I have 5 views, I have my custom 'pagertabstrip; with 5 buttons and the user clicks the 5th button, then I can scroll to the 5th view).

Are these possible easily with your approach?
Is it possible to access the pageradapter and viewpager somehow?

Thanks again for your time,

Bye,
Csabi

On Thursday, July 18, 2013 11:02:06 AM UTC+2, Andy Tsui wrote:

Andy Tsui

unread,
Jul 18, 2013, 8:14:05 AM7/18/13
to android...@googlegroups.com
Hi,

Interesting question. I just updated the library (download it from Github: https://github.com/gueei/AndroidBinding/) and added a new demo for this purpose.


Will blog a new post on this later. 

Andy

Csaba Troják

unread,
Jul 18, 2013, 8:55:23 AM7/18/13
to android...@googlegroups.com
Dear Andy,

Checked it, seems nice, however what I did and need is more:

I want to display UI items for each page (similar relationship how a tabcontrol displays tab for each tabpage), and when these UI items (eg. buttons) are clicked, the specific page is scrolled in.

Of course I would like to use databinding in this 'navigation' control, so somehow I have to access some VMs (not the PageVMs!) which indicate whether they are selected, etc.

If you want I can send you my current prototype, of course it's not that sophisticated, and requires code-behind to set up, which I guess you'd like to avoid...

Question #2: shall I use the newly release .jar file (v718) for production development?

Bye.
Csabi

Andy Tsui

unread,
Jul 18, 2013, 10:41:17 AM7/18/13
to android...@googlegroups.com
You can send me the APK. 

Yes, you can use the latest version.

Andy

Csaba Troják

unread,
Jul 18, 2013, 10:55:45 AM7/18/13
to android...@googlegroups.com
Dear Andy,

Attached the APK.

Bye,
Csabi


2013/7/18 Andy Tsui <guee...@gmail.com>
VPTest-debug-unaligned.apk

Andy Tsui

unread,
Jul 20, 2013, 12:28:42 AM7/20/13
to android...@googlegroups.com
Installed the App, please use the latest library as a bug related to delete page is addressed.

For navigation, frankly I still experimenting the best way to be isolated, not only from view, but also view model and activities.

I am not sure how you organize the stuff in your view pager, but let's explore how I would implement, if for Google Play Store.

The main page of store, shows: HOME, TOP FREE, TOP NEW and so on. Before we have Viewpager, we separated them in different tabs/activities. I will define an application wide (static or singleton) Navigation Controller to handle this. Then, I would utilize the EventAggregator to pass/receive navigation messages. e.g.

For the main Vm (which host those pages)
Define:

Command NavigateToTopFree = new Command(){
  execute(){
      EventAggregator.getInstance(mActivity).publish("navigateToTopFree", null);
  }
}

similar for other pages.

Note, since the navigation here is within activity only, the Navigation controller should register in "activity" context rather than "application" context. 

By using EventAggregator, VMs can communicate with each other without depending on each other. This is particular useful when you have multiple layouts using same VMs, for example, in the MarkupDemoICS, the portrait and landscape is different.. while portrait, the demo is shown in new activity, but for landscape, the demo list and demo is side-by-side. For the demo-list vm, it only triggers a "navigate" command (thru event aggregator), and it's up to the navigation controller to determine how to display. 

Andy
Reply all
Reply to author
Forward
0 new messages