編輯:關於Android編程
ExpandableListView可擴展列表一般可用來開發類似QQ聯系人的界面效果。簡單整理了一下ExpandableListView的使用,希望幫助到需要的親們,由於比較簡單就沒有添加文字描述,歡迎留言交流!!!
先看下效果圖:

/**
* Created by magic on 2016年10月5日.防QQ聯系人效果
*/
public class MainActivity extends Activity {
ExpandableListView expandableListView;
ExpandableListViewAdapter adapter;
List list_group = new ArrayList();
List list_child1 = new ArrayList();
List list_child2 = new ArrayList();
List list_child3 = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.exl);
list_child1.add(new UserBean("趙麗穎"));
list_child1.add(new UserBean("鄭爽"));
list_child1.add(new UserBean("楊冪"));
list_child2.add(new UserBean("夏帆"));
list_child2.add(new UserBean("劉亦菲"));
list_child2.add(new UserBean("何曼婷"));
list_child3.add(new UserBean("蒼老師"));
list_child3.add(new UserBean("劉詩詩 "));
list_child3.add(new UserBean("佟麗娅"));
list_group.add(new GroupBean("萌妹", "29/48", list_child1));
list_group.add(new GroupBean("清純", "42/60", list_child2));
list_group.add(new GroupBean("美女", "53/71", list_child3));
adapter = new ExpandableListViewAdapter(this, list_group);
expandableListView.setAdapter(adapter);
int groupCount = adapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
// 設置expandableListView展開子項
// expandableListView.expandGroup(i);
// 設置expandableListView不展開子項
// expandableListView.collapseGroup(i);
}
// group 的點擊事件
expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});
// child 的點擊事件
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this,adapter.getGroup(groupPosition).getUserBeans().get(childPosition).getName(),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
相關實體類:
GroupBean.java
/**
* Created by magic on 2016年10月5日.分組實體類
*/
public class GroupBean {
// 組名
String groupName;
// 組描述
String groupDesc;
// 用戶集合
List userBeans;
public GroupBean() {
super();
}
public GroupBean(String groupName, String groupDesc,
List userBeans) {
super();
this.groupName = groupName;
this.groupDesc = groupDesc;
this.userBeans = userBeans;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getGroupDesc() {
return groupDesc;
}
public void setGroupDesc(String groupDesc) {
this.groupDesc = groupDesc;
}
public List getUserBeans() {
return userBeans;
}
public void setUserBeans(List userBeans) {
this.userBeans = userBeans;
}
}
UserBean.java
/**
* Created by magic on 2016年10月5日.用戶實體類
*/
public class UserBean {
String name;
public UserBean(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* Created by magic on 2016年10月5日.可擴展列表適配器
*/
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
Context context;
List list_group;
public ExpandableListViewAdapter(Context context,List list_group) {
super();
this.context = context;
this.list_group = list_group;
}
/**
* 獲取指定組中的指定子元素數據。
*/
@Override
public UserBean getChild(int groupPosition, int childPosition) {
return list_group.get(groupPosition).getUserBeans().get(childPosition);
}
/**
* 獲取指定組中的指定子元素ID
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/**
* 獲取一個視圖對象,顯示指定組中的指定子元素數據。
*
* @param groupPosition
* 組位置
* @param childPosition
* 子元素位置
* @param isLastChild
* 子元素是否處於組中的最後一個
* @param convertView
* 重用已有的視圖(View)對象
* @param parent
* 返回的視圖(View)對象始終依附於的視圖組
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean arg2, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.child_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tev_child);
textView.setText(getChild(groupPosition, childPosition).getName());
return convertView;
}
/**
* 獲取指定組中的子元素個數
*/
@Override
public int getChildrenCount(int groupPosition) {
return list_group.get(groupPosition).getUserBeans().size();
}
/**
* 獲取指定組中的數據
*/
@Override
public GroupBean getGroup(int groupPosition) {
return list_group.get(groupPosition);
}
/**
* 獲取組的個數
*/
@Override
public int getGroupCount() {
return list_group.size();
}
/**
* 獲取指定組的ID,這個組ID必須是唯一的
*/
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
/**
* 獲取顯示指定組的視圖對象
*
* @param groupPosition
* 組位置
* @param isExpanded
* 該組是展開狀態還是伸縮狀態
* @param convertView
* 重用已有的視圖對象
* @param parent
* 返回的視圖對象始終依附於的視圖組
*/
@Override
public View getGroupView(int groupPosition,boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.group_layout, null);
CheckBox chb_group=(CheckBox)convertView.findViewById(R.id.chb_group);
TextView tev_group_name = (TextView) convertView.findViewById(R.id.tev_group_name);
TextView tev_group_desc = (TextView) convertView.findViewById(R.id.tev_group_desc);
tev_group_name.setText(getGroup(groupPosition).getGroupName() + "");
tev_group_desc.setText(getGroup(groupPosition).getGroupDesc() + "");
chb_group.setChecked(isExpanded);
return convertView;
}
/**
* 組和子元素是否持有穩定的ID,也就是底層數據的改變不會影響到它們。
*/
@Override
public boolean hasStableIds() {
return false;
}
/**
* 是否選中指定位置上的子元素。
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
相關布局:
group_layout.xml
child_layout.xml
Android:自定義PopupMenu的樣式(顯示圖標/設置RadioButton圖標)
PopupMenu是Android中一個十分輕量級的組件。與PopupWindow相比,PopupMenu的可自定義的能力較小,但使用更加方便。 先上效果圖: 本
Android開發實踐 Service
Service是Android四大組件與Activity最相似的組件,都代表可執行的程序,區別在於Service一直在後台運行且沒有用戶界面。1.Service的類圖和生
Android Studio中查看類的繼承關系
查看類的繼承關系的快捷鍵F4,在Android Studio常用快捷鍵這篇文章中,有寫了。今天主要是講一些關於這個快捷鍵出來的界面的一些配置,這塊功能相對偏冷一些,可能
android開源庫發布到jcenter圖文詳解與填坑
相信很多人都用過開源項目,特別是android studio普及以後,使用開源庫更方便簡單。而如何上傳開源庫到jcenter供大家方便使用,雖然網上也有教程,但還是遇坑