Default Navigation Drawer View to ExpandableListView

后端 未结 2 922
滥情空心
滥情空心 2020-12-03 05:59

In Android Studio 2.1.2, if i create a default navigation activity I get this view:

Which uses the following activity_main.xml file:

         


        
2条回答
  •  被撕碎了的回忆
    2020-12-03 06:30

    This is the code for expandable list adapter:

    public class ExpandListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap> _listDataChild;
    
    public ExpandListAdapter(Context context, List listDataHeader,
                             HashMap> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }
    
    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
    
        final String childText = (String) getChild(groupPosition, childPosition);
    
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }
    
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
    
        txtListChild.setText(childText);
        return convertView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }
    
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
    
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
    
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }
    
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
    
        return convertView;
    }
    
    @Override
    public boolean hasStableIds() {
        return false;
    }
    
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }}
    

    In your activity:

         private void enableExpandableList() {
          listDataHeader = new ArrayList();
          listDataChild = new HashMap>();
        expListView = (ExpandableListView) findViewById(R.id.left_drawer);
    
        prepareListData(listDataHeader, listDataChild);
        listAdapter = new ExpandListAdapter(this, listDataHeader, listDataChild);
        // setting list adapter
        expListView.setAdapter(listAdapter);
    
        expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });
    
        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
    
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();
    
            }
        });
    
        // Listview on child click listener
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                // Temporary code:
    
                // till here
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });}
    

    Method to create List with data:

       private void prepareListData(List listDataHeader, Map> listDataChild) {
    
    
        // Adding child data
        listDataHeader.add("Product1");
        listDataHeader.add("product2");
        listDataHeader.add("Product3");
    
        // Adding child data
        List top = new ArrayList();
        top.add("x1");
        top.add("x2");
        top.add("x3");
        top.add("x4");
        top.add("x5");
    
    
        List mid = new ArrayList();
        mid.add("y1");
        mid.add("y2");
        mid.add("y3");
    
        List bottom = new ArrayList();
        bottom.add("z1");
        bottom.add("z2");
        bottom.add("z3");
    
    
    
        listDataChild.put(listDataHeader.get(0), top); // Header, Child data
        listDataChild.put(listDataHeader.get(1), mid);
        listDataChild.put(listDataHeader.get(2), bottom);
    }
    

    this code in layout xml

    
        
    
    
    
    
        
    
        
    
    
    
    

    xml for ExplistHeader list_group.xml

        
            
    
                
            
    

    xml for Explistchild list_item.xml

        
            
    
            
            
    

提交回复
热议问题