`
crazysumer
  • 浏览: 49752 次
社区版块
存档分类
最新评论

【2013.06.27】ExpandableListView等

 
阅读更多

1. 一个界面布局,list有一级跟二级目录,用到ExpandableListView。使用此ListView,需要定义一个界面布局,一个一级目录布局和一个二级目录布局。可使用SimpleExpandableListAdapter去适配。要更多功能,比如修改一级目录图标,可自定义adapter。

   要去掉ExpandableListView一级目录自带图标,只需设置ExpandableListView属性

 

android:groupIndicator="@null"

 

 

   (1)遇到问题:ExpandableListView一级目录自带的图标修改成自定义图标出错

  原因:SimpleExpandableListAdapter会将所有传进来的控件转换成TextView,导致ClassCastException。

这是SimpleExpandableListAdapter的内部实现方法

 

    private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
        int len = to.length;

        for (int i = 0; i < len; i++) {
            TextView v = (TextView)view.findViewById(to[i]);
            if (v != null) {
                v.setText((String)data.get(from[i]));
            }
        }
    }

 

这样修改bindView方法即可

public class MyExpandListAdapter extends BaseExpandableListAdapter {

	private ViewBinder mViewBinder;

	private List<? extends Map<String, ?>> mGroupData;
	private int mExpandedGroupLayout;
	private int mCollapsedGroupLayout;
	private String[] mGroupFrom;
	private int[] mGroupTo;

	private List<? extends List<? extends Map<String, ?>>> mChildData;
	private int mChildLayout;
	private int mLastChildLayout;
	private String[] mChildFrom;
	private int[] mChildTo;

	private LayoutInflater mInflater;

	public MyExpandListAdapter(Context context,
			List<? extends Map<String, ?>> groupData, int groupLayout,
			String[] groupFrom, int[] groupTo,
			List<? extends List<? extends Map<String, ?>>> childData,
			int childLayout, String[] childFrom, int[] childTo) {
		this(context, groupData, groupLayout, groupLayout, groupFrom, groupTo,
				childData, childLayout, childLayout, childFrom, childTo);
	}

	public MyExpandListAdapter(Context context,
			List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
			int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
			List<? extends List<? extends Map<String, ?>>> childData,
			int childLayout, String[] childFrom, int[] childTo) {
		this(context, groupData, expandedGroupLayout, collapsedGroupLayout,
				groupFrom, groupTo, childData, childLayout, childLayout,
				childFrom, childTo);
	}

	public MyExpandListAdapter(Context context,
			List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
			int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
			List<? extends List<? extends Map<String, ?>>> childData,
			int childLayout, int lastChildLayout, String[] childFrom,
			int[] childTo) {
		mGroupData = groupData;
		mExpandedGroupLayout = expandedGroupLayout;
		mCollapsedGroupLayout = collapsedGroupLayout;
		mGroupFrom = groupFrom;
		mGroupTo = groupTo;

		mChildData = childData;
		mChildLayout = childLayout;
		mLastChildLayout = lastChildLayout;
		mChildFrom = childFrom;
		mChildTo = childTo;

		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public Object getChild(int groupPosition, int childPosition) {
		return mChildData.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	/**
	 * Instantiates a new View for a child.
	 * 
	 * @param isLastChild
	 *            Whether the child is the last child within its group.
	 * @param parent
	 *            The eventual parent of this new View.
	 * @return A new child View
	 */
	public View newChildView(boolean isLastChild, ViewGroup parent) {
		return mInflater.inflate((isLastChild) ? mLastChildLayout
				: mChildLayout, parent, false);
	}

	public int getChildrenCount(int groupPosition) {
		return mChildData.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		return mGroupData.get(groupPosition);
	}

	public int getGroupCount() {
		return mGroupData.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/**
	 * Instantiates a new View for a group.
	 * 
	 * @param isExpanded
	 *            Whether the group is currently expanded.
	 * @param parent
	 *            The eventual parent of this new View.
	 * @return A new group View
	 */
	public View newGroupView(boolean isExpanded, ViewGroup parent) {
		return mInflater.inflate((isExpanded) ? mExpandedGroupLayout
				: mCollapsedGroupLayout, parent, false);
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}


	private void bindView(View view, Map<String, ?> data, String[] from,
			int[] to) {
		int len = to.length;

		boolean isBound = false;
		for (int i = 0; i < len; i++) {

			final View v = view.findViewById(to[i]);

			if (v != null) {
				final Object _data = data.get(from[i]);
				String text = _data == null ? "" : data.toString();
				if (text == null) {
					text = "";
				}

				if (mViewBinder != null) {// 如果Binder不为空,使用Binder进行处理
					isBound = mViewBinder.setViewValue(v, data.get(from[i]),
							text);
				}

				if (!isBound) {// 如果Binder跳过,使用原来的方法进行处理
					TextView _v = (TextView) v;
					_v.setText((String) data.get(from[i]));
				}
			}
		}
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View v;
		if (convertView == null) {
			v = newGroupView(isExpanded, parent);
		} else {
			v = convertView;
		}

		bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
		return v;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View v;
		if (convertView == null) {
			v = newChildView(isLastChild, parent);
		} else {
			v = convertView;
		}

		bindView(v, mChildData.get(groupPosition).get(childPosition),
				mChildFrom, mChildTo);
		return v;
	}

	public void setViewBinder(ViewBinder mViewBinder) {
		this.mViewBinder = mViewBinder;
	}

	/**
	 * 提供视图渲染的绑定器
	 * 
	 * @author Atomic
	 */
	public static interface ViewBinder {
		boolean setViewValue(View view, Object data, String textRepresentation);
	}

 代码摘自http://www.eoeandroid.com/code/2011/1202/220.html

 

 

 

修改完以后,可以在Activity代码中像SimpleExpandableListAdapter一样使用。在一级目录的布局中就可以新添加ImageView属性,使用自定义的图标了。

 

public class ProjectTableExActivity extends Activity {

	private ExpandableListView lv;
	// private SimpleExpandableListAdapter adapter;
	private MyExpandListAdapter adapter1;
	private List<List<Map<String, Object>>> child;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.project_table_expandlist);

		child = new ArrayList<List<Map<String, Object>>>();
		lv = (ExpandableListView) findViewById(R.id.projectTabExpandList);

		adapter1 = new MyExpandListAdapter(this,
				groupData(new String[] { "你好","我好","大家好" }),
				R.layout.project_table_item, new String[] { "img", "text" },
				new int[] { R.id.item_img2, R.id.item_txt3 }, child,
				R.layout.project_table_child, new String[] {}, new int[] {});

		adapter1.setViewBinder(new MyExpandListAdapter.ViewBinder() {

			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				switch (view.getId()) {
				case R.id.item_img2: {//要替换成的图标的Id
					if (view instanceof ImageView) {
						ImageView _view = (ImageView) view;
						if (data instanceof Integer) {
							_view.setImageResource((Integer) data);
						} else if (data instanceof Drawable) {
							_view.setImageDrawable((Drawable) data);
						} else {
							throw new IllegalArgumentException(
									"The Data is Not a Drawable Or Resource Id!");
						}
					}

					return true;
				}
				}

				return false;
			}
		});
	
		lv.setAdapter(adapter1);

	}

	private List<? extends Map<String, ?>> groupData(String[] str) {

		ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		// Field field;
		try {

			for (int i = 0; i < str.length; i++) {

				Resources res = getResources();
				int lable = res.getIdentifier("cv_lable" + (i + 1), "drawable",
						getPackageName());

				/*
				 * field = R.drawable.class.getField("R.drawable.cv_lable" + (i
				 * + 1)); int lable = field.getInt(new R.drawable());
				 */

				Map map = new HashMap<String, Object>();
				map.put("img", lable);
				map.put("text", str[i]);
				list.add(map);

				List<Map<String, Object>> child1 = new ArrayList<Map<String, Object>>();
				Map<String, Object> childdata = new HashMap<String, Object>();

				child1.add(childdata);
				child.add(child1);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

}

 

注意取得lable的方法

 

(2)遇到问题:二级目录打不开

原因:在一级目录中有按钮控件存在

给按钮控件添加属性

 android:focusable="false"

 

 

 

2.自定义EditText的背景图和边框

 

背景图可直接修改EditText的background

 

新建类继承自EditText,绘制边框

 

public class MyEditText extends EditText {
	public MyEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {

		Paint paint = new Paint();
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(2);
		if (this.isFocused() == true)
			paint.setColor(Color.parseColor("#D9DAD4"));
		else
			paint.setColor(Color.parseColor("#D9DAD4"));
		canvas.drawRoundRect(
				new RectF(this.getScrollX(),  this.getScrollY(), this
						.getWidth()  + this.getScrollX(), this.getHeight()
						+ this.getScrollY() ), 3, 3, paint);
		super.onDraw(canvas);
	}

}

 

代码源自网络

 

在xml中引用时直接使用这个自定义的EditText即可【全类名】

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    ExpandableListView打造带折叠动画效果的多条目展示框架

    使用ExpandableListView打造的可折叠的多条目布局的效果,每个Item里面的布局都可以自己定义,并且扩展了ExpandableListView的功能添加了折叠动画的效果。 项目使用AS2.3和gradle_3.3构建 Github下载地址: ...

    可展开列表ExpandableListView嵌套GirdView demo

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if ...

    ExpandableListView....

    expandableListView.setAdapter(new MyExpandableListAdapter(this, groupList)); ``` 以上就是关于`ExpandableListView`的核心知识点,它为Android应用提供了构建层级结构列表的强大工具。开发者可以根据实际需求...

    Node.js-ExpandableListView简单实现商品列表折叠

    expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // 处理...

    【Android】树形菜单、扩展下拉菜单android.widget.ExpandableListView

    在实际项目中,`ExpandableListView`常用于创建如设置菜单、文件系统目录等场景。通过自定义适配器,我们可以实现各种样式和动画效果,提高用户体验。同时,`ExpandableListView`还可以与其他组件结合,如搜索栏,...

    多级列表ExpandableListView使用实例代码

    expandableListView.setAdapter(adapter); ``` 6. **事件监听** 可以通过`setOnChildClickListener`和`setOnGroupClickListener`来监听父项和子项的点击事件,以便进行相应的操作。 7. **自定义行为** 为了...

    自定义ExpandableListView带CheckBox全选功能

    ExpandableListView中group和child自定义视图带CheckBox,实现child全选、部分选中功能,同时实现类似邮件群发的用户选择功能,选中的用户在EditText中显示,点击后移除. 更新: 1. ExpandableListView勾选后生成的...

    多级列表ExpandableListView(模仿QQ)

    expandableListView.setOnGroupClickListener(...); expandableListView.setOnChildClickListener(...); ``` **6. 展开和折叠操作** 你可以使用`expandGroup()`和`collapseGroup()`方法来控制父项的展开和折叠状态...

    Android 利用ExpandableListView实现城市列表

    expandableListView.setOnChildClickListener(...); ``` 8. **CityListDemo项目** 提供的`CityListDemo`可能包含了实现上述步骤的代码示例。解压这个文件,你可以查看项目的结构,包括`MainActivity.java`(实现...

    自定义ExpandableListView带CheckBox全选的功能

    ExpandableListView中group和child自定义视图带CheckBox,实现child全选、部分选中功能,同时实现类似邮件群发的用户选择功能,选中的用户在EditText中显示,点击后移除. 更新: 1. ExpandableListView勾选后生成的...

    ExpandableListView 展开列表控件

    expandableListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // 处理父项点击事件 ...

    Android 音乐播放器 秒表源码 日历源码Android应用源码 27个合集.zip

    Android 音乐播放器 秒表源码 日历源码Android应用源码 27个合集.zip Android 音乐播放器(晴天播放)源码.rar Android录音程序源码.rar ...自定义适配器的ExpandableListView.rar 进度条对话框Demo源码.zip

    ExpandableListView的用法

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // ...

    2013.01.03 (2)——— android开发实例之仿QQExpandableListView

    本文将深入探讨2013年01月03日发布的一个Android开发实例,该实例旨在模仿QQ应用中的ExpandableListView功能,并结合"源码"和"工具"这两个标签,我们将会看到如何通过代码来实现这一功能。 首先,...

    ExpandableListView.zip

    在本项目"ExpandableListView.zip"中,我们可能找到了实现这一功能的具体源码,这将帮助我们更深入地理解如何在实际应用中操作和定制`ExpandableListView`。 首先,`ExpandableListView`是`ListView`的一个扩展,...

    ExpandableListView的使用实例

    expandableListView.setOnGroupClickListener(...); expandableListView.setOnChildClickListener(...); ``` 其中,`MyExpandableListAdapter`需要实现`BaseExpandableListAdapter`,根据数据模型填充视图。 ...

    ExpandableListView使用详解

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { //...

    安卓Android源码——自定义适配器的ExpandableListView.zip

    这个压缩包文件"安卓Android源码——自定义适配器的ExpandableListView.zip"包含了一份关于如何在Android应用中自定义适配器以实现`ExpandableListView`功能的详细教程和示例代码。 首先,我们要理解`...

    ViewPager结合ExpandableListView实现页面滑动及二级菜单

    `ViewPager`和`ExpandableListView`是两个非常重要的组件,它们可以帮助我们构建出高效、流畅且功能丰富的界面。`ViewPager`用于实现页面间的滑动切换,而`ExpandableListView`则提供了一种展示层次结构数据的方式。...

    ExpandableListView的使用

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int ...

Global site tag (gtag.js) - Google Analytics