The target

90% of the actual work of most enterprises in App development requirements, and RESTful API interface interaction data and presentation requirements, this tutorial to achieve such basic requirements, based on the rapid framework Genos link

It takes anywhere from a day to two weeks to fully master Genos, and then you can go and look for a job.

Trust me, you’ll make a more stable and beautiful App with less code than half of the Android programmers on the market.

Why use Genos, see after the tutorial


Start this tutorial

The complete code V2EXSample see https://github.com/nyssance/genos-samples

Step zero (1 minute to 1 day to quit): download Java8 andAndroid Studio 3.0.1Learn a little Java

Step 1 (1 minute, depending on the severity, 5-30 minutes for obsessive-compulsive patients): Create a new project

Start a new project. Official Guide

Screen Configure
Target Android Devices Phone and Tablet : API 17
Add an Activity to Mobile Empty Activity

Gradle Scripts: build.gradle (Module: app)_.

dependencies {
    implementation fileTree(dir: 'libs'.include: ['*.jar'])
    // Replace with Genos
    / / implementation 'com. Android. Support: appcompat - v7:26.1.0'
    / / implementation 'com. Android. Support. The constraint, the constraint - layout: 1.0.2'
    implementation 'com. Nyssance. Genos: genos: 1.0.0 - rc2'.Copy the code

Then sync, about the use of Gradle in Android knowledge, and if the wall is how to solve the problem, you can own baidu

Step 2 (2 minutes): Create the model class

  • Visit V2EX hottest topic address https://www.v2ex.com/api/topics/hot.json
  • Copy and take the first data, Posted on http://www.jsonschema2pojo.org/, the Source type to choose JSON, the Annotation style selected Gson, selected to Use primitive types. Do not Include Getters and setters in this tutorial.
  • (In real work, all model classes should be generated by the server and provided to the client programmer. If your company does not do this and considers this to be the client’s own work, well, consider switching to another company, the engineering and management level is too weak.)
  • For iOS: Gson is the ObjectMapper for Android

Step 3 (5 minutes): Write the necessary classes

If you are a Mac, move the cursor to each of the areas marked red, and thenAlt + enter, will automatically complete the required operations

Appservice.java (in practice, this class should not be written by hand, but should be generated by the server side according to the format, since the API is provided by the server side)

public interface APIService {
    @GET("topics/{pk}.json")
    Call<List<Topic>> topicList(@Path("pk") String pk);

    @GET("nodes/show.json")
    Call<Node> nodeList(@Query("name") String name);

    @GET("members/show.json")
    Call<Member> memberList(@Query("id") int id);
}
Copy the code
  • Read more: Retrofit
    • For iOS: Retrofit is the Alamofire of Android

AppManager.java

public class AppManager extends BaseAppManager {
    private static final AppManager INSTANCE = new AppManager();
    public static APIService API;

    private AppManager(a) {
        super(a); }public static AppManager getInstance(a) {
        return INSTANCE;
    }

    @Override
    public void settings(a) {
        BASE_URL = "https://www.v2ex.com/api/";
        // Create retrofit
        API = onCreateRetrofit().create(APIService.class);
    }

    @Override
    public void route(Fragment fragment, String uri) {}}Copy the code

TopicList.java

public class TopicList extends TableList<Topic.SubtitleHolder> {

    @Override
    protected void onPrepare(a) {
        mCall = API.topicList("hot"); / / the hottest
        mTileId = R.layout.list_item_subtitle; // You can define your own layout, define the style of each line, and change the corresponding Holder to your own
    }

    @Override
    protected void onDisplayItem(Topic item, SubtitleHolder holder, int viewType) {
        holder.title.setText(item.title);
        holder.subtitle.setText("id: " + item.id);
        holder.setImage(holder.icon, "https:" + item.node.avatarNormal);
    }

    @Override
    protected void onOpenItem(Topic item) {
        Snackbar.make(mListView, "Replace with your own action", Snackbar.LENGTH_SHORT).show();
        // startActivity and so on
    }

    @Override
    protected void loadMore(int size, int position) {
        V2EX API (V2EX API, V2EX API, V2EX API, V2EX API, V2EX API) MCall = api.topicList ("hot", mPage)}}Copy the code
  • TableList is a Fragment with only one element in a row. CollectionList/GridList is used for multiple rows
  • Naming rules Model name +List/ model name +Detail. In most cases, you only need to extend the List/Detail interface, because the API returns either List or Detail (standard Create and Update pages will be provided later). In addition, according to business requirements, you may also partition some of your own pages according to DDD, which may be easier to understand on the server side
  • V2EX returns data as a direct list, which can be inherited if your interface returns packages in datagenos.ui.fragment.ListFragmentWrite your own TableList, and then write your own ListModel, overwriting the TransformListFromData method

Step 4 (2 minutes): Modify the necessary classes

MainActivity.java

public class MainActivity extends TabBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragments.append(R.id.navigation_hot, new TopicList());
        mFragments.append(R.id.navigation_latest, PlaceholderFragment.newInstance(2));
        mFragments.append(R.id.navigation_me, PlaceholderFragment.newInstance(3)); onNavigationItemSelected(R.id.navigation_hot); }}Copy the code
  • TabBarActivity is the bottom navigation that requires sidebar navigation, inheritedgenos.ui.activity.DrawerActivityCan be
  • The default menu name for TabBarActivity is NAVIGation_tab_bar in RES /menu. DrawerActivity is navigation_draweronCreateOptionsMenuInflate the
  • XXXListActivity will automatically search for the menu and string named list_XXX as the menu and title, or you can assemble it manually
  • Fragment Because the name will be confused, all Settings need to be manual

Step 5 (1 minute) Run

You’re an Android expert now