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;
}
});
return viewGroup;
}
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;
}}
childmodel과 ParentModel 입니다. 각각 따로 class를 줬습니다
public class ChildModel {
private String name = "";
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}}
public class ParentModel {
private String name;
private ArrayList<ChildModel> itemList = new ArrayList<ChildModel>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<ChildModel> getItemList() {
return itemList;
}
public void setItemList(ArrayList<ChildModel> itemList) {
this.itemList = itemList;
}
}
Adapter입니다
public class HelperAdapter extends BaseExpandableListAdapter {
private final Context context;
private final ArrayList<ParentModel> childList;
public HelperAdapter(Context context, ArrayList<ParentModel> childList){
this.context = context;
this.childList = childList;
}
@Override
public int getGroupCount() {
return childList.size();
}
@Override
public int getChildrenCount(int parentPosition) {
ArrayList<ChildModel> itemList = childList.get(parentPosition).getItemList();
return itemList.size();
}
@Override
public Object getGroup(int parentPosition) {
return childList.get(parentPosition);
}
@Override
public Object getChild(int parentPosition, int childPosition) {
ArrayList<ChildModel> itemList = childList.
get(parentPosition).getItemList();
return itemList.get(childPosition);
}
@Override
public long getGroupId(int parentPosition) {
return parentPosition;
}
@Override
public long getChildId(int parentPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int parentPosition, boolean b, View view, ViewGroup viewGroup) {
ParentModel parentInfo = (ParentModel) getGroup(parentPosition);
if (view == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.parent_items, viewGroup,false);
}
TextView textView = view.findViewById(R.id.parent_txt);
textView.setText(parentInfo.getName().trim());
return view;
}
@Override
public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
ChildModel childInfo = (ChildModel) getChild(parentPosition, childPosition);
if (view == null){
LayoutInflater layoutInflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.child_items,viewGroup, false);
}
TextView childItem = view.findViewById(R.id.child_txt);
childItem.setText(childInfo.getName().trim());
return view;
}
@Override
public boolean isChildSelectable(int parentPosition, int childPosition) {
return true;
}
}
Bottomnavigation에서 1, 2 번 탭 중에 1번에 ExpandableListView을 준 상태입니다
Listview에서는 chilposition을 터치하는 거 까지 오류없이 잘 되는 상태입니다
근데 bottom 탭에서 2번을 터치하고 다시 1번으로 들어가면 ListView child목록이 그 개수 만큼 늘어나는 현상이 생깁니다.
예로 들자면
가나다
--- a
--- b
--- c
이렇게 차일드 뷰가 세개가 있다고 가정하고 bottom 탭 2번을 누르고 다시 1번으로 들어가면
가나다
--- a
--- b
--- c
--- a
--- b
--- c
이렇게 늘어난 상태입니다
근데 그게 탭 1번을 누른 횟수 만큼 늘어납니다
혹시 이 경우 bottom의 문제인가요? 아니면 ListView의 문제인가요?
다른 bottom 탭은 정상돌아갑니다(아직 나머지 탭 화면을 다 채워넣은건 아니지만 아직까지는 정상작동 하는걸로 보여집니다)
근데 listview를 넣은 탭만 이런 오류가 생겨서 질문드립니다