Android中ExpandableListView控件基本使用 – Android-Idea – 博客频道 – CSDN.NET

本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源。直接上代码如下:

程序结构图:

layout目录下的 main.xml 文件源码如下:

&lt;?xml version="1.0" encoding="utf-8"?&gt;</p><p>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</p><p>    android:orientation="vertical"</p><p>    android:layout_width="fill_parent"</p><p>    android:layout_height="fill_parent"&gt;</p><p>    &lt;!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式 </p><p>         如果自定义listview的显示方式这里这个android:id="@id/android:<b style="color:black;background-color:#A0FFFF">list</b>" 必须这样写 --&gt;</p><p>    &lt;!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会</p><p> 	 挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 --&gt; </p><p>    &lt;ExpandableListView </p><p>        android:id="@id/android:<b style="color:black;background-color:#A0FFFF">list</b>"               </p><p>	 	android:layout_width="fill_parent"                </p><p>	 	android:layout_height="wrap_content"              </p><p>	 	android:layout_weight="1"               </p><p>	 	android:drawSelectorOnTop="false"/&gt; </p><p>&lt;/LinearLayout&gt;</p><p>

包 com.andyidea.demo中ContactsActivity.java源码如下:

package com.andyidea.demo;</p><p>import java.util.ArrayList;</p><p>import java.util.<b style="color:black;background-color:#A0FFFF">List</b>;</p><p>import android.app.ExpandableListActivity;</p><p>import android.os.Bundle;</p><p>import android.<b style="color:black;background-color:#99ff99">view</b>.Gravity;</p><p>import android.<b style="color:black;background-color:#99ff99">view</b>.<b style="color:black;background-color:#99ff99">View</b>;</p><p>import android.<b style="color:black;background-color:#99ff99">view</b>.ViewGroup;</p><p>import android.<b style="color:black;background-color:#99ff99">view</b>.Window;</p><p>import android.widget.AbsListView;</p><p>import android.widget.BaseExpandableListAdapter;</p><p>import android.widget.TextView;</p><p>public class ContactsActivity extends ExpandableListActivity {</p><p>	</p><p>	<b style="color:black;background-color:#A0FFFF">List</b>&lt;String&gt; group;           //组列表</p><p>	<b style="color:black;background-color:#A0FFFF">List</b>&lt;<b style="color:black;background-color:#A0FFFF">List</b>&lt;String&gt;&gt; child;     //子列表</p><p>	ContactsInfoAdapter adapter;  //数据适配器</p><p>	</p><p>    /** Called when the activity is first created. */</p><p>    @Override</p><p>    public void onCreate(Bundle savedInstanceState) {</p><p>        super.onCreate(savedInstanceState);</p><p>        requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置为无标题</p><p>        setContentView(R.layout.main);</p><p>        getExpandableListView().setBackgroundResource(R.drawable.default_bg);</p><p>        </p><p>        initializeData();</p><p>        getExpandableListView().setAdapter(new ContactsInfoAdapter());</p><p>        getExpandableListView().setCacheColorHint(0);  //设置拖动列表的时候防止出现黑色背景</p><p>    }</p><p>    </p><p>    /**</p><p>     * 初始化组、子列表数据</p><p>     */</p><p>    private void initializeData(){</p><p>    	group = new ArrayList&lt;String&gt;();</p><p>    	child = new ArrayList&lt;<b style="color:black;background-color:#A0FFFF">List</b>&lt;String&gt;&gt;();</p><p>    	</p><p>    	addInfo("Andy",new String[]{"male","138123***","GuangZhou"});</p><p>    	addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});</p><p>    	addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});</p><p>    	addInfo("Tom",new String[]{"female","138123***","ShangHai"});</p><p>    	addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});</p><p>    	</p><p>    }</p><p>    </p><p>    /**</p><p>     * 模拟给组、子列表添加数据</p><p>     * @param g-group</p><p>     * @param c-child</p><p>     */</p><p>    private void addInfo(String g,String[] c){</p><p>    	group.add(g);</p><p>    	<b style="color:black;background-color:#A0FFFF">List</b>&lt;String&gt; childitem = new ArrayList&lt;String&gt;();</p><p>    	for(int i=0;i&lt;c.length;i++){</p><p>    		childitem.add(c[i]);</p><p>    	}</p><p>    	child.add(childitem);</p><p>    }</p><p>    </p><p>    class ContactsInfoAdapter extends BaseExpandableListAdapter{</p><p>    	</p><p>    	//-----------------Child----------------//</p><p>    	@Override</p><p>		public Object getChild(int groupPosition, int childPosition) {</p><p>			return child.get(groupPosition).get(childPosition);</p><p>		}</p><p>    	</p><p>    	@Override</p><p>		public long getChildId(int groupPosition, int childPosition) {</p><p>			return childPosition;</p><p>		}</p><p>    	</p><p>    	@Override</p><p>		public int getChildrenCount(int groupPosition) {</p><p>			return child.get(groupPosition).size();</p><p>		}</p><p>    	</p><p>    	@Override</p><p>		public <b style="color:black;background-color:#99ff99">View</b> getChildView(int groupPosition, int childPosition,</p><p>				boolean isLastChild, <b style="color:black;background-color:#99ff99">View</b> convertView, ViewGroup parent) {</p><p>    		String string = child.get(groupPosition).get(childPosition); </p><p>			return getGenericView(string);</p><p>		}</p><p>    	</p><p>    	//----------------Group----------------//</p><p>    	@Override</p><p>		public Object getGroup(int groupPosition) {</p><p>			return group.get(groupPosition);</p><p>		}				</p><p>		@Override</p><p>		public long getGroupId(int groupPosition) {</p><p>			return groupPosition;</p><p>		}	</p><p>		</p><p>		@Override</p><p>		public int getGroupCount() {</p><p>			return group.size();</p><p>		}	</p><p>		</p><p>		@Override</p><p>		public <b style="color:black;background-color:#99ff99">View</b> getGroupView(int groupPosition, boolean isExpanded,</p><p>				<b style="color:black;background-color:#99ff99">View</b> convertView, ViewGroup parent) {</p><p>			String string = group.get(groupPosition);  </p><p>			return getGenericView(string);</p><p>		}</p><p>		//创建组/子视图  </p><p>        public TextView getGenericView(String s) {  </p><p>            // Layout parameters for the ExpandableListView  </p><p>            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  </p><p>                    ViewGroup.LayoutParams.FILL_PARENT, 40);</p><p>  </p><p>            TextView text = new TextView(ContactsActivity.this);  </p><p>            text.setLayoutParams(lp);  </p><p>            // Center the text vertically  </p><p>            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  </p><p>            // Set the text starting position  </p><p>            text.setPadding(36, 0, 0, 0);  </p><p>              </p><p>            text.setText(s);  </p><p>            return text;  </p><p>        }  </p><p>		</p><p>		</p><p>		@Override</p><p>		public boolean hasStableIds() {</p><p>			// TODO Auto-generated method stub</p><p>			return false;</p><p>		}		</p><p>		@Override</p><p>		public boolean isChildSelectable(int groupPosition, int childPosition) {</p><p>			// TODO Auto-generated method stub</p><p>			return true;</p><p>		}</p><p>    	</p><p>    }</p><p>}

最后,程序运行后截图如下:

       

版权声明:本文为博主原创文章,未

来源URL:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246101f3ebaf03a715042889422301cf91e1ab9ab68332a072bb59acf8a4cc9fecf687987707e324fda17418347f8dc47559f7bd11cfeae69a3e1a777&p=cb759a45d5c759fc57efcd605753c1&newp=836cc54ad6c045e019bac7710f528d231610db2151d4d4123095c4&user=baidu&fm=sc&query=ExpandableListView&qid=8464d74b00006c36&p1=2