Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Android
- 9498
- Android Support Library
- parser
- ReactiveX
- RXjava
- android weekly
- rxandroid
- 성공
- 구현
- Observable
- Connectable Observable Operators
- 분류
- android resource automation
- MVP
- logansquare
- DIABLO4
- 백준
- AndroidWeek
- Season3
- 안드로이드 리소스
- 안드로이드
- 리소스 자동화
- JSON
- retrolambda
- RX
- 디버그
- Kulle
- gradle
Archives
- Today
- Total
안드로이드 개발 팁 블로그
RecyclerView에 ItemClick,ItemLongClick 적용하기 본문
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Recylerview는 LayoutManager를 통해서 GridView, List뷰의 가로,세로 변환등 Layout의 커스터마이징을 손 쉽게 할 수 있는 뷰입니다.
나온지도 1년이 넘었고... 소스는 여기저기 있으니 소스 보관차원에서 쓴 포스트이니
Recylerview가 자세하게 궁금하신 분들은 따로 검색해 주기 바랍니다.
어쨌든 Recylerview에는 ItemTouchListener밖게 존재하지 않는다. 따라서 이전에 listview를 사용하던 사람들이 변경이 어려웠었다.
SimpleOnItemTouchListener을 상속 받은 별도의 리스너를 만들어 사용할 수 있다.
이하는 소스 코드.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class RecyclerViewOnItemClickListener extends RecyclerView.SimpleOnItemTouchListener { | |
private OnItemClickListener mListener; | |
private GestureDetector mGestureDetector; | |
public RecyclerViewOnItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { | |
this.mListener = listener; | |
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent e) { | |
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY()); | |
if(childView != null && mListener != null){ | |
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView)); | |
} | |
} | |
}); | |
} | |
@Override | |
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { | |
View child = rv.findChildViewUnder(e.getX(), e.getY()); | |
if (child != null && mListener != null && mGestureDetector.onTouchEvent(e)) { | |
mListener.onItemClick(child, rv.getChildAdapterPosition(child)); | |
return true; | |
} | |
return false; | |
} | |
public interface OnItemClickListener { | |
void onItemClick(View v, int position); | |
void onItemLongClick(View v, int position); | |
} | |
} | |
How to Use? | |
mRecyclerView.addOnItemTouchListener(new RecyclerViewOnItemClickListener(getActivity(), mRecyclerView, | |
new RecyclerViewOnItemClickListener.OnItemClickListener() { | |
@Override | |
public void onItemClick(View v, int position) { | |
Log.d(TAG, "click"); | |
} | |
@Override | |
public void onItemLongClick(View v, int position) { | |
Log.d(TAG, "long click"); | |
} | |
} | |
)); | |
'Android Tip' 카테고리의 다른 글
okhttp:3.0.2 -> okhttp3.1.2 업데이트 시 RealTrustRootIndex NullPointer에러 해결 법 (0) | 2016.02.11 |
---|---|
Retrofit2 + okhttp3 Cookie 관리하기 (1) | 2016.02.05 |
Retrofit2 + okhttp3 + Rxandroid 사용법 (5) | 2016.01.26 |
retrofit 1.9 -> 2.0, okhttp2 -> okhttp3 라이브러리 변경 삽질기. feat.Fresco (0) | 2016.01.15 |
Multiple APK 적용하기 (1) | 2016.01.14 |