欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android学习笔记之ExpandableListView 三级

程序员文章站 2022-06-03 12:55:32
...

在做三级列表的时候,用的是ExpandableListView里套一个ExpandableListView,由于里面的ExpandableListView把图标隐藏了……点又点不开,就以为是里面的ExpandableListView失效,弄了三小时,才想到把图标显示出来,,结果……已经打开了,郁闷得要死了,,,然后知道是因为没有位置的原因了……哎,,记下,别以后还杨白劳三个小时……

public class EGuideFourAdapter extends BaseExpandableListAdapter {
	private Context mContext;
	private List<NamedList> list;

	public EGuideFourAdapter(Context context, List<NamedList> list) {
		this.list = list;
		this.mContext = context;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return list.get(groupPosition).getChildList().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	static public TextView getTextView(Context context) {// 控制所有级的菜单,如果各级有另外设置,实现的是另外的设置。
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 90);
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setTextSize(17);
		textView.setTextColor(Color.BLACK);
		textView.setPadding(10, 5, 0, 5);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		return textView;
	}

	/**
	 * 三层树结构中的第二层是一个ExpandableListView
	 */
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (list.get(groupPosition).getChildList().get(childPosition)
				.getChildList().size() > 0) {// 该项是否有第三级
			// 是
			final ExpandableListView examGuidView = getExpandableListView();
			examGuidView.setGroupIndicator(null);
			examGuidView.setPadding(pixelsToDip(mContext,25), 0, 0, 0);
			final ExamGuideAdapter examGuideAdapter = new ExamGuideAdapter(
					this.mContext, list.get(groupPosition).getChildList());
			// list.get(groupPosition).getChildList()为第二级的list
			examGuidView.setAdapter(examGuideAdapter);
			// 点击第三级时,
			examGuidView.setOnChildClickListener(new OnChildClickListener() {

				@Override
				public boolean onChildClick(ExpandableListView parent, View v,
						int groupPositionTwo, int childPosition, long id) {

					// 获取第四级列表项
					List<NamedList> eGlist = list.get(groupPosition)
							.getChildList().get(groupPositionTwo)
							.getChildList().get(childPosition).getChildList();
					// 获取第三级标题
					String title = list.get(groupPosition).getChildList()
							.get(groupPositionTwo).getChildList()
							.get(childPosition).getName();
					if (eGlist.size() > 0) {// 第四级不是详细内容
						Intent intent = new Intent(mContext,
								UIExamGuideFour.class);
						intent.putExtra("list", (Serializable) eGlist);
						intent.putExtra("title", title);
						mContext.startActivity(intent);
					} else {
						Intent intent = new Intent(mContext,
								UIExamGuideContent.class);
						intent.putExtra("title", title);
						mContext.startActivity(intent);
					}
					return false;
				}
			});

			/**
			 * 重点:没有这个方法第三级会没有位置,所以会看不到,误以为ExpandableListView失效
			 * 关键点:第二级菜单展开时通过取得节点数来设置第三级菜单的大小
			 */
			examGuidView.setOnGroupExpandListener(new OnGroupExpandListener() {
				int i = 0;

				@Override
				public void onGroupExpand(int groupPosition) {
					i = (list.get(groupPosition).getChildList()
							.get(childPosition).getChildList().size() + 1) * 90 + 30;
					AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
							ViewGroup.LayoutParams.FILL_PARENT, i);
					examGuidView.setLayoutParams(lp);
				}
			});
			/**
			 * 第二级菜单回收时设置为标准Item大小
			 */
			examGuidView
					.setOnGroupCollapseListener(new OnGroupCollapseListener() {

						@Override
						public void onGroupCollapse(int groupPosition) {
							AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
									ViewGroup.LayoutParams.FILL_PARENT, 90);
							examGuidView.setLayoutParams(lp);
						}
					});
			return examGuidView;
		} else {
			TextView textView = getTextView(mContext);
			ExamGuide named = (ExamGuide) list.get(groupPosition)
					.getChildList().get(childPosition);
			textView.setTextColor(Color.BLACK);
			textView.setText(named.getName());
			// textView.setTag(temp[1]);
			// textView.setText(getChild(groupPosition,
			// childPosition).toString());
			textView.setPadding(pixelsToDip(mContext,55), 0, 0, 0);// 第一个参数原来为:
												// myPaddingLeft+PaddingLeft
												// ,是设置第三级菜单文字离左边的距离。
			return textView;
		}
	}

	public ExpandableListView getExpandableListView() {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 90);
		ExpandableListView superTreeView = new ExpandableListView(mContext);
		superTreeView.setLayoutParams(lp);
		return superTreeView;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView textView = getTextView(mContext);
		ExamGuide named = (ExamGuide) list.get(groupPosition);
		textView.setText(named.getName());
		textView.setPadding(pixelsToDip(mContext,40), 0, 0, 0);// 第一个参数原来为:myPaddingLeft+(PaddingLeft>>1)
											// ,是设置第二级菜单文字离左边的距离。
		textView.setTextColor(Color.BLACK);
		return textView;
	}
	//像素转dip解决分辨率问题
    private  int pixelsToDip(Context context,int Pixels) {
            int dip=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Pixels, context.getResources().getDisplayMetrics());
            return dip;
    }

	/**
	 * 第二级点击打开第三级
	 * 
	 * @param examGuidView
	 * @param groupPosition
	 */
	public void clickEvent(ExpandableListView examGuidView, int groupPosition) {
		examGuidView.expandGroup(groupPosition);
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return list.get(groupPosition).getChildList().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return list.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int p, int id) {
		// TODO Auto-generated method stub
		return true;
	}