<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ExpandableListView是ListView的子類,他在ListView上進行了擴展,它把列表項分成了幾組,每組里包含了多個列表項 ExpandableListView的列表項是由ExpandableListAdapter提供的,實現**ExpandableListAdapter三種常用方式**,常用的ExpandableListAdapter子類如下: 1,擴展BaseExpandableListAdapter實現ExpandableListAdapter 2,使用SimpleExpandableListAdapter將 兩個List集合包裝成ExpandableListAdapter 3,使用SimpleCursorTreeAdapter將Cursor中的數據包裝成SimpleCursorTreeAdapter ExpandableListAdapter及其子類的繼承關系類圖看這篇文章講的比較好,看懂了:[http://hubingforever.blog.163.com/blog/static/1710405792010538823477/](http://hubingforever.blog.163.com/blog/static/1710405792010538823477/) ExpandableListView的xml屬性: ![](https://box.kancloud.cn/2016-03-10_56e0d9ad09cf8.jpg) android:childDivider 指定個組內各子列表項之間的分隔條 android:childIndicator 顯示子列表項旁邊的Drawble對象 android:groupIndicator 顯示組列表項旁邊的Drawble對象 ExpandableListView是android中可以實現下拉list的一個控件,是一個垂直滾動的心事兩個級別列表項手風琴試圖,列表項是來自ExpandableListViewaAdapter,組可以單獨展開。 重要方法: ~~~ <span style="font-size:24px;">expandGroup (int groupPos) ;//在分組列表視圖中 展開一組, setSelectedGroup (int groupPosition) ;//設置選擇指定的組。 setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//設置選擇指定的子項。 getPackedPositionGroup (long packedPosition);//返回所選擇的組 getPackedPositionForChild (int groupPosition, int childPosition) ;//返回所選擇的子項 getPackedPositionType (long packedPosition);//返回所選擇項的類型(Child,Group) isGroupExpanded (int groupPosition);//判斷此組是否展開</span> ~~~ ~~~ <span style="font-size:24px;">expandableListView.setDivider();這個是設定每個Group之間的分割線。 expandableListView.setGroupIndicator();這個是設定每個Group之前的那個圖標。 expandableListView.collapseGroup(int group); 將第group組收起</span> ~~~ **一,使用擴展BaseExpandableListAdapter來提供數據源** 擴展BaseExpandableListAdapter需要重寫11個方法: getGroupCount(),返回包含組列表的數量 getChildrenCount(int groupPosition),返回包含組列表的數量 getGroup(int groupPosition),返回組列表的對象 getChild(int groupPosition, int childPosition),返回組列表下的子列表對象 getGroupId(int groupPosition),返回組列表Id getChildId(int groupPosition, int childPosition),返回祖列表的子列表的Id 下面兩個屬性很重要! getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent),該方法決定每個組選項的外觀、 getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent),該方法決定每個子選項的外觀、** isChildSelectable(int groupPosition, int childPosition):如果child添加監聽事件,則要返回true Main.xml ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--ExpandableListView組件 android:childDivider 指定個組內各子列表項之間的分隔條 android:childIndicator 顯示子列表項旁邊的Drawble對象 android:groupIndicator 顯示組列表項旁邊的Drawble對象 --> <ExpandableListView android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:childDivider="#f0f" > </ExpandableListView> </LinearLayout> ~~~ MainActivity.java ~~~ package com.hust.expandablelistview; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.LayoutParams; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BaseExpandableListAdapter bela=new BaseExpandableListAdapter(){ /*自動實現這10個方法 * getGroupCount(),返回包含組列表的數量 * getChildrenCount(int groupPosition),返回包含組列表的數量 * * getGroup(int groupPosition),返回組列表的對象 * getChild(int groupPosition, int childPosition),返回組列表下的子列表對象 * * getGroupId(int groupPosition),返回組列表Id * getChildId(int groupPosition, int childPosition),返回祖列表的子列表的Id * * getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent),該方法決定每個組選項的外觀、 * getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent),該方法決定每個子選項的外觀、 * * * */ int[] logos=new int[]{ R.drawable.p,R.drawable.z,R.drawable.t }; //組列表的數量 private String[] armTypes=new String[]{"我的好友","高中同學","大學同學"}; //組列表下的子列表項,可擴展的ExpandableListView是個二維數組 private String[][] arms=new String[][]{ {"狂戰士","龍騎士","黑暗圣堂","電兵"}, {"張娜","李四","趙龍","錢爽"}, {"王燕","劉濤","張坦克","汪明城"} }; //返回包含組列表的數量 @Override public int getGroupCount() { // TODO Auto-generated method stub return armTypes.length; } //返回組位置下的子列表項的數量 @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return arms[groupPosition].length; } //返回組列表的對象 @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return armTypes[groupPosition]; } //返回組列表下的子列表對象 @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return arms[groupPosition][childPosition]; } //返回組列表Id @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } //返回祖列表的子列表的Id @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } //該方法決定每個組選項的外觀、這里這定義組列表布局,也可以用xml文件 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LinearLayout ll=new LinearLayout(MainActivity.this); ll.setOrientation(0); ImageView logo=new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); TextView textview=getTextView(); textview.setText(getGroup(groupPosition).toString()); ll.addView(logo); ll.addView(textview); return ll; } //設置TextView的參數 private TextView getTextView() { // TODO Auto-generated method stub AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64); TextView textview=new TextView(MainActivity.this); textview.setLayoutParams(lp);//設置布局參數,android:layout_Width="match_parent",android:layout_Height="64" textview.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);//android:gravity="CENTER_VERTICAL|LEFT" textview.setPadding(36, 0, 0, 0); textview.setTextSize(16); //android:textsize="20dp" return textview; } //該方法決定每個子選項的外觀 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView text=getTextView(); text.setText(getChild(groupPosition,childPosition).toString()); return text; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } }; ExpandableListView expandablelistview=(ExpandableListView) findViewById(R.id.expandableListView1); //設置adapter expandablelistview.setAdapter(bela); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } ~~~ ![](https://box.kancloud.cn/2016-03-10_56e0d9ad21287.jpg) **二,使用SimpleExpandableListAdapter顯示ExpandableListView** ~~~ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, gruops, R.drawable.expandablelistview_groups, new String[]{"group"}, new int[]{R.id.textGroup}, childs, R.drawable.expandablelistview_child, new String[]{"child"}, new int[]{R.id.textChild} ); ~~~ * 參數1.上下文對象Context * 參數2.一級條目目錄集合 * 參數3.一級條目對應的布局文件 (expandablelistview_groups.xml文件 * 參數4.fromto,就是map中的key,指定要顯示的對象 * 參數5.與參數4對應,指定要顯示在groups中的id * 參數6.二級條目目錄集合 * 參數7.二級條目對應的布局文件 * 參數9.與參數8對應,指定要顯示在childs中的id 1,定義一個主界面expandablelistview.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:orientation="vertical" > <ExpandableListView android:id ="@+id/expandableListView" android:layout_width ="fill_parent" android:layout_height ="wrap_content" > </ExpandableListView> </LinearLayout> ~~~ 2.在res/drawable目錄下創建樣式文件expandablelistview_groups.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:orientation="vertical" > <TextView android:id="@+id/textGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="40px" android:paddingTop="6px" android:paddingBottom="6px" android:textSize="15sp" android:text="No data" > </TextView> </LinearLayout> ~~~ 3.在res/drawable目錄下創建樣式文件expandablelistview_child.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:orientation="vertical" > <TextView android:id="@+id/textChild" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="20sp" android:text="No Data" /> </LinearLayout> ~~~ ExpandableListViewDemo_two.java ~~~ public class ExpandableListViewDemo_two extends Activity { /** Called when the activity is first created. */ private ExpandableListView expandableListView_one; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandablelistview); expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView); //創建二個一級條目標題 Map<String, String> title_1 = new HashMap<String, String>(); Map<String, String> title_2 = new HashMap<String, String>(); title_1.put("group", "移動開發"); title_2.put("group", "Web開發"); //創建一級條目容器 List<Map<String, String>> gruops = new ArrayList<Map<String,String>>(); gruops.add(title_1); gruops.add(title_2); //創建二級條目內容 //內容一 Map<String, String> content_1 = new HashMap<String, String>(); Map<String, String> content_2 = new HashMap<String, String>(); content_1.put("child", "ANDROID"); content_2.put("child", "IOS"); List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>(); childs_1.add(content_1); childs_1.add(content_2); //內容二 Map<String, String> content_3 = new HashMap<String, String>(); Map<String, String> content_4 = new HashMap<String, String>(); Map<String, String> content_5 = new HashMap<String, String>(); content_3.put("child", "jsp"); content_4.put("child", "servlet"); content_5.put("child", "page"); List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>(); childs_2.add(content_3); childs_2.add(content_4); childs_2.add(content_5); //存放兩個內容, 以便顯示在列表中 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>(); childs.add(childs_1); childs.add(childs_2); //創建ExpandableList的Adapter容器 /** * 使用SimpleExpandableListAdapter顯示ExpandableListView * 參數1.上下文對象Context * 參數2.一級條目目錄集合 * 參數3.一級條目對應的布局文件 (expandablelistview_groups.xml文件 * 參數4.fromto,就是map中的key,指定要顯示的對象 * 參數5.與參數4對應,指定要顯示在groups中的id * 參數6.二級條目目錄集合 * 參數7.二級條目對應的布局文件 * 參數9.與參數8對應,指定要顯示在childs中的id / SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, gruops, R.drawable.expandablelistview_groups, new String[]{"group"}, new int[]{R.id.textGroup}, childs, R.drawable.expandablelistview_child, new String[]{"child"}, new int[]{R.id.textChild} ); //加入列表 expandableListView_one.setAdapter(adapter); expandableListView_one.setOnChildClickListener(listener); } private OnChildClickListener listener =new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub toast("點擊了"); return false; } }; private void toast(String str) { Toast.makeText(this, str, Toast.LENGTH_LONG).show(); } } ~~~ 上面的樣式也可以使用系統的自帶的樣式如下: android.R.layout.simple_expandable_list_item_1,//層顯示樣式 ,系統自定義 android.R.layout.simple_expandable_list_item_2, **ExpandableListActivity直接繼承了Activity。** 1,繼承ExpandableListActivity 2,定義好內容ExpandableListAdapter,擴展BaseExpandableListAdapter和SimpleExpandableListAdapter都可以 3,setListAdapter的方法添加adapter? ~~~ package com.example.expandablelistactivity; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends ExpandableListActivity//繼承ExpandableListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //無需布局文件 //自定義擴展BaseExpandableListAdapter ExpandableListAdapter adapter = new BaseExpandableListAdapter() { int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t }; //組列表的數量 private String[] armTypes=new String[]{"我的好友","高中同學","大學同學"}; //組列表下的子列表項,可擴展的ExpandableListView是個二維數組 private String[][] arms=new String[][]{ {"狂戰士","龍騎士","黑暗圣堂","電兵"}, {"張娜","李四","趙龍","錢爽"}, {"王燕","劉濤","張坦克","汪明城"} }; //獲取指定組位置、指定子列表項處的子列表項數據 @Override public Object getChild(int groupPosition, int childPosition) { return arms[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return arms[groupPosition].length; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36, 0, 0, 0); textView.setTextSize(16); return textView; } //該方法決定每個子選項的外觀 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } //獲取指定組位置處的組數據 @Override public Object getGroup(int groupPosition) { return armTypes[groupPosition]; } @Override public int getGroupCount() { return armTypes.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } //該方法決定每個組選項的外觀 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); ll.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } }; // 設置該窗口顯示列表 setListAdapter(adapter); } } ~~~ ![](https://box.kancloud.cn/2016-03-10_56e0d9ad48ec5.jpg) 附上源碼更方便學習: ~~~ public class ExpandableListActivity extends Activity implements OnCreateContextMenuListener, ExpandableListView.OnChildClickListener, ExpandableListView.OnGroupCollapseListener, ExpandableListView.OnGroupExpandListener { ExpandableListAdapter mAdapter; ExpandableListView mList; boolean mFinishedStart = false; /** * Override this to populate the context menu when an item is long pressed. menuInfo * will contain an {@link android.widget.ExpandableListView.ExpandableListContextMenuInfo} * whose packedPosition is a packed position * that should be used with {@link ExpandableListView#getPackedPositionType(long)} and * the other similar methods. * <p> * {@inheritDoc} */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { } /** * Override this for receiving callbacks when a child has been clicked. * <p> * {@inheritDoc} */ public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { return false; } /** * Override this for receiving callbacks when a group has been collapsed. */ public void onGroupCollapse(int groupPosition) { } /** * Override this for receiving callbacks when a group has been expanded. */ public void onGroupExpand(int groupPosition) { } /** * Ensures the expandable list view has been created before Activity restores all * of the view states. * *@see Activity#onRestoreInstanceState(Bundle) */ @Override protected void onRestoreInstanceState(Bundle state) { ensureList(); super.onRestoreInstanceState(state); } /** * Updates the screen state (current list and other views) when the * content changes. * * @see Activity#onContentChanged() */ @Override public void onContentChanged() { super.onContentChanged(); View emptyView = findViewById(com.android.internal.R.id.empty); mList = (ExpandableListView)findViewById(com.android.internal.R.id.list); if (mList == null) { throw new RuntimeException( "Your content must have a ExpandableListView whose id attribute is " + "'android.R.id.list'"); } if (emptyView != null) { mList.setEmptyView(emptyView); } mList.setOnChildClickListener(this); mList.setOnGroupExpandListener(this); mList.setOnGroupCollapseListener(this); if (mFinishedStart) { setListAdapter(mAdapter); } mFinishedStart = true; } /** * Provide the adapter for the expandable list. */ public void setListAdapter(ExpandableListAdapter adapter) { synchronized (this) { ensureList(); mAdapter = adapter; mList.setAdapter(adapter); } } /** * Get the activity's expandable list view widget. This can be used to get the selection, * set the selection, and many other useful functions. * * @see ExpandableListView */ public ExpandableListView getExpandableListView() { ensureList(); return mList; } /** * Get the ExpandableListAdapter associated with this activity's * ExpandableListView. */ public ExpandableListAdapter getExpandableListAdapter() { return mAdapter; } private void ensureList() { if (mList != null) { return; } setContentView(com.android.internal.R.layout.expandable_list_content); } /** * Gets the ID of the currently selected group or child. * * @return The ID of the currently selected group or child. */ public long getSelectedId() { return mList.getSelectedId(); } /** * Gets the position (in packed position representation) of the currently * selected group or child. Use * {@link ExpandableListView#getPackedPositionType}, * {@link ExpandableListView#getPackedPositionGroup}, and * {@link ExpandableListView#getPackedPositionChild} to unpack the returned * packed position. * * @return A packed position representation containing the currently * selected group or child's position and type. */ public long getSelectedPosition() { return mList.getSelectedPosition(); } /** * Sets the selection to the specified child. If the child is in a collapsed * group, the group will only be expanded and child subsequently selected if * shouldExpandGroup is set to true, otherwise the method will return false. * * @param groupPosition The position of the group that contains the child. * @param childPosition The position of the child within the group. * @param shouldExpandGroup Whether the child's group should be expanded if * it is collapsed. * @return Whether the selection was successfully set on the child. */ public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) { return mList.setSelectedChild(groupPosition, childPosition, shouldExpandGroup); } /** * Sets the selection to the specified group. * @param groupPosition The position of the group that should be selected. */ public void setSelectedGroup(int groupPosition) { mList.setSelectedGroup(groupPosition); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看