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 | 31 |
Tags
- Cocoapods #PrivateRepo #SpecRepo
- 스쿠버다이빙
- 스페인여행
- Concurrency #Swift #Combine
- 그라나다
- 시밀란
- 도심공항
- 아시아나
- cocoapod
- 대한항공
- 강릉
- Swift #Concurrency #쓰레드
- 지팍스페인
- Gradle
- 리브어보드
- 공기먹는다이버스
- 세비야
- 러브스플리트
- Device 등록
- 스페인광장
- 러브자그레브
- 라이브러리
- swiftUI
- SwiftUI #Skeleton #데이터갱신
- 스플리트
- 크로아티아
- 괌 자유여행
- 연금저축펀드
- 푸켓여행
- xcode
Archives
- Today
- Total
JEP's Diary
Recycler + Switch Button 본문
Recycler + Switch Button
1. 라이브러리 추가
Project Structure > app > Dependencies > + > Library Dependency > recyclerview 추가
2. 레이아웃 구성
activity_recycler_switch.xml
1 2 3 4 5 6 7 8 9 10 11 12 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.pje.android.recyclerviewsample.SwitchRecyclerActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> | cs |
row_switch.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/row_textView_padding" android:text="test"/> <android.support.v7.widget.SwitchCompat android:id="@+id/switchOnOff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_alignParentRight="true"/> </RelativeLayout> | cs |
3. Adapter 생성
SwitchRecyclerAdapter.java
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | public class SwitchRecyclerAdapter extends RecyclerView.Adapter<SwitchRecyclerAdapter.ViewHolder> { private Context mContext = null; private List<SwitchItemVO> mDataList = null; private OnItemClickedListener mItemClickListener = null; private OnCheckedChangeListener onItemCheckedListener; public interface OnItemClickedListener { void onItemClicked(SwitchItemVO vo, int position); } public interface OnCheckedChangeListener { void onCheckedChanged(int position , boolean isChecked); } public SwitchRecyclerAdapter(Context context, List<SwitchItemVO> items) { mContext = context; mDataList = items; } public void setOnClickListener(OnItemClickedListener listener) { mItemClickListener = listener; } public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { onItemCheckedListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_switch, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { SwitchItemVO item = mDataList.get(position); holder.title.setText(item.getTitle()); if (item.isSwitch()) { holder.switchBtn.setVisibility(View.VISIBLE); } else { holder.switchBtn.setVisibility(View.GONE); } holder.switchBtn.setTag(position); holder.switchBtn.setOnCheckedChangeListener(mOnCheckedChangeListener); holder.itemView.setTag(position); holder.itemView.setOnClickListener(mOnClickListener); } @Override public int getItemCount() { return mDataList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { TextView title; SwitchCompat switchBtn; public ViewHolder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.textTitle); switchBtn = (SwitchCompat) itemView.findViewById(R.id.switchOnOff); } } /** * 클릭 리스너 */ private View.OnClickListener mOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { int position = (int) v.getTag(); if (mItemClickListener != null) { mItemClickListener.onItemClicked(mDataList.get(position), position); } } }; /** * 스위치 리스너 */ private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int position = (int) buttonView.getTag(); if (onItemCheckedListener != null) { onItemCheckedListener.onCheckedChanged(position, isChecked); } } }; } | cs |
4. VO
SwitchItemVO.java
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 | public class SwitchItemVO { private String title; private boolean isSwitch; public SwitchItemVO(String title, boolean isSwitch) { this.title = title; this.isSwitch = isSwitch; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public boolean isSwitch() { return isSwitch; } public void setIsSwitch(boolean isSwitch) { this.isSwitch = isSwitch; } } | cs |
5. Activity
SwitchRecyclerActivity.java
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public class SwitchRecyclerActivity extends Activity { private RecyclerView recyclerView = null; private LinearLayoutManager layoutMgr = null; private List<SwitchItemVO> mDataList = null; private SwitchRecyclerAdapter mAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_switch); init(); initLayout(); } private void init() { mDataList = new ArrayList<>(); mDataList.add(new SwitchItemVO("Test1", true)); mDataList.add(new SwitchItemVO("Test2", false)); mDataList.add(new SwitchItemVO("Test3", true)); mDataList.add(new SwitchItemVO("Test4", false)); mDataList.add(new SwitchItemVO("Test5", true)); } private void initLayout() { recyclerView = (RecyclerView) findViewById(R.id.recycler); layoutMgr = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutMgr); mAdapter = new SwitchRecyclerAdapter(this, mDataList); recyclerView.setAdapter(mAdapter); mAdapter.setOnCheckedChangeListener(mOnCheckedChangeListener); mAdapter.setOnClickListener(mOnClickedListener); } private SwitchRecyclerAdapter.OnCheckedChangeListener mOnCheckedChangeListener = new SwitchRecyclerAdapter.OnCheckedChangeListener() { @Override public void onCheckedChanged(int position, boolean isChecked) { Log.e("", ">>> position : " + position + ", isChecked : " + isChecked); } }; private SwitchRecyclerAdapter.OnItemClickedListener mOnClickedListener = new SwitchRecyclerAdapter.OnItemClickedListener() { @Override public void onItemClicked(SwitchItemVO vo, int position) { Log.e("", ">>> vo : " + vo.getTitle() + ", position : " + position); } }; } | cs |
'Development > Android' 카테고리의 다른 글
Android Unit Tests(단위 테스트) - Local Unit Tests (1) | 2016.03.15 |
---|---|
Android 6.0 마시멜로 대응(런타임 권한 모델) (0) | 2016.03.14 |
Realm 모바일 데이터 베이스(Android) (0) | 2016.03.13 |
안드로이드 샘플 불러오기 (0) | 2015.11.25 |
RecyclerView (1) | 2015.11.19 |