Course Content
-
Подготовка
-
Учимся работать с Retrofit и GSON
-
Всё вместе. Создание UI и отображение ответа от сервера
-
Использование RxJava 2.0 для создания вызовов В этом разделе вы научитесь использовать RxJava 2.0 для создания вызовов к серверу и получения ответа
Создание ячейки и адаптера
Для того чтобы вывести полученные фильмы необходимо создать ячейку и адаптер для RecyclerView
Важно! Не забудьте добавить библиотеку RecyclerView в build.gradle dependencies и нажать Sync Now
Создание ячейки
Создадим адаптер с ViewHolder и layout ячейки
class MoviesAdapter(
private val movies: List<Movie>,
private val rowLayout: Int
) : RecyclerView.Adapter<MoviesAdapter.MovieViewHolder>() {
class MovieViewHolder(v: View) : RecyclerView.ViewHolder(v) {
internal var movieTitle: TextView = v.findViewById(R.id.title)
internal var data: TextView = v.findViewById(R.id.subtitle)
internal var movieDescription: TextView = v.findViewById(R.id.description)
internal var rating: TextView = v.findViewById(R.id.rating)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): MovieViewHolder {
val view = LayoutInflater.from(parent.context).inflate(rowLayout, parent, false)
return MovieViewHolder(view)
}
override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val current = movies[position]
holder.movieTitle.text = current.title
holder.data.text = current.releaseDate
holder.movieDescription.text = current.overview
holder.rating.text = current.voteAverage!!.toString()
}
override fun getItemCount(): Int {
return movies.size
}
}
Создание верстки в layouts/list_item_movie.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/movies_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="72dp"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingRight="16dp"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:textColor="#000000" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:paddingRight="16dp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="35dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/rating_image"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@drawable/star"
android:tint="@color/colorAccent" />
<TextView
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="5.0" />
</LinearLayout>
</LinearLayout>
В следующем уроке мы создадим layout главной ленты для отображения списка фильмов
Предыдущий
Вызов методов API и получение ответа
Следующий
Отображение ответа на экране