如何进入基于特定Expandable listView的活动childChildPosition和groupPosition(三级)Android

时间:2018-12-26 17:31:49

标签: android expandablelistview

如何从Expandable ListView项目中获取第三级项目,我需要基于listview项目进入特定的活动。帮帮我。

 public class MainActivity extends AppCompatActivity {

private ExpandableListView expandableListView;

String[] parent = new String[]{"Rooms"};
String[] q1 = new String[]{"Living Room", "Dining Room","Bed Room","Kitchen","Outdoor Living"};
String[] q2 = new String[]{"Linear Layout", "Relative Layout"};
String[] q3 = new String[]{"Recycle View"};
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"};


LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
 * Second level array list
 */
List<String[]> secondLevel = new ArrayList<>();
/**
 * Inner level data
 */
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpAdapter();
}

private void setUpAdapter() {
    secondLevel.add(q1);
    secondLevel.add(q2);
    secondLevel.add(q3);
    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);


    data.add(thirdLevelq1);
    data.add(thirdLevelq2);
    data.add(thirdLevelq3);
    expandableListView = (ExpandableListView) findViewById(R.id.expandible_listview);
    //passing three level of information to constructor
    ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);
    expandableListView.setAdapter(threeLevelListAdapterAdapter);
  expandableListView.setOnChildClickListener(new 
 ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {

  //here I got confused
 // I need to get a particular child item for the String data variable
        String info= thirdLevelq1.get(groupPosition).get(childPosition);
        if(info=="chairs")
        {
            Intent i1 = new Intent(getApplicationContext(), chairs.class);
            startActivity(i1);
        }
            return false;
        }
    });
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousGroup = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            if (groupPosition != previousGroup)
                expandableListView.collapseGroup(previousGroup);
            previousGroup = groupPosition;
        }
    });


}

}

下面是三级可扩展listView的第二级适配器

 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);
        String groupText = getGroup(groupPosition).toString();
        text.setText(groupText);

    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;
}

}

下面是三级可扩展listView的三级适配器

 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 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;
}

}

下面是三级可扩展listView的SecondLevelExpandableListView

 public class SecondLevelExpandableListView extends ExpandableListView {

public SecondLevelExpandableListView(Context context) {
    super(context);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

0 个答案:

没有答案