如何在可扩展列表视图中进一步扩展子项

时间:2017-10-21 08:00:02

标签: java android listview expandablelistview expandablelistadapter

我在我的应用程序中使用了可扩展的列表视图。它有4个父母。第一位父母有5个孩子,其余父母都有零孩子。我希望第一个父母的第三个孩子扩大更多,即生育更多的孩子。

我怎样才能做到这一点?

的strings.xml

<resources>

    <string-array name="exp_listview_header_titles">
        <item>BASIC FEATURES</item>  <!-- 1st parent -->
        <item>ABOUT US</item>
        <item>MORE APPLICATIONS</item>
        <item>RATE US</item>
    </string-array>

    <string-array name="basic_features">
        <item>Torch</item>
        <item>SoS</item>
        <item>Screen Color</item>   <!-- 3rd child -->
        <item>Notification Bar Widget</item>
        <item>Screen Widget</item>
    </string-array>

<!-- 3rd child further children -->
<string-array name="colors">
        <item>Red</item>
        <item>Green</item>
        <item>Blue</item>
    </string-array>

</resources>

主要Activity.Java

expandableListView = (ExpandableListView) findViewById(R.id.exp_listview);

    HashMap<String,List<String>> Parent_Child_HashMap = new HashMap<String,List<String>>();

    List<String> header_titles_list = new ArrayList<String>();

    List<String> action_items_list = new ArrayList<String>();

    List<String> empty_list = new ArrayList<String>();

    String header_titles[] = getResources().getStringArray(R.array.exp_listview_header_titles);

    String basic_features[] = getResources().getStringArray(R.array.basic_features);

    for(String title : header_titles)
    {
        header_titles_list.add(title);
    }

    for(String title : basic_features)
    {
        action_items_list.add(title);
    }

    Parent_Child_HashMap.put(header_titles_list.get(0),action_items_list);
    Parent_Child_HashMap.put(header_titles_list.get(1),empty_list);
    Parent_Child_HashMap.put(header_titles_list.get(2),empty_list);
    Parent_Child_HashMap.put(header_titles_list.get(3),empty_list);

    Exp_listview_adapter exp_listview_adapter = new Exp_listview_adapter(this,header_titles_list,Parent_Child_HashMap);
    expandableListView.setAdapter(exp_listview_adapter);
    expandableListView.expandGroup(0);

可扩展Listview Adapter.Java

public class Exp_listview_adapter extends BaseExpandableListAdapter {

    private List<String> header_titles;
    private HashMap<String,List<String>> child_titles; //for representing child item with heading
    private Context ctx;

    Exp_listview_adapter(Context ctx, List<String> header_titles, HashMap<String, List<String>> child_titles)
    {
        this.ctx = ctx;
        this.header_titles = header_titles;
        this.child_titles = child_titles;
    }

    @Override
    public int getGroupCount() {
        return header_titles.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child_titles.get(header_titles.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return header_titles.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child_titles.get(header_titles.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        String title = (String)this.getGroup(groupPosition);


        if(convertView == null){

            LayoutInflater layoutInflater =(LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.exp_listview_header,null);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        String title = (String) this.getChild(groupPosition, childPosition);

        if(convertView == null){

            LayoutInflater layoutInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.exp_listview_header_items, null);
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

我希望 Screen Color 孩子有更多孩子。我怎样才能做到这一点?我不知道该怎么做。任何帮助将不胜感激。

0 个答案:

没有答案