메뉴 건너뛰기

?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
리스트뷰를 이용하여 구현하였고 리스트뷰의 한 항목을 눌렀을때 그 항목에만 해당되는 Context Menu를 넣을 수 있다.


1
2
3
4
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
    new MenuInflater(this).inflate(R.menu.context_mylibrary, menu);
}
1
2
3
4
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
    new MenuInflater(this).inflate(R.menu.context_mylibrary, menu);
}

어떤 도서관을 클릭해도 이와 같은 Context Menu가 나온다... 하지만 제대로 된 항목을 클릭한 것 일까?
아니면 실수나 오류로 인해서 위 또는 아래의 항목이 클릭된 것 일까?
이건 메뉴중 하나를 선택하지 않으면 정확히 알 수 없는 문제이다... 
이런 문제를 해결하기 위해서 Title을 넣어 보려고 한다.
1
2
3
4
5
6
7
8
9
10
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    ListView lv = (ListView) findViewById(R.id.LVLibrary);
    Library library = (Library) lv.getAdapter().getItem(info.position);
     
    new MenuInflater(this).inflate(R.menu.context_mylibrary, menu);
    menu.setHeaderTitle(library.getName());
}
info.position 을 통해 몇번째 항목인지 가져온다.
리스트뷰에서 Adapter를 얻어서 해당하는 항목의 객체를 얻어올 수 있다.
이제 타이틀로 넣을 값을 얻었으니 이제 설정만 해주면 끝이다.

표시될 메뉴 객체 menu의 setHeaderTitle 메소드를 호출하여 원하는 정보를 설정해 준다.


이게 결과를 실행한 화면이다.
타이틀이 나와서 그런지 어떤 항목을 선택했는지 확실히 알 수 있다.

List of Articles
번호 제목 날짜 조회 수
257 버튼 이벤트 추가하기 file 2021.03.31 191
256 안드로이드 스튜디오 - 필수 재정의 함수 자동 코드 추가 file 2021.03.29 194
255 버튼 이벤트 file 2021.03.31 205
254 안드로이드 - 컨텍스트 메뉴(Context Menu) 사용 예제 file 2021.04.01 207
253 안드로이드 액티비티 세로고정 2021.09.14 207
252 안드로이드 - 인텐트(Intent)를 활용한 액티비티(Activity) 생성 및 실행하기 file 2021.03.31 213
251 안드로이드 - 액티비티(Activity)와 액티비티 생명 주기(Activity Life Cycle) file 2021.04.01 225
250 안드로이드 - setContentView()와 레이아웃 전개자(LayoutInflater) 2021.04.01 227
249 안드로이드 가상머신 실행 속도 빠르게 하기 file 2021.03.31 228
248 버튼 생성, 이벤트 처리 file 2021.03.31 236
247 안드로이드 - 스타일 리소스(Style Resource) 사용하기 <style> file 2021.03.31 238
246 안드로이드 - 랠러티브 레이아웃(Relative Layout) file 2021.03.29 239
245 This Handler class should be static or leaks might occur 시 해결법 2020.12.14 240
244 Fragment를 통한 하단탭 예제1 file 2020.12.14 242
243 App 실행 file 2021.03.31 244
242 안드로이드 - SQLiteDatabase 구현하기 file 2021.04.01 249
241 Virtual Device , 디자인 화면 file 2021.03.31 260
240 setContentView()와 레이아웃 전개자(LayoutInflater) 2021.03.31 266
239 안드로이드 - AsyncTask 구현 예제 file 2021.04.01 280
238 안드로이드 - 익명 클래스(Anonymous Class) 사용법 file 2021.03.31 281
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved