Default Navigation Drawer View to ExpandableListView

后端 未结 2 920
滥情空心
滥情空心 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:31

    Download source code from here(Navigation drawer with expandablelistview in android)

     import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    
    public class CountryAdapter extends BaseExpandableListAdapter {
        Context context;
        ArrayList al_country;
    
        public CountryAdapter(Context context, ArrayList al_country) {
            this.context = context;
            this.al_country = al_country;
        }
    
        @Override
        public int getGroupCount() {
            return al_country.size();
        }
    
        @Override
        public int getChildrenCount(int i) {
            return al_country.get(i).getAl_state().size();
        }
    
        @Override
        public Object getGroup(int i) {
            return al_country.get(i);
        }
    
        @Override
        public Object getChild(int i, int i1) {
            return al_country.get(i).getAl_state().get(i1);
        }
    
        @Override
        public long getGroupId(int i) {
            return i;
        }
    
        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }
    
        @Override
        public boolean hasStableIds() {
            return false;
        }
    
        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            if (view==null){
    
                LayoutInflater layoutInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.adapter_header, null);
            }
            TextView tv_state = (TextView)view.findViewById(R.id.tv_name);
            tv_state.setText(al_country.get(i).getStr_country());
            return view;
        }
    
        @Override
        public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view==null){
    
                LayoutInflater layoutInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.adapter_childview, null);
            }
    
            TextView tv_state = (TextView)view.findViewById(R.id.tv_name);
    
            tv_state.setText(al_country.get(i).getAl_state().get(i1).getStr_name());
            tv_state.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((MainActivity)context).fn_selectedPosition(i,i1);
                }
            });
            return view;
        }
    
        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }
    
    
    }
    

    Thanks!

提交回复
热议问题