작성
·
305
·
수정됨
0
질문은 아래에 있습니다....
public class DataFragment extends Fragment {
private Context context;
private ExpandableListView list_view;
List<String> listDataParent;
HashMap<String, List<String>> listDataChild;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.context = this.getActivity();
return inflater.inflate(R.layout.fragment_data_list, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
list_view = (ExpandableListView) view.findViewById(R.id.list_view);
createListData();
// Listview Group click listener
list_view.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO GroupClickListener work
return false;
}
});
// Listview Group expanded listener
list_view.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
// TODO GroupExpandListener work
}
});
// Listview Group collasped listener
list_view.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
// TODO GroupCollapseListener work
}
});
// Listview on child click listener
list_view.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText( context,
"wow, this is - "+listDataChild.get(listDataParent.get(groupPosition)).get(childPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void createListData() {
listDataParent = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataParent.add("Colors");
listDataParent.add("Fruits");
listDataParent.add("Animals");
// Adding child data List one
List<String> colors = new ArrayList<String>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Yellow");
colors.add("Blue");
listDataChild.put(listDataParent.get(0), colors); // Header, Child data
listDataChild.put(listDataParent.get(1), fruits); // Header, Child data
listDataChild.put(listDataParent.get(2), animals); // Header, Child data
ExpandableListAdapter listAdapter = new ExpandableListAdapter(context, listDataParent, listDataChild);
list_view.setAdapter(listAdapter);
}
}
참고 사이트 : https://www.elsebazaar.com/blog/how-to-create-an-expandable-list-view-in-android-studio/
잘 몰라서 질문드립니다.
코틀린 공부하다 잠깐 자바 코드가 필요해서요.
위에 자바코드중 colors.add("Red"); ..Green, Blue 등 이름으로 각각 디테일 액티비티로 인텐트하는 방법을 알고 싶습니다. 자바로요..
중간쯤 Red 클릭시 토스트 메세지는 잘 뜹니다....
그리고 시간 나시면 자바를 코틀린으로 변환은 되는데 안되는 부분이 있네요. 주의점이나 수동으로 바꾸어야할점을 한번 교육해 주심이....
바쁘시겠지만 질문 기다리겠습니다.