工作需要,这两天晚上弄的都是多级树形菜单。
通过按钮或者手势触发一个侧滑出的多级品类菜单,实现思路如下:
1.触发事件:button的onClick的监听时间、手势的左滑触发
2.弹出效果和控件选择:dialog的自定义类和自定义的style搭配使用,从而自定义dialog
3.多级树形菜单的实现
4.网络json数据请求后对数据的解析格式化以及对树形菜单节点的适配
==========================================================================================
树形菜单实现(大部分为网络博客学习) 树形菜单原文
==========================================================================================
1.Model——结点的model类
public class Element {
/** 文字内容 */
private String contentText;
/** 在tree中的层级 */
private int level;
/** 元素的id */
private int id;
/** 父元素的id */
private int parendId;
/** 是否有子元素 */
private boolean hasChildren;
/** item是否展开 */
private boolean isExpanded;
/** 表示该节点没有父元素,也就是level为0的节点 */
public static final int NO_PARENT = -1;
/** 表示该元素位于最顶层的层级 */
public static final int TOP_LEVEL = 1;
public Element(String contentText, int level, int id, int parendId,
boolean hasChildren, boolean isExpanded) {
super();
this.contentText = contentText;
this.level = level;
this.id = id;
this.parendId = parendId;
this.hasChildren = hasChildren;
this.isExpanded = isExpanded;
}
// getter & setter
}
public class TreeViewDialog extends Dialog {
Context context;
private ListView treeview;
/** 树中的元素集合 */
private ArrayList elements;
/** 数据源元素集合 */
private ArrayList elementsData;
public TreeViewDialog(Context context) {
super(context);
this.context = context;
}
public TreeViewDialog(Context context, int theme) {
super(context,theme);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.store_category_treeview_dialog_layout);
LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(context);
init();
treeview = (ListView) findViewById(R.id.store_category_treeview);
TreeViewAdapter treeViewAdapter = new TreeViewAdapter(elements, elementsData, inflater);
TreeViewItemClickListener treeViewItemClickListener = new TreeViewItemClickListener(treeViewAdapter);
treeview.setAdapter(treeViewAdapter);
treeview.setOnItemClickListener(treeViewItemClickListener);
}
private void init() {
elements = new ArrayList();
elementsData = new ArrayList();
Element coat = new Element("服饰鞋帽", Element.TOP_LEVEL, 1315, Element.NO_PARENT, true, false);
Element maleCoat = new Element("男装", Element.TOP_LEVEL + 1, 1342, 1315, true, false);
Element shirt = new Element("衬衫", Element.TOP_LEVEL + 2, 1348, 1342, false, false);
elements.add(coat);
elementsData.add(coat);
elementsData.add(maleCoat);
elementsData.add(shirt);
//添加节点 -- 节点名称,节点level,节点id,父节点id,是否有子节点,是否展开
}
}
public class TreeViewAdapter extends BaseAdapter {
/** 元素数据源 */
private ArrayList elementsData;
/** 树中元素 */
private ArrayList elements;
/** LayoutInflater */
private LayoutInflater inflater;
/** item的行首缩进基数 */
private int indentionBase;
public TreeViewAdapter(ArrayList elements, ArrayList elementsData, LayoutInflater inflater) {
this.elements = elements;
this.elementsData = elementsData;
this.inflater = inflater;
indentionBase = 15;
}
public ArrayList getElements() {
return elements;
}
public ArrayList getElementsData() {
return elementsData;
}
@Override
public int getCount() {
return elements.size();
}
@Override
public Object getItem(int position) {
return elements.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.store_catefory_treeview_item, null);
holder.disclosureImg = (ImageView) convertView.findViewById(R.id.disclosureImg);
holder.contentText = (TextView) convertView.findViewById(R.id.contentText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Element element = elements.get(position);
int level = element.getLevel();
holder.contentText.setPadding(
indentionBase * (level - 1),
holder.disclosureImg.getPaddingTop(),
holder.disclosureImg.getPaddingRight(),
holder.disclosureImg.getPaddingBottom());
holder.contentText.setText(element.getContentText());
if (element.isHasChildren() && !element.isExpanded()) {
holder.disclosureImg.setImageResource(R.drawable.com_lable_arrow_dropdown);
//这里要主动设置一下icon可见,因为convertView有可能是重用了"设置了不可见"的view,下同。
holder.disclosureImg.setVisibility(View.VISIBLE);
} else if (element.isHasChildren() && element.isExpanded()) {
holder.disclosureImg.setImageResource(R.drawable.com_lable_arrow_pickup);
holder.disclosureImg.setVisibility(View.VISIBLE);
} else if (!element.isHasChildren()) {
holder.disclosureImg.setImageResource(R.drawable.com_lable_arrow_dropdown);
holder.disclosureImg.setVisibility(View.INVISIBLE);
}
return convertView;
}
/**
* 优化Holder
* @author liushiyao
*
*/
static class ViewHolder{
ImageView disclosureImg;
TextView contentText;
}
}
public class TreeViewItemClickListener implements OnItemClickListener {
/** adapter */
private TreeViewAdapter treeViewAdapter;
public TreeViewItemClickListener(TreeViewAdapter treeViewAdapter) {
this.treeViewAdapter = treeViewAdapter;
}
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
//点击的item代表的元素
Element element = (Element) treeViewAdapter.getItem(position);
//树中的元素
ArrayList elements = treeViewAdapter.getElements();
//元素的数据源
ArrayList elementsData = treeViewAdapter.getElementsData();
//点击没有子项的item直接返回
if (!element.isHasChildren()) {
return;
}
if (element.isExpanded()) {
element.setExpanded(false);
//删除节点内部对应子节点数据,包括子节点的子节点...
ArrayList elementsToDel = new ArrayList();
for (int i = position + 1; i < elements.size(); i++) {
if (element.getLevel() >= elements.get(i).getLevel())
break;
elementsToDel.add(elements.get(i));
}
elements.removeAll(elementsToDel);
treeViewAdapter.notifyDataSetChanged();
} else {
element.setExpanded(true);
//从数据源中提取子节点数据添加进树,注意这里只是添加了下一级子节点,为了简化逻辑
int i = 1;//注意这里的计数器放在for外面才能保证计数有效
for (Element e : elementsData) {
if (e.getParendId() == element.getId()) {
e.setExpanded(false);
elements.add(position + i, e);
i ++;
}
}
treeViewAdapter.notifyDataSetChanged();
}
}
}
// 商品品类对话框声明对象 & 设置相应属性
categoryDialog = new TreeViewDialog(StoreActivity.this, R.style.TreeViewDialog);
Window win = categoryDialog.getWindow();
LayoutParams params = new LayoutParams();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
params.x = width;// 设置x坐标
params.y = 0;// 设置y坐标
win.setAttributes(params);
win.setWindowAnimations(R.style.dialog_animation_style);
// 商品品类对话框声明对象 & 设置相应属性 end
categoryDialog.show();