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即可【全类名】