年后第一篇笔记,还是听听过年歌吧
效果
用法
1.首先要准备好要填充的一级列表的的数据和对应的二级列表数据
demo一级列表一共是三个选项
所以会有三个子选项的选项集合
代码语言:javascript复制ParentList = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.parent_item)){
ParentList.add((String)ss);
}
ArrayList<String> childLists1 = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.child_1)){
childLists1.add((String)ss);
}
ArrayList<String> childLists2 = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.child_2)){
childLists2.add((String)ss);
}
ArrayList<String> childLists3 = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.child_3)){
childLists3.add((String)ss);
}
ChildLists = new ArrayList<>();
ChildLists.add(childLists1);
ChildLists.add(childLists2);
ChildLists.add(childLists3);
2.把准备好的一级二级数据传入adapter
代码语言:javascript复制...
mExpandableListView = dialogView.findViewById(R.id.expandablelistview);
final MultistageAdapter moAdapter = new MultistageAdapter(this,ParentList, ChildLists, mParentmValue, mChildValue);
mExpandableListView.setAdapter(moAdapter);
...
3.监听点击事件
代码语言:javascript复制//一级选项点击回调
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
mParentmValue2 = groupPosition;
mChildValue2 = 0;
moAdapter.notifyDataSetChanged(mParentmValue2,mChildValue2); //更新选中状态
return false;
}
});
//二级选项点击回调
mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int reverseTypePosition, int reverseCisPosition, long id) {
mParentmValue2 = reverseTypePosition;
mChildValue2 = reverseCisPosition;
moAdapter.notifyDataSetChanged(mParentmValue2,mChildValue2);//更新选中状态
return false;
}
});
4.确认框中处理
代码语言:javascript复制private void showConfirmDialog(){
final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
final String value = ParentList.get(mParentmValue2) "/" ChildLists.get(mParentmValue2).get(mChildValue);
normalDialog.setTitle(value);
normalDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mParentmValue = mParentmValue2;
mChildValue = mChildValue2;
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
}
});
...
normalDialog.show();
}