项目需要,要在ExpandableListView中根据父类条目的位置对子类条目左右显示位置进行调整。
经过试验,最总效果如下:
经过最近开发总结得出,RelativeLayout的可用性要远远超过LinearLayout。
LinearLayout相对来说是比较简单的,入门快,能够快速搭建简单的效果图,但是遇到复杂UI时,使用LinearLayout往往是得不到很好的解决方案的,而这时候RelativeLayout就显得非常的灵活,迎刃而解。建议对RelativeLayout深入了解。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background" android:orientation="vertical" > <ExpandableListView android:id="@+id/ExpandableListView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:childDivider="@drawable/list_separator" android:clickable="true" android:dividerHeight="2.0dip" android:fadeScrollbars="true" android:fadingEdge="none" android:groupIndicator="@null" android:paddingLeft="4.0dip" android:paddingRight="4.0dip" android:cacheColorHint="#00000000" android:scrollbarAlwaysDrawHorizontalTrack="true" android:scrollbarStyle="outsideOverlay" > </ExpandableListView> <!-- android:cacheColorHint="#00000000" 取消上下滑动时会出现黑色底色的情况 android:listSelector属性,默认会显示选中的item为橙黄底色--> </LinearLayout>TreeViewAdapter.java
package com.yang.adapter; import android.content.Context; import android.graphics.Color; import android.text.TextPaint; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.RelativeLayout; import android.widget.TextView; import com.yang.R; public class TreeViewAdapter extends BaseExpandableListAdapter { private String[] groups = { "草帽海贼团", "四 皇", "王下七武海", "海军本部" }; private String[][] children = { { "蒙其·D·路飞 ", "罗罗诺亚·索隆 ", "娜美 ", "乌索普 ", "山治", "乔巴", "弗兰奇", "布鲁克", "妮可·罗宾" }, { "红发海贼团", "白胡子海贼团", "凯多", "夏洛特·玲玲" }, { "乔拉可尔·密佛格", "巴索罗米·熊", "唐吉诃德·杜夫拉明高", "波雅·汉库克", "沙·克洛克达尔", "海侠·吉贝尔", "月光·莫利亚" }, { "元帅", "大将", "中将", "准将" } }; private LayoutInflater inflater; private LayoutInflater inflater1; public TreeViewAdapter(Context c) { this.inflater = LayoutInflater.from(c); this.inflater1 = LayoutInflater.from(c); } @Override public Object getChild(int groupPosition, int childPosition) { return childPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } class ViewHolder { TextView TextView001; TextView TextView002; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = inflater1.inflate(R.layout.child, null); holder = new ViewHolder(); holder.TextView001 = (TextView) convertView .findViewById(R.id.TextView001); holder.TextView002 = (TextView) convertView .findViewById(R.id.TextView002); //设置为粗体 TextPaint tp = holder.TextView002.getPaint(); tp.setFakeBoldText(true); holder.TextView002.setTextColor(Color.RED); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.TextView001.setText(children[groupPosition][childPosition]); if (groupPosition % 2== 0 ) { //关键设置代码 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.RIGHT_OF, holder.TextView001.getId()); holder.TextView002.setLayoutParams(params); holder.TextView002.setText("牛"); } else { //关键设置代码 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); holder.TextView002.setLayoutParams(params); holder.TextView002.setText("厉害"); } return convertView; } @Override public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return "dd"; } @Override public int getGroupCount() { return groups.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textview = null; if (convertView == null) { convertView = inflater.inflate(R.layout.parent, null); textview = (TextView) convertView.findViewById(R.id.TextView01); convertView.setTag(textview); } else { textview = (TextView) convertView.getTag(); } textview.setText(groups[groupPosition]); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } }