您好,登錄后才能下訂單哦!
在Android開發中,列表可以說是最常見的了,一般都是使用ListView,當涉及到二維數組時,更多的使用到ExpandableListView,然而當數據結構比較復雜時,就需要使用三級菜單或者更多級的菜單來顯示,這就讓人比較頭疼了,最近做的項目就涉及到了三級菜單,遇到了不少問題,雖然不夠完美,但是基本需求實現了,在此記錄一下。(之前見過有人使用ListView實現4級、5級甚至更多級菜單的,是在Adapter的數據源里定義的結構,根據等級縮進左間距的倍數,鏈接地址找不到了,有興趣的可以自己找找)
先上效果圖:
簡單介紹下重點,為了簡便,把第一層ExpandableListView稱之為EListOne,相應的Adapter稱之為AdpOne;第二層ExpandableListView稱之為EListTwo,相應的Adapter稱之為AdpTwo。
首先第一個要處理的問題是在AdpOne的getChildView方法中,需要對EListTwo的高度進行動態計算,因為EListTwo展開和關閉時高度是不一樣的,所以要在EListTwo的setOnGroupExpandListener和setOnGroupCollapseListener方法中做相應的處理:
/** * @author Apathy、恒 * * 子ExpandableListView展開時,因為group只有一項,所以子ExpandableListView的總高度= * (子ExpandableListView的child數量 + 1 )* 每一項的高度 * */ eListView.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (child.getChildNames().size() + 1)* (int) mContext.getResources().getDimension(R.dimen.parent_expandable_list_height)); eListView.setLayoutParams(lp); } }); /** * @author Apathy、恒 * * 子ExpandableListView關閉時,此時只剩下group這一項,所以子ExpandableListView的總高度即為一項的高度 * */ eListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { LayoutParams lp = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext .getResources().getDimension( R.dimen.parent_expandable_list_height)); eListView.setLayoutParams(lp); } });
只展示菜單肯定不是我們的最終需求,我們一般需要點擊菜單后進行相應的界面跳轉或者數據處理,所以就需要獲取所點擊的菜單精確下標,獲取方法很簡單,只需要定義一個接口,在AdpOne的getChildView方法中回調即可:
/** * @author Apathy、恒 * * 點擊子ExpandableListView子項時,調用回調接口 * */ eListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView arg0, View arg1, int groupIndex, int childIndex, long arg4) { if (mTreeViewClickListener != null) { mTreeViewClickListener.onClickPosition(groupPosition, childPosition, childIndex); } return false; } });
下面是完整的代碼:
MainActivity.java:
package com.heng.tree; import java.util.ArrayList; import com.heng.tree.R; import com.heng.tree.adapter.ParentAdapter; import com.heng.tree.adapter.ParentAdapter.OnChildTreeViewClickListener; import com.heng.tree.entity.ChildEntity; import com.heng.tree.entity.ParentEntity; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.Toast; /** * * @author Apathy、恒 * * <br/> * * @email shexiaoheng@163.com * * @blog * <a >http://blog.csdn.net/shexiaoheng</a > * * <br/> * <br/> * * @Detail 本Demo為ExpandableListView嵌套ExpandableListView實現三級菜單的例子 * * #ParentAdapter.OnChildTreeViewClickListener * * */ public class MainActivity extends Activity implements OnGroupExpandListener, OnChildTreeViewClickListener { private Context mContext; private ExpandableListView eList; private ArrayList<ParentEntity> parents; private ParentAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; setContentView(R.layout.activity_main); loadData(); initEList(); } /** * @author Apathy、恒 * * 初始化菜單數據源 * */ private void loadData() { parents = new ArrayList<ParentEntity>(); for (int i = 0; i < 10; i++) { ParentEntity parent = new ParentEntity(); parent.setGroupName("父類父分組第" + i + "項"); parent.setGroupColor(getResources().getColor( android.R.color.holo_red_light)); ArrayList<ChildEntity> childs = new ArrayList<ChildEntity>(); for (int j = 0; j < 8; j++) { ChildEntity child = new ChildEntity(); child.setGroupName("子類父分組第" + j + "項"); child.setGroupColor(Color.parseColor("#ff00ff")); ArrayList<String> childNames = new ArrayList<String>(); ArrayList<Integer> childColors = new ArrayList<Integer>(); for (int k = 0; k < 5; k++) { childNames.add("子類第" + k + "項"); childColors.add(Color.parseColor("#ff00ff")); } child.setChildNames(childNames); childs.add(child); } parent.setChilds(childs); parents.add(parent); } } /** * @author Apathy、恒 * * 初始化ExpandableListView * */ private void initEList() { eList = (ExpandableListView) findViewById(R.id.eList); eList.setOnGroupExpandListener(this); adapter = new ParentAdapter(mContext, parents); eList.setAdapter(adapter); adapter.setOnChildTreeViewClickListener(this); } /** * @author Apathy、恒 * * 點擊子ExpandableListView的子項時,回調本方法,根據下標獲取值來做相應的操作 * */ @Override public void onClickPosition(int parentPosition, int groupPosition, int childPosition) { // do something String childName = parents.get(parentPosition).getChilds() .get(groupPosition).getChildNames().get(childPosition) .toString(); Toast.makeText( mContext, "點擊的下標為: parentPosition=" + parentPosition + " groupPosition=" + groupPosition + " childPosition=" + childPosition + "\n點擊的是:" + childName, Toast.LENGTH_SHORT).show(); } /** * @author Apathy、恒 * * 展開一項,關閉其他項,保證每次只能展開一項 * */ @Override public void onGroupExpand(int groupPosition) { for (int i = 0; i < parents.size(); i++) { if (i != groupPosition) { eList.collapseGroup(i); } } } }
ParentAdapter.java
package com.heng.tree.adapter; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.AbsListView.LayoutParams; import android.widget.TextView; import com.heng.tree.R; import com.heng.tree.entity.ChildEntity; import com.heng.tree.entity.ParentEntity; /** * * @author Apathy、恒 * * 父類分組的適配器 * * <br/> * <br/> * * 方法 {@link #getChildView(int, int, boolean, View, ViewGroup)}<b><font * color='#ff00ff' size='2'>極其重要</font></b> * * */ public class ParentAdapter extends BaseExpandableListAdapter { private Context mContext;// 上下文 private ArrayList<ParentEntity> mParents;// 數據源 private OnChildTreeViewClickListener mTreeViewClickListener;// 點擊子ExpandableListView子項的監聽 public ParentAdapter(Context context, ArrayList<ParentEntity> parents) { this.mContext = context; this.mParents = parents; } @Override public ChildEntity getChild(int groupPosition, int childPosition) { return mParents.get(groupPosition).getChilds().get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return mParents.get(groupPosition).getChilds() != null ? mParents .get(groupPosition).getChilds().size() : 0; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isExpanded, View convertView, ViewGroup parent) { final ExpandableListView eListView = getExpandableListView(); ArrayList<ChildEntity> childs = new ArrayList<ChildEntity>(); final ChildEntity child = getChild(groupPosition, childPosition); childs.add(child); final ChildAdapter childAdapter = new ChildAdapter(this.mContext, childs); eListView.setAdapter(childAdapter); /** * @author Apathy、恒 * * 點擊子ExpandableListView子項時,調用回調接口 * */ eListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView arg0, View arg1, int groupIndex, int childIndex, long arg4) { if (mTreeViewClickListener != null) { mTreeViewClickListener.onClickPosition(groupPosition, childPosition, childIndex); } return false; } }); /** * @author Apathy、恒 * * 子ExpandableListView展開時,因為group只有一項,所以子ExpandableListView的總高度= * (子ExpandableListView的child數量 + 1 )* 每一項的高度 * */ eListView.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { LayoutParams lp = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, (child .getChildNames().size() + 1) * (int) mContext.getResources().getDimension( R.dimen.parent_expandable_list_height)); eListView.setLayoutParams(lp); } }); /** * @author Apathy、恒 * * 子ExpandableListView關閉時,此時只剩下group這一項, * 所以子ExpandableListView的總高度即為一項的高度 * */ eListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { LayoutParams lp = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext .getResources().getDimension( R.dimen.parent_expandable_list_height)); eListView.setLayoutParams(lp); } }); return eListView; } /** * @author Apathy、恒 * * 動態創建子ExpandableListView * */ public ExpandableListView getExpandableListView() { ExpandableListView mExpandableListView = new ExpandableListView( mContext); LayoutParams lp = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext .getResources().getDimension( R.dimen.parent_expandable_list_height)); mExpandableListView.setLayoutParams(lp); mExpandableListView.setDividerHeight(0);// 取消group項的分割線 mExpandableListView.setChildDivider(null);// 取消child項的分割線 mExpandableListView.setGroupIndicator(null);// 取消展開折疊的指示圖標 return mExpandableListView; } @Override public Object getGroup(int groupPosition) { return mParents.get(groupPosition); } @Override public int getGroupCount() { return mParents != null ? mParents.size() : 0; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.parent_group_item, null); holder = new GroupHolder(convertView); convertView.setTag(holder); } else { holder = (GroupHolder) convertView.getTag(); } holder.update(mParents.get(groupPosition)); return convertView; } /** * @author Apathy、恒 * * Holder優化 * */ class GroupHolder { private TextView parentGroupTV; public GroupHolder(View v) { parentGroupTV = (TextView) v.findViewById(R.id.parentGroupTV); } public void update(ParentEntity model) { parentGroupTV.setText(model.getGroupName()); parentGroupTV.setTextColor(model.getGroupColor()); } } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } /** * @author Apathy、恒 * * 設置點擊子ExpandableListView子項的監聽 * */ public void setOnChildTreeViewClickListener( OnChildTreeViewClickListener treeViewClickListener) { this.mTreeViewClickListener = treeViewClickListener; } /** * @author Apathy、恒 * * 點擊子ExpandableListView子項的回調接口 * */ public interface OnChildTreeViewClickListener { void onClickPosition(int parentPosition, int groupPosition, int childPosition); } }
ChildAdapter.java
package com.heng.tree.adapter; import java.util.ArrayList; import com.heng.tree.R; import com.heng.tree.entity.ChildEntity; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; /** * * @author Apathy、恒 * * <br/> * <br/> * * 子類分組的適配器 * * <br/> * <br/> * * 方法{@link #isChildSelectable(int,int)} <b><font color='#ff00ff' * size='2'>必須返回true</font></b> * * */ public class ChildAdapter extends BaseExpandableListAdapter { private Context mContext;// 上下文 private ArrayList<ChildEntity> mChilds;// 數據源 public ChildAdapter(Context context, ArrayList<ChildEntity> childs) { this.mContext = context; this.mChilds = childs; } @Override public int getChildrenCount(int groupPosition) { return mChilds.get(groupPosition).getChildNames() != null ? mChilds .get(groupPosition).getChildNames().size() : 0; } @Override public String getChild(int groupPosition, int childPosition) { if (mChilds.get(groupPosition).getChildNames() != null && mChilds.get(groupPosition).getChildNames().size() > 0) return mChilds.get(groupPosition).getChildNames() .get(childPosition).toString(); return null; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View convertView, ViewGroup parent) { ChildHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.child_child_item, null); holder = new ChildHolder(convertView); convertView.setTag(holder); } else { holder = (ChildHolder) convertView.getTag(); } holder.update(getChild(groupPosition, childPosition)); return convertView; } /** * @author Apathy、恒 * * Holder優化 * */ class ChildHolder { private TextView childChildTV; public ChildHolder(View v) { childChildTV = (TextView) v.findViewById(R.id.childChildTV); } public void update(String str) { childChildTV.setText(str); childChildTV.setTextColor(Color.parseColor("#00ffff")); } } @Override public Object getGroup(int groupPosition) { if (mChilds != null && mChilds.size() > 0) return mChilds.get(groupPosition); return null; } @Override public int getGroupCount() { return mChilds != null ? mChilds.size() : 0; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.child_group_item, null); holder = new GroupHolder(convertView); convertView.setTag(holder); } else { holder = (GroupHolder) convertView.getTag(); } holder.update(mChilds.get(groupPosition)); return convertView; } /** * @author Apathy、恒 * * Holder優化 * */ class GroupHolder { private TextView childGroupTV; public GroupHolder(View v) { childGroupTV = (TextView) v.findViewById(R.id.childGroupTV); } public void update(ChildEntity model) { childGroupTV.setText(model.getGroupName()); childGroupTV.setTextColor(model.getGroupColor()); } } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { /** * ============================================== * 此處必須返回true,否則無法響應子項的點擊事件=============== * ============================================== **/ return true; } }
CListAdapter.java
package com.heng.tree.adapter; import java.util.ArrayList; import com.heng.tree.R; import com.heng.tree.entity.ChildEntity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * * @author Apathy、恒 * * 子類子類列表的適配器 * * */ public class CListAdapter extends BaseAdapter { private Context mContext; private ArrayList<ChildEntity> mChilds; public CListAdapter(Context context, ArrayList<ChildEntity> childs) { this.mContext = context; this.mChilds = childs; } @Override public int getCount() { return mChilds != null ? mChilds.size() : 0; } @Override public Object getItem(int position) { if ((getCount() > 0) && (position > 0 && position < mChilds.size())) { return mChilds.get(position); } return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.child_child_item, null); holder = new Holder(convertView); convertView.setTag(holder); } else { holder = (Holder) convertView.getTag(); } holder.update(mChilds.get(position).getGroupName()); return convertView; } class Holder { private TextView tv; public Holder(View v) { tv = (TextView) v.findViewById(R.id.childChildTV); } public void update(String text) { tv.setText(text); } } }
ParentEntity.java
package com.heng.tree.entity; import java.util.ArrayList; /** * * @author Apathy、恒 * * 子類分組的實體 * * */ public class ParentEntity { private int groupColor; private String groupName; private ArrayList<ChildEntity> childs; /* ========================================================== * ======================= get method ======================= * ========================================================== */ public int getGroupColor() { return groupColor; } public String getGroupName() { return groupName; } public ArrayList<ChildEntity> getChilds() { return childs; } /* ========================================================== * ======================= set method ======================= * ========================================================== */ public void setGroupColor(int groupColor) { this.groupColor = groupColor; } public void setGroupName(String groupName) { this.groupName = groupName; } public void setChilds(ArrayList<ChildEntity> childs) { this.childs = childs; } }
ChildEntity.java
package com.heng.tree.entity; import java.util.ArrayList; /** * * @author Apathy、恒 * * 父類分組的實體 * * */ public class ChildEntity { private int groupColor; private String groupName; private ArrayList<String> childNames; /* ========================================================== * ======================= get method ======================= * ========================================================== */ public int getGroupColor() { return groupColor; } public String getGroupName() { return groupName; } public ArrayList<String> getChildNames() { return childNames; } /* ========================================================== * ======================= set method ======================= * ========================================================== */ public void setGroupColor(int groupColor) { this.groupColor = groupColor; } public void setGroupName(String groupName) { this.groupName = groupName; } public void setChildNames(ArrayList<String> childNames) { this.childNames = childNames; } }
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ExpandableListView android:id="@+id/eList" android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@null" /> </FrameLayout>
parent_group_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="@dimen/parent_expandable_list_group_padding_left" > <TextView android:id="@+id/parentGroupTV" android:layout_width="wrap_content" android:layout_height="@dimen/parent_expandable_list_height" android:gravity="center_vertical" /> </RelativeLayout>
child_group_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/parent_expandable_list_height" > <TextView android:id="@+id/childGroupTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:paddingLeft="@dimen/child_expandable_list_group_padding_left" /> </RelativeLayout> </LinearLayout>
child_child_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/parent_expandable_list_height" > <TextView android:id="@+id/childChildTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:paddingLeft="@dimen/child_expandable_list_child_padding_left" /> </RelativeLayout> </LinearLayout>
dimens.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="parent_expandable_list_height">50dp</dimen> <dimen name="parent_expandable_list_group_padding_left">10dp</dimen> <dimen name="child_expandable_list_group_padding_left">40dp</dimen> <dimen name="child_expandable_list_child_padding_left">75dp</dimen> </resources>
點此下載demo
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。