【Android】樹形菜單、擴充下拉式功能表BaseExpandableListAdapter、AbsListView

來源:互聯網
上載者:User

先看效果~

 

也就是BaseExpandableListAdapter、AbsListView類的使用,就不多說了..大牛留情...

就兩個類。

ExpandLabel:

package com.yfz;<br />import java.util.ArrayList;<br />import java.util.List;<br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.ExpandableListView;<br />import android.widget.ViewFlipper;<br />public class ExpandLabel extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> // 資料來源<br /> //標題<br /> List<String> groupArray = new ArrayList<String>();<br /> //子標題<br /> List<List<String>> childArray = new ArrayList<List<String>>();<br />groupArray.add("第一章");<br />groupArray.add("第二章");<br />List<String> tempArray01 = new ArrayList<String>();<br />tempArray01.add("第1.1節");<br />tempArray01.add("第1.2節");<br />tempArray01.add("第1.3節");</p><p>List<String> tempArray02 = new ArrayList<String>();<br />tempArray02.add("第2.1節");<br />tempArray02.add("第2.2節");<br />tempArray02.add("第2.3節");</p><p>childArray.add(tempArray01);<br />childArray.add(tempArray02);</p><p> ExpandableListView expandableListView = new ExpandableListView(this);</p><p> ExpandableAdapter adapter = new ExpandableAdapter(this,groupArray,childArray);</p><p>expandableListView.setAdapter(adapter);</p><p>addContentView(expandableListView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));<br /> }<br />}

樹形菜單類,只要傳兩個List過來就可以直接使用了。

groupArray:表示一級菜單

childArray: 表示二級菜單

二級菜單相當於一個二維List,該List中儲存的每個元素都是一個List。第一個元素對應一級菜單中第一個菜單的子項List,第二個元素對應一級菜單中第二個菜單的子項List,依次類推。

ExpandableAdapter:

package com.yfz;<br />import java.util.ArrayList;<br />import java.util.List;<br />import android.app.Activity;<br />import android.view.Gravity;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.widget.AbsListView;<br />import android.widget.BaseExpandableListAdapter;<br />import android.widget.TextView;<br />public class ExpandableAdapter extends BaseExpandableListAdapter {</p><p>private List<String> groupArray;<br />private List<List<String>> childArray;<br />private Activity activity;<br />public ExpandableAdapter(Activity a,List<String> groupArray,List<List<String>> childArray) {<br />activity = a;<br />this.groupArray = groupArray;<br />this.childArray = childArray;<br />}</p><p>@Override<br />public Object getChild(int groupPosition, int childPosition) {<br />// TODO Auto-generated method stub<br />return childArray.get(groupPosition).get(childPosition);<br />}<br />@Override<br />public long getChildId(int groupPosition, int childPosition) {<br />// TODO Auto-generated method stub<br />return childPosition;<br />}<br />@Override<br />public View getChildView(int groupPosition, int childPosition,<br />boolean isLastChild, View convertView, ViewGroup parent) {<br />// TODO Auto-generated method stub<br />String string = childArray.get(groupPosition).get(childPosition);<br />return getGenericView(string);<br />}<br />@Override<br />public int getChildrenCount(int groupPosition) {<br />// TODO Auto-generated method stub<br />return childArray.get(groupPosition).size();<br />}<br />@Override<br />public Object getGroup(int groupPosition) {<br />// TODO Auto-generated method stub<br />return groupArray.get(groupPosition);<br />}<br />@Override<br />public int getGroupCount() {<br />// TODO Auto-generated method stub<br />return groupArray.size();<br />}<br />@Override<br />public long getGroupId(int groupPosition) {<br />// TODO Auto-generated method stub<br />return groupPosition;<br />}<br />@Override<br />public View getGroupView(int groupPosition, boolean isExpanded,<br />View convertView, ViewGroup parent) {<br />// TODO Auto-generated method stub<br />String string = groupArray.get(groupPosition);<br />return getGenericView(string);<br />}<br />@Override<br />public boolean hasStableIds() {<br />// TODO Auto-generated method stub<br />return false;<br />}<br />@Override<br />public boolean isChildSelectable(int groupPosition, int childPosition) {<br />// TODO Auto-generated method stub<br />return false;<br />}</p><p>/****************************************以下為自訂方法*********************************************/<br />/**<br /> * Children 's View<br /> * @param string<br /> * @return<br /> */<br />public TextView getGenericView(String string) {<br />AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);<br />TextView text = new TextView(activity);<br />text.setLayoutParams(layoutParams);<br />// Center the text vertically<br />text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);<br />// Set the text starting position<br />text.setPadding(36, 0, 0, 0);<br />text.setText(string);<br />return text;<br />}<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.