0

Who knows about programming for iOS and data source/delegate paradigm will understand me.

The best practice for implementing custom view in iOS:

  1. Implement MyView class inherited from UIView
  2. MyView has dataSource field.
  3. On drawRect: method MyView asks dataSource for items to draw.
  4. dataSource object conforms to required protocol and implements needed methods.

It is very similar how is implemented UITableView, but I am not talking right now about cells.

Could you please let me know the best practices to implement custom view (like MyView) in Android with MVC pattern?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Droid
  • 1
  • 1
  • 1
  • Try be a little more specific and change the title else it might get closed as a duplicate of the question in the link I provided. – rogermushroom Jan 09 '12 at 17:28
  • Check out my blog posts on this starting here [Android Architecture: MV?](http://doridori.github.io/Android-Architecture-MV%3F/) – Dori Apr 03 '15 at 17:33

2 Answers2

4

See MVC pattern on Android

Read all the answers to get a rounded view of android patterns as I don't find the first one very helpful.

Design decisions have been made regarding fundamental application components which prevent pure MVC being implemented, for example the use of activities which don't allow decoupling of the layers.

I find viewing android apps in a MVC way confuses matters as you end up misusing components like activities and adapters by trying to adapt them to perform a function they weren't really designed to do.

UPDATE

You need to make the question a little more specific to get a good answer, provide details of what sort of view you require (list, grid, simple) and what sort of models you are using (where are they persisted etc).

Although it's fairly subjective I have found the following when programming with Android:

Models tend to end up being unavoidably dumb (the anemic anti-pattern). This can be because in many occasions you will find the adapter is passed content to present in the view in the form of an object or collection and then operations on those objects or collections should be performed through the adapter so the adapter is aware of when they are changed and can manage the view accordingly.

The adapter can be treated as a link between model and view, a controller if you like but without many of the advantages of a pure MVC controller. I have found that customising the adapter can be effective (although you end up with 'fat controllers' if you are viewing it from an MVC perspective). Any UI input which would invoke changes to the content can then be called by adapter methods which will edit the models using add/remove for lists, or cursor operations for the database etc.

Community
  • 1
  • 1
rogermushroom
  • 5,486
  • 4
  • 42
  • 68
  • Thanks for response and provided link. I still would be happy to know what is the best way to implement custom view in Android. How should I give my custom view a model? (via list adapter?) What is the best solution: model draws itself in view or view get some properties from model and draws it? – Droid Jan 09 '12 at 16:20
0

To implement a custom view in Android, you should derive from an existing View, then override the methods you need, which will often include onDraw(), onMeasure(), and probably one or more event handlers1.

To implement something in the spirit of MVC, the data that your component represents should not be in the view class itself but instead in other classes. The exact design is up to you: you can have data stored in files, in Java objects, pull it from a Content Provider2, etc.

When drawing (override onDraw()), you should look at the state of your data and draw accordingly;

When handling an event that alters the data, alter the data in the model and call invalidate() on the view to request that it be redraw to reflect the changes.

Bruno Oliveira
  • 5,056
  • 18
  • 24