Simple RecyclerView in Android for show simple listing into Android App. If you are first time create simple listing screen than this example is best for you.
1. Step => Create simple Activity for example of simple RecyclerView or listing data. Create model class below.
4. ==> Create MainActivity file for recycler view show Movie list with title,
subtitle, and year.
1. Step => Create simple Activity for example of simple RecyclerView or listing data. Create model class below.
package com.akhari.myapplication; public class MovieModel { private String title, genre, year; public MovieModel(String title, String genre, String year) { this.title = title; this.genre = genre; this.year = year; } public String getTitle() { return title; } public void setTitle(String name) { this.title = name; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } }
2. Step==> Create Simple adapter for set adapter of recycler View.
With arrayList of MovieList Model class
package com.akhari.myapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; public class MoviesListAdapter extends
RecyclerView.Adapter<MoviesListAdapter.MyViewHolder> {
private ArrayList<MovieModel> modelArrayList;
private Context mContext;
public MoviesListAdapter(ArrayList<MovieModel> modelArrayList,
Context mContext) {
this.modelArrayList = modelArrayList;
this.mContext = mContext;
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_row, parent, false); return new MyViewHolder(itemView); } @Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { MovieModel movie = modelArrayList.get(position); holder.title.setText(movie.getTitle()); holder.genre.setText(movie.getGenre()); holder.year.setText(movie.getYear()); } @Override public int getItemCount() { return modelArrayList.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { public TextView title, year, genre; public MyViewHolder(@NonNull View itemView) { super(itemView); title = itemView.findViewById(R.id.title); genre = itemView.findViewById(R.id.genre); year = itemView.findViewById(R.id.year); } } }
3. ==> Create Simple XML layout file for recycler view row layout.
Name of layout file list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="@dimen/row_padding_vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/row_padding_vertical"> <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="@color/title"
android:textSize="16dp"
android:textStyle="bold" /> <TextView
android:id="@+id/genre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title" /> <TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@color/year" /> </RelativeLayout>
4. ==> Create MainActivity file for recycler view show Movie list with title,
subtitle, and year.
package com.akhari.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ArrayList<MovieModel> movieList = new ArrayList<>(); private RecyclerView recyclerView; private MoviesListAdapter mMoviesListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recycler_view); mMoviesListAdapter = new MoviesListAdapter(movieList,this); RecyclerView.LayoutManager mLayoutManager =
new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(mMoviesListAdapter); prepareMovieList(); } private void prepareMovieList() { MovieModel movie = new MovieModel("Fury Road",
"Action", "2018"); movieList.add(movie); movie = new MovieModel("Inside",
"Kids & Family", "2018"); movieList.add(movie); movie = new MovieModel("Star Wars",
"Action", "2019"); movieList.add(movie); movie = new MovieModel("Shaun the Sheep",
"Animation", "2019"); movieList.add(movie); movie = new MovieModel("Martian",
"Science Fiction", "2019"); movieList.add(movie); movie = new MovieModel("Mission:
Impossible Rogue Nation", "Action", "2019"); mMoviesListAdapter.notifyDataSetChanged(); } }
No comments:
Post a Comment