투케이2K

393. (android/java) ExpandableListView 확장 리스트 뷰 사용해 부모, 자식 리스트 추가 및 클릭 이벤트 정의 실시 본문

Android

393. (android/java) ExpandableListView 확장 리스트 뷰 사용해 부모, 자식 리스트 추가 및 클릭 이벤트 정의 실시

투케이2K 2022. 11. 4. 19:23
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[Java : 소스 코드]

	// TODO [테스트 메소드 정의 실시]
	public void testMain(){
		Log.i("---","---");
		Log.d("//===========//","================================================");
		Log.i("","\n"+"[Test_Java > testMain() 메소드 : 테스트 함수 동작 실시]");
		Log.d("//===========//","================================================");
		Log.i("---","---");


		try {

			// [부모 리스트]
			ArrayList<HashMap<String, String>> groupData = new ArrayList<>();


			// [자식 리스트]
			ArrayList<ArrayList<HashMap<String, String>>> childData = new ArrayList<>();


			// [부모 리스트에 요소를 추가한다]
			HashMap<String, String> groupA = new HashMap<>();
			groupA.put("group", "투케이");

			HashMap<String, String> groupB = new HashMap<>();
			groupB.put("group", "2K");

			groupData.add(groupA);
			groupData.add(groupB);


			// [자식 리스트에 요소를 추가한다 (1)]
			ArrayList<HashMap<String, String>> childListA = new ArrayList<>();

			HashMap<String, String> childAA = new HashMap<>();
			childAA.put("data", "안녕");
			childListA.add(childAA);

			HashMap<String, String> childAB = new HashMap<>();
			childAB.put("data", "좋아");
			childListA.add(childAB);

			childData.add(childListA);


			// [자식 리스트에 요소를 추가한다 (2)]
			ArrayList<HashMap<String, String>> childListB = new ArrayList<>();

			HashMap<String, String> childBA = new HashMap<>();
			childBA.put("data", "hello");
			childListB.add(childBA);

			HashMap<String, String> childBB = new HashMap<>();
			childBB.put("data", "good");
			childListB.add(childBB);

			childData.add(childListB);


			// [부모 리스트와 자식 리스트를 포함한 Adapter를 생성]
			SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
					this, groupData,
					android.R.layout.simple_expandable_list_item_1,
					new String[] {"group"}, new int[] { android.R.id.text1},

					childData, android.R.layout.simple_expandable_list_item_2,
					new String[] {"data"}, new int[] { android.R.id.text1 } );


			// [ExpandableListView 에 Adapter 설정]
			ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandableListView);
			listView.setAdapter(adapter);


			// [리스트 뷰 클릭 이벤트]
			listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
				@Override
				public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
					Log.i("---","---");
					Log.w("//===========//","================================================");
					Log.i("","\n"+"[Test_Java > onItemClick() 메소드 : 리스트 뷰 클릭 이벤트 확인]");
					Log.i("","\n"+"[groupPosition : "+String.valueOf(groupPosition)+"]");
					Log.i("","\n"+"[childPosition : "+String.valueOf(childPosition)+"]");
					Log.w("//===========//","================================================");
					Log.i("---","---");

					return false;
				}
			});
		}
		catch (Exception e){
			Log.i("---","---");
			Log.e("//===========//","================================================");
			Log.i("","\n"+"[Test_Java > testMain() 메소드 : 예외 상황 발생 확인]");
			Log.i("","\n"+"[error : "+String.valueOf(e.getMessage())+"]");
			Log.e("//===========//","================================================");
			Log.i("---","---");
		}
	}
 

[Xml : 소스 코드]

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
 

[결과 출력]

 

 

반응형
Comments