public
class
user_1
extends
Fragment {
private
final
LinkedHashMap<String, ParentModel> parentItem
=
new
LinkedHashMap<String, ParentModel>();
private
ArrayList<ParentModel> itemList =
new
ArrayList<ParentModel>();
private
HelperAdapter helperAdapter;
private
ExpandableListView expandableListView;
ViewGroup viewGroup;
@Nullable
@Override
public
View onCreateView(
@NonNull
LayoutInflater inflater,
@Nullable
ViewGroup container,
@Nullable
Bundle savedInstanceState) {
viewGroup = (ViewGroup) inflater.inflate(R.layout.user_1, container,
false
);
expandableListView = viewGroup.findViewById(R.id.expandableListView);
addData();
helperAdapter =
new
HelperAdapter(getContext(), itemList);
expandableListView.setAdapter(helperAdapter);
expandAllGroup();
expandableListView.setOnChildClickListener(
new
ExpandableListView.OnChildClickListener() {
@Override
public
boolean
onChildClick(ExpandableListView parent, View view,
int
parentPosition,
int
childPosition,
long
l) {
ParentModel parentInfo = itemList.get(parentPosition);
ChildModel childInfo = parentInfo.getItemList().get(childPosition);
if
(parentPosition ==
0
){
switch
(childPosition){
case
0
: Intent intent =
new
Intent(getContext(), apple.
class
);
startActivity(intent);
break
;
case
1
: Intent intent1 =
new
Intent(getContext(), banana.
class
);
startActivity(intent1);
break
;
}
}
else
if
(parentPosition ==
1
){
switch
(childPosition){
case
0
: Intent intent =
new
Intent(getContext(), test_page.
class
);
startActivity(intent);
break
;
case
1
: Intent intent1 =
new
Intent(getContext(), test_page2.
class
);
startActivity(intent1);
break
;
}
}
return
false
;
}
});
<span style=
"color:#cc7832"
>
return
</span><span style=
"color:#9876aa"
>viewGroup</span><span style=
"color:#cc7832"
>;
</span>}
private
void
expandAllGroup() {
int
count = helperAdapter.getGroupCount();
for
(
int
parentPosition =
0
; parentPosition<count; parentPosition ++){
expandableListView.expandGroup(parentPosition);
}
}
private
void
addData() {
addItem(
"가나 1"
,
"SubItem.1"
);
addItem(
"가나 1"
,
"SubItem.1"
);
addItem(
"가나 2"
,
"SubItem.1"
);
addItem(
"가나 2"
,
"SubItem.1"
);
}
private
int
addItem(String parentItemList, String subItem){
int
parentPosition =
0
;
ParentModel parentInfo = parentItem.get(parentItemList);
if
(parentInfo ==
null
){
parentInfo =
new
ParentModel();
parentInfo.setName(parentItemList);
parentItem.put(parentItemList, parentInfo);
itemList.add(parentInfo);
}
ArrayList<ChildModel> childItemList = parentInfo.getItemList();
int
listSize = childItemList.size();
listSize++;
ChildModel childInfo =
new
ChildModel();
childInfo.setName(subItem);
childItemList.add(childInfo);
parentInfo.setItemList(childItemList);
parentPosition = childItemList.indexOf(parentInfo);
return
parentPosition;
}}