**CustomExpandableListAdapter입니다
package com.example.user_page;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<String> expandableListTitle;
private final HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context mContext, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
this.mContext = mContext;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public int getChildrenCount(int i) {
return this.expandableListDetail.get(this.expandableListTitle.get(i)).size();
}
@Override
public Object getGroup(int i) {
return this.expandableListTitle.get(i);
}
@Override
public Object getChild(int i, int i1) {
return this.expandableListDetail.get(this.expandableListTitle.get(i)).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String listTitle = (String) getGroup(i);
if (view == null){
LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_header,null);
}
TextView listTitleTextView = view.findViewById(R.id.header_title);
listTitleTextView.setText(listTitle);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
String expandedListText = (String) getChild(i,i1);
if (view == null){
LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_child,null);
}
TextView expandedListTextView = view.findViewById(R.id.child_title);
expandedListTextView.setText(expandedListText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}
**ExpandableListDataPump.java 입니다
package com.example.user_page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData(){
HashMap<String, List<String>> expandableListDetail = new HashMap<>();
List<String> cricket = new ArrayList<>();
cricket.add("a");
cricket.add("b");
cricket.add("c");
cricket.add("d");
cricket.add("f");
List<String> football = new ArrayList<>();
football.add("a");
football.add("b");
football.add("c");
football.add("d");
football.add("f");
List<String> basketball = new ArrayList<>();
basketball.add("a");
basketball.add("b");
basketball.add("c");
basketball.add("d");
basketball.add("f");
expandableListDetail.put("CRICKET", cricket);
expandableListDetail.put("FOOTBALL", football);
expandableListDetail.put("BASKETBALL", basketball);
return expandableListDetail;
}
}
**프래그먼트입니다
package com.example.user_menu;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.user_page.CustomExpandableListAdapter;
import com.example.user_page.ExpandableListDataPump;
import com.example.user_page.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class user_1 extends Fragment {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
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);
expandableListDetail = ExpandableListDataPump.getData();
expandableListTitle = new ArrayList<>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(getActivity(),expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
return viewGroup;
}
}
실행을 해보면 순서가 FOOTBALL / CRICKET/ BASKETBALL
순으로 순서가 이상하게나옵니다..
유튜브보면서 천천히 검토 하면서 봤는데도 순서가 제대로 안되서 문의드립니다...