📜 ⬆️ ⬇️

Rapid application development using the Greendroid library



I will show Greendroid in work on the example of the last application developed by me (the client for reading the site , by the way, he recently promoted to Habré .

Many of you have probably seen Google’s patterns for developing android applications. Greendroid is a library that implements the ActionBar pattern and many other interface widgets. It allows you to focus on the application logic, do not think about the creation and layout of ActionBars, list items, and so on.


')
To use this library, you need to use the Theme.GreenDroid theme (or any friend inherited from it) and specify the application class inherited from GDApplication . What is all this for? 1. The theme is needed in order for the application to know how to display custom widgets, for which we actually use Greendroid. 2. I will give an example of the application that is used in my code.

public class ShortikiApplication extends GDApplication { @Override public Class<?> getHomeActivityClass() { return HomeActivity.class; } } 


This is necessary for the ActionBar to know where the starting position (the main page of the application). It's simple, you just need to override the getHomeActivityClass method by returning the class of the Activity we need.

Now you can use all the features of Greendroid. Let's start with ActionBar. Here is a simple example of use.

 public class ShortikiActivity extends GDActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setActionBarContentView(R.layout.list); } } 


Instead of the usual setContentView , we use setActionBarContentView, they do the same thing, except that the latter adds an ActionBar to this layout.

Let's try to add an element to the ActionBar.

 addActionBarItem(ActionBarItem.Type.Refresh); 


As you can see, Greendroid already contains the main elements of ActionBar, as well as the ability to create your own Action. Here is a listing class that speaks for itself.

  public enum Type { GoHome, // A house Search, // A magnifying glass Talk, // A speech bubble Compose, // A sheet of paper with a pen Export, // A dot with an arrow Share, // A dot with two arrows Refresh, // Two curved arrows TakePhoto, // A camera // PickPhoto, // Two pictures with an arrow Locate, // The traditional GMaps pin Edit, // A pencil Add, // A plus sign Star, // A star SortBySize, // Some bars LocateMyself, // A surrounded dot Compass, Help, Info, Settings, List, Trashcan, Eye, AllFriends, Group, Gallery, Slideshow, Mail } 


Now let's try to handle the pressing of some Action, for this you need to override the method

 @Override public boolean onHandleActionBarItemClick(ActionBarItem item, int position) { //   if (position == 0) { //... } return true; } 


As you can see everything is simple.

At this convenience when using Greendroid does not end. This library includes the class ItemAdapter is a descendant of the class BaseAdapter, which makes it very convenient to fill out a sheet.
I will show an example. Filling the sheet with quotes in my application happens like this:

  public ListAdapter createAdapter(List<RssItem> result) { ItemAdapter adapter = new ItemAdapter(this); for (RssItem rssItem : result) { adapter.add(new LongTextItem(rssItem.getDescription())); adapter.add(new SeparatorItem()); } return adapter; } //............ listView.setAdapter(createAdapter(items)); 


I will explain, the Add method should receive as a parameter the heir of the Item class, the heirs of the Item class from the box in Greendroid are sufficient and usually enough, although you can always implement the Item you need. Here are some items already: DrawableItem , LongTextItem , ThumbnailItem , SubtextItem , SubtitleItem , TextItem . For details, please read the library documentation.

Also in the library there are several handy widgets, but this is already in the next post.
Here's what came out of all this: android client for shortiki.com

Thanks for attention.

Source: https://habr.com/ru/post/126480/


All Articles