0
我在敲一個Android應用程序,我決定使用可擴展列表視圖。我有我需要的一切,表工作正常,但是當我填充它,團體似乎是在不同的順序比他們應該,這裏有一個例子: ExpandableListView沒有以正確的順序出現
正如你所看到的,子值很好,只有父值以隨機順序出現。下面是完整的代碼,我有:
我的片段代碼:
public class BuildingsFragment extends Fragment {
public BuildingsFragment() {
// Required empty public constructor
}
public static BuildingsFragment newInstance(Context context) {
BuildingsFragment fragment = new BuildingsFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_buildings, container, false);
final ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListViewBuildings);
HashMap<String, List<String>> expandableListDetail = ExpandableListDataPump.getData();
List<String> expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
ExpandableListAdapter expandableListAdapter = new CustomExpandableListAdapter(getContext(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Fragment fragment = null;
Class fragmentClass = BuildingDetailsFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction trans = getFragmentManager().beginTransaction();
Bundle args = new Bundle();
args.putString("BUILDING_NAME", "test");
fragment.setArguments(args);
trans.replace(R.id.frameLayoutForFragments, fragment);
trans.addToBackStack(null);
trans.commit();
return false;
}
});
return view;
}
}
我的自定義適配器:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_buildings_row_layout, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_buildings_group_layout, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listGroup);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
我使用的ExpandableListDataPump:
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
List<String> abuildings = new ArrayList<String>();
abuildings.add("A-1");
abuildings.add("A-2");
abuildings.add("A-3");
abuildings.add("A-5");
abuildings.add("A-7");
expandableListDetail.put("A", abuildings);
List<String> bbuildings = new ArrayList<String>();
bbuildings.add("B-1");
bbuildings.add("B-2");
bbuildings.add("B-3");
bbuildings.add("B-5");
bbuildings.add("B-6");
expandableListDetail.put("B", bbuildings);
List<String> cbuildings = new ArrayList<String>();
cbuildings.add("C-1");
cbuildings.add("C-3");
cbuildings.add("C-4");
cbuildings.add("C-5");
cbuildings.add("C-6");
expandableListDetail.put("C", cbuildings);
List<String> dbuildings = new ArrayList<String>();
dbuildings.add("D-1");
dbuildings.add("D-2");
dbuildings.add("D-3");
dbuildings.add("D-5");
dbuildings.add("D-7");
expandableListDetail.put("D", dbuildings);
return expandableListDetail;
}
}
我認爲這是一切與ExpandableListView相關。我真的想離開HashMap,但如果沒有其他方法,我可以將它更改爲TreeMap並使用它的排序功能(作爲最後的手段)?感謝幫助。
P.S.雖然這樣做我ExpandableListView我下面這個教程(我不知道,如果該事項):Tutorial
是否需要更改我的代碼中的其他內容,或者我可以將所有「HashMap」替換爲「LinkedHashMap」? – TheTechGuy96
你可以用「LinkedHashMap」替換所有的「HashMap」。你也可以抽象一些,用Map來代替你的HashMap聲明。這樣,你只會改變實現類型,如果你將來改用TreeMap或其他的東西。另外,根據文檔,LinkedHashMap比TreeMap便宜(這就是你說的最後一招)。 – austinbruch