我特别需要SecondLevel和ThirdLevel Expandable ListView的groupPositon和ChildPosition。我需要根据组位置以及子位置值添加OnClick事件,并参考LinkedHashmap或Array中给出的数据项
ExpandableListView expListView,expandableListView;
public static String[] parent = new String[]{"Rooms","Lighting","Accessories","Rugs","Gift Cards"};
String[] q1 = new String[]{"Living Room", "Dining Room","Bed Room","Kitchen","Outdoor Living"};
String[] des1 = new String[]{"Living Room Sets","Sofas & LoveSeats","Sectional Sofas","Sofa Sleeper","Pillows","Chairs","Coffee & End tables","Table Sets","Consoles & Desks","TV Consoles","Ottoman","Accent tables","Chaises","Pouf","Recliners","Serving Carts","Stools"};
setUpAdapter();
String[] des2=new String[]{};
//level 3
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq4 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq5 = new LinkedHashMap<>();
List<String[]> secondLevel = new ArrayList<>();
private void setUpAdapter() {
secondLevel.add(q1);
secondLevel.add(q1);
secondLevel.add(q1);
secondLevel.add(q1);
secondLevel.add(q1);
thirdLevelq1.put(q1[0], des1);
thirdLevelq1.put(q1[1], des1);
thirdLevelq1.put(q1[2], des1);
thirdLevelq1.put(q1[3], des1);
thirdLevelq1.put(q1[4], des1);
thirdLevelq2.put(q1[0], des2);
thirdLevelq2.put(q1[1], des2);
thirdLevelq2.put(q1[2], des2);
thirdLevelq2.put(q1[3], des2);
thirdLevelq2.put(q1[4], des2);
thirdLevelq3.put(q1[0], des2);
thirdLevelq3.put(q1[1], des2);
thirdLevelq3.put(q1[2], des2);
thirdLevelq3.put(q1[3], des2);
thirdLevelq3.put(q1[4], des2);
thirdLevelq4.put(q1[0], des2);
thirdLevelq4.put(q1[1], des2);
thirdLevelq4.put(q1[2], des2);
thirdLevelq4.put(q1[3], des2);
thirdLevelq4.put(q1[4], des2);
thirdLevelq5.put(q1[0], des2);
thirdLevelq5.put(q1[1], des2);
thirdLevelq5.put(q1[2], des2);
thirdLevelq5.put(q1[3], des2);
thirdLevelq5.put(q1[4], des2);
data.add(thirdLevelq1);
data.add(thirdLevelq2);
data.add(thirdLevelq3);
data.add(thirdLevelq4);
data.add(thirdLevelq5);
final ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);
expandableListView = (ExpandableListView) findViewById(R.id.expandible_listview);
//passing three level of information to constructor
expandableListView.setAdapter(threeLevelListAdapterAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupsPosition, int childsPosition, long id) {
//String data =listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
String[] value=secondLevel.get(0);
Toast.makeText(getApplicationContext(),""+value,Toast.LENGTH_LONG).show();
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
expandableListView.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
}
这是我的SecondLevelAdapter
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
List<String[]> data;
String[] headers;
ImageView ivGroupIndicator;
public SecondLevelAdapter(Context context, String[] headers, List<String[]> data) {
this.context = context;
this.data = data;
this.headers = headers;
}
@Override
public Object getGroup(int groupPosition) {
return headers[groupPosition];
}
@Override
public int getGroupCount() {
return headers.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.rowSecondText);
ImageView img_selection=(ImageView) convertView.findViewById(R.id.ivGroupIndicator);
String groupText = getGroup(groupPosition).toString();
text.setText(groupText);
int imageResourceId = isExpanded ? R.drawable.ic_keyboard_arrow_up_black_24dp
: R.drawable.ic_keyboard_arrow_down_black_24dp;
img_selection.setImageResource(imageResourceId);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String[] childData;
childData = data.get(groupPosition);
return childData[childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView textView = (TextView) convertView.findViewById(R.id.rowThirdText);
String[] childArray = data.get(groupPosition);
String text = childArray[childPosition];
textView.setText(text);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
String[] children = data.get(groupPosition);
return children.length;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的thirdLevelAdapter
public class ThreeLevelListAdapter extends BaseExpandableListAdapter {
String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
/**
* Constructor
* @param context
* @param parentHeader
* @param secondLevel
* @param data
*/
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
this.context = context;
this.parentHeaders = parentHeader;
this.secondLevel = secondLevel;
this.data = data;
}
@Override
public int getGroupCount() {
return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// no idea why this code is working
return 1;
}
@Override
public Object getGroup(int i) {
return null;
}
public Object getGroup(String[] groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int group, int child) {
return child;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String[] headers = secondLevel.get(groupPosition);
List<String[]> childData = new ArrayList<>();
HashMap<String, String[]> secondLevelData = data.get(groupPosition);
for (String key : secondLevelData.keySet()) {
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}