先上效果图:
@BindView (R.id.expandList )
ExpandableListView expandableListView ;
ExpandableListViewAdapter expandableListViewAdapter ;
public static int childPositionCurrent =-1 ;
public static int groupPositionCurrent =-1 ;
String[] arrayGroupList = new String[]{"陈奕迅" , "许嵩" , "陈粒" , "毛不易" };
String[][] arrayChildList = new String[][]{
{"富士山下" , "红玫瑰" , "孤独患者" , "不要说话" },
{"雅俗共赏" , "山水之间" , "内线" , "大千世界" },
{"小半" , "祝星" , "易燃易爆炸" , "奇妙能力歌" },
{"消愁" , "像我这样的人" , "牧马城市" , "借" }
};
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
expandableListViewAdapter = new ExpandableListViewAdapter(arrayGroupList , arrayChildList );
expandableListView .setAdapter(expandableListViewAdapter );
expandableListViewAdapter .setOnGroupExpandedListener(new ExpandableListViewAdapter.GroupClickListener() {
@Override
public void onGroupClickListener (int groupPosition) {
expandOnlyOne(groupPosition);
}
});
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick (ExpandableListView expandableListView, View view, int i, long l) {
return false;
}
});
expandableListView .setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick (ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
childPositionCurrent = childPosition;
groupPositionCurrent = groupPosition;
expandableListViewAdapter .notifyDataSetChanged();
return true;
}
});
}
Adapter里面的 主要代码:
@Override
public View getChildView (int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
ChildrenViewHolder childrenViewHolder;
if (view == null ) {
view = LayoutInflater.from (viewGroup.getContext()).inflate(R.layout.item_child_text , viewGroup, false );
childrenViewHolder = new ChildrenViewHolder();
childrenViewHolder.childrenText = view.findViewById(R.id.childrenText );
view.setTag(childrenViewHolder);
} else {
childrenViewHolder = (ChildrenViewHolder) view.getTag();
}
childrenViewHolder.childrenText .setText(arrayChildList [groupPosition][childPosition]);
if (ExpandableListViewActivity.childPositionCurrent == childPosition && ExpandableListViewActivity.groupPositionCurrent == groupPosition) {
childrenViewHolder.childrenText .setTextColor(Color.parseColor ("#f2927b" ));
} else {
childrenViewHolder.childrenText .setTextColor(Color.parseColor ("#000000" ));
}
return view;
}
源码地址:
https://download.csdn.net/download/weixin_42248493/10475966