Erstellt euch ein neues Projekt und wählt Basic Activity, das erleichtert uns schon etwas – das Layout wird uns erstellt – und wir brauchen nicht ganz so viel schreiben.
Wenn AndroidStudio soweit ist, kümmern wir uns als erstes um die compile Importe.
{Projekt-Name}/app/build.gradle
compile ‚com.android.support:appcompat-v7:25.0.1‘
compile ‚com.android.support:cardview-v7:25.0.1‘
compile ‚com.android.support:recyclerview-v7:25.0.1‘
compile ‚com.android.support:design:25.0.1‘
compile ‚com.squareup.retrofit2:retrofit:2.1.0‘
compile ‚com.squareup.retrofit2:converter-gson:2.1.0‘
compile ‚com.squareup.okhttp3:okhttp:3.4.1‘
Ergänzt in der AndroidMainfest.xml der Berechtigung:
<uses-permission android:name="android.permission.INTERNET"/>
In dem von AS erstelltem Layout content_main.xml, müssen wir unser RecyclerView hinzufügen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/content_main2"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.astra_g.mysqldatabaseapi.MainActivity"
android:orientation="horizontal"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</LinearLayout>
Ein Layout müssen wir noch erstellen card_view_row.xml, wir verwenden unser eigenes CardView-Layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/textViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="id"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@+id/textViewManufacturer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Manufacturer"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textViewType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>