1、分析二级菜单内容
从上面图可见 ,
一级菜单是一个String
二级菜单是imagerView 、String
我们从第二级写数据开始,String类型 int 类型
public class Chile { private String name; private int img; public String getName() { return name; } public int getImg() { return img; } public Chile(String name, int img) { this.name = name; this.img = img; } }
然后在写第一级 String类型 还有 集合(这个就是存放着多个子条目)
public class Group { private String group_str; private ArrayList<Chile> data; public Group(String group_str, ArrayList<Chile> data) { this.group_str = group_str; this.data = data; } public String getGroup_str() { return group_str; } public ArrayList<Chile> getData() { return data; } public Group() { super(); } }
2、适配所有
public class Expand_Adapter extends BaseExpandableListAdapter { private Context context; private ArrayList<Group> data; public Expand_Adapter(Context context, ArrayList<Group> data) { super(); this.context = context; this.data = data; } /** * @param groupPosition 父标题 * @param childPosition 子标题 * @return */ @Override public Object getChild(int groupPosition, int childPosition) { return data.get(groupPosition).getData().get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } /** * 获取子布局的视图 * * @param groupPosition * @param childPosition * @param isLastChild * @param convertView * @param parent * @return */ @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { Chile chile = data.get(groupPosition).getData().get(childPosition); View view = View.inflate(context, R.layout.layout_chile, null); TextView textView = view.findViewById(R.id.child); ImageView imageView = view.findViewById(R.id.child_img); textView.setText(chile.getName()); imageView.setBackgroundResource(chile.getImg()); return view; } @Override public int getChildrenCount(int groupPosition) { return data.get(groupPosition).getData().size(); } @Override public Object getGroup(int groupPosition) { return data.get(groupPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getGroupCount() { return data.size(); }
//一级标题布局的加载以及图标切换
@Overridepublic View getGroupView( int groupPosition , boolean isExpanded , View convertView , ViewGroup parent) { Group group = data.get(groupPosition) ; View view = View. inflate( context , R.layout. layout_group , null) ; TextView textView = view.findViewById(R.id. group) ; textView.setText(group.getGroup_str()) ; ImageView imageView = view.findViewById(R.id. group_image) ; if (isExpanded) { imageView.setBackgroundResource(R.drawable. down) ; } else { imageView.setBackgroundResource(R.drawable. up) ; } return view ; } @Override public boolean isChildSelectable( int groupPosition , int childPosition) { return true; } @Override public boolean hasStableIds() { return false; }}
3、创建、设置
private ExpandableListView expandableListView; private ArrayList<Group> data = new ArrayList<Group>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main5); expandableListView = (ExpandableListView) findViewById(R.id.expand); // 数据准备 initData(); expandableListView.setGroupIndicator(null); Log.e("ArrayList<Group> data",""+data.size()); // 设置设配器 Expand_Adapter adapter = new Expand_Adapter(getApplicationContext(), data); expandableListView.setAdapter(adapter); }
4、看看布局怎样的吧
1)、主布局
setContentView(R.layout.activity_main5);
<ExpandableListView android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@null" android:id="@+id/expand" > </ExpandableListView>2)、
layout_chile.xml
<ImageView android:layout_width="20dp" android:layout_height="20dp" android:id="@+id/child_img"/> <TextView android:id="@+id/child" android:textColor="#101010" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tttttttt"/>
3)、layout_group.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"android :layout_height= "90dp" > <TextView android :id= "@+id/group" android :layout_width= "wrap_content" android :layout_height= "30dp" android :layout_centerVertical= "true" android :layout_marginLeft= "20dp" android :text= " 标题栏 " android :textColor= "#99d70505" /> <ImageView android :id= "@+id/group_image" android :layout_width= "20dp" android :layout_height= "20dp" android :layout_alignParentEnd= "true" android :layout_centerVertical= "true" android :layout_gravity= "end" android :layout_marginRight= "20dp" android :background= "@drawable/up" /> </RelativeLayout>
当你复制完我的代码之后你会发现这个东西怎么高度不会变???
什么鬼???
慢慢研究,需要的留言。