public
class
MainActivity
extends
AppCompatActivity {
private
List<Movie> movieList =
new
ArrayList<>();
private
RecyclerView recyclerView;
private
MoviesAdapter mAdapter;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter =
new
MoviesAdapter(movieList);
RecyclerView.LayoutManager mLayoutManager =
new
LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(
new
DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.addOnItemTouchListener(
new
RecyclerTouchListener(getApplicationContext(), recyclerView,
new
RecyclerTouchListener.ClickListener() {
@Override
public
void
onClick(View view,
int
position) {
Movie movie = movieList.get(position);
Toast.makeText(getApplicationContext(), movie.getTitle() +
" is selected!"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onLongClick(View view,
int
position) {
}
}));
prepareMovieData();
}
private
void
prepareMovieData() {
Movie movie =
new
Movie(
"Mad Max: Fury Road"
,
"Action & Adventure"
,
"2015"
);
movieList.add(movie);
movie =
new
Movie(
"Inside Out"
,
"Animation, Kids & Family"
,
"2015"
);
movieList.add(movie);
movie =
new
Movie(
"Star Wars: Episode VII - The Force Awakens"
,
"Action"
,
"2015"
);
movieList.add(movie);
movie =
new
Movie(
"Shaun the Sheep"
,
"Animation"
,
"2015"
);
movieList.add(movie);
movie =
new
Movie(
"The Martian"
,
"Science Fiction & Fantasy"
,
"2015"
);
movieList.add(movie);
movie =
new
Movie(
"Mission: Impossible Rogue Nation"
,
"Action"
,
"2015"
);
movieList.add(movie);
movie =
new
Movie(
"Up"
,
"Animation"
,
"2009"
);
movieList.add(movie);
movie =
new
Movie(
"Star Trek"
,
"Science Fiction"
,
"2009"
);
movieList.add(movie);
movie =
new
Movie(
"The LEGO Movie"
,
"Animation"
,
"2014"
);
movieList.add(movie);
movie =
new
Movie(
"Iron Man"
,
"Action & Adventure"
,
"2008"
);
movieList.add(movie);
movie =
new
Movie(
"Aliens"
,
"Science Fiction"
,
"1986"
);
movieList.add(movie);
movie =
new
Movie(
"Chicken Run"
,
"Animation"
,
"2000"
);
movieList.add(movie);
movie =
new
Movie(
"Back to the Future"
,
"Science Fiction"
,
"1985"
);
movieList.add(movie);
movie =
new
Movie(
"Raiders of the Lost Ark"
,
"Action & Adventure"
,
"1981"
);
movieList.add(movie);
movie =
new
Movie(
"Goldfinger"
,
"Action & Adventure"
,
"1965"
);
movieList.add(movie);
movie =
new
Movie(
"Guardians of the Galaxy"
,
"Science Fiction & Fantasy"
,
"2014"
);
movieList.add(movie);
mAdapter.notifyDataSetChanged();
}
}
Adding RecyclerView Item Click Listener
RecyclerView doesn’t have OnItemClickListener method too to identify item click. You need to write your own class extending RecyclerView.OnItemTouchListener.
Create a class named RecyclerTouchListener.java and extend it from RecyclerView.OnItemTouchListener. You can notice that ClickListener interface also added here.
public
class
RecyclerTouchListener
implements
RecyclerView.OnItemTouchListener {
private
GestureDetector gestureDetector;
private
ClickListener clickListener;
public
RecyclerTouchListener(Context context,
final
RecyclerView recyclerView,
final
ClickListener clickListener) {
this
.clickListener = clickListener;
gestureDetector =
new
GestureDetector(context,
new
GestureDetector.SimpleOnGestureListener() {
@Override
public
boolean
onSingleTapUp(MotionEvent e) {
return
true
;
}
@Override
public
void
onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if
(child !=
null
&& clickListener !=
null
) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public
boolean
onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if
(child !=
null
&& clickListener !=
null
&& gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return
false
;
}
@Override
public
void
onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public
void
onRequestDisallowInterceptTouchEvent(
boolean
disallowIntercept) {
}
public
interface
ClickListener {
void
onClick(View view,
int
position);
void
onLongClick(View view,
int
position);
}
}
Comments
Post a Comment