日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > 【安卓筆記】ExpandableListView的使用

【安卓筆記】ExpandableListView的使用

來源:程序員人生   發(fā)布時間:2014-12-16 08:12:40 閱讀次數(shù):3705次

實現(xiàn)效果:

便可舒展的ListView



其實跟普通的ListView使用沒啥區(qū)分,只是ListView改成了ExpandableListView,另外適配器由BaseAdapter也換成了BaseExpandableListAdapter。

步驟:

1.編寫布局文件。

分為3個,分別是主布局,group分組布局,group item布局:
activity_main.xml:
<RelativeLayout 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" tools:context="com.example.expandablelistviewdemo.MainActivity" > <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#000" android:dividerHeight="2.5dp" android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft" > </ExpandableListView> </RelativeLayout>

list_group.xml:
<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/listTitle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" android:paddingTop="10dp" android:textColor="#A4C739" /> </LinearLayout>
list_item.xml:

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/expandedListItem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" android:paddingTop="10dp" /> </LinearLayout>
2.編寫適配器代碼:

package com.example.expandablelistviewdemo; import java.util.HashMap; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; public class MyExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> expandableListTitle; private HashMap<String, List<String>> expandableListDetail; public MyExpandableListAdapter(List<String> expandableListTitle,HashMap<String,List<String>> expandableListDetail,Context mContext) { this.expandableListDetail = expandableListDetail; this.expandableListTitle = expandableListTitle; this.mContext = mContext; } @Override public int getGroupCount()//分組數(shù) { return this.expandableListTitle.size(); } @Override public int getChildrenCount(int groupPosition)//分組內(nèi)的item數(shù) { return this.expandableListDetail.get(expandableListTitle.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition)//獲得分組數(shù)據(jù) { return this.expandableListTitle.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition)//獲得第幾分組第幾個item的數(shù)據(jù) { return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String data = this.expandableListTitle.get(groupPosition); if(convertView == null) { convertView = View.inflate(mContext,R.layout.list_group, null); } TextView tv = (TextView) convertView.findViewById(R.id.listTitle); tv.setText(data); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // String data = this.expandableListDetail.get(this.expandableListTitle.get(groupPosition)).get(childPosition); String data = (String) this.getChild(groupPosition, childPosition); if(convertView == null) { convertView = View.inflate(mContext, R.layout.list_item, null); } TextView tv = (TextView) convertView.findViewById(R.id.expandedListItem); tv.setText(data); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }

可以看到,適配器中有兩個getView的方法,分別去構(gòu)造group和child的view。

3.構(gòu)造數(shù)據(jù)源
package com.example.expandablelistviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ExpandableListDataPump { public static HashMap<String, List<String>> getData() { HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); List<String> technology = new ArrayList<String>(); technology.add("Beats sued for noise-cancelling tech"); technology.add("Wikipedia blocks US Congress edits"); technology.add("Google quizzed over deleted links"); technology.add("Nasa seeks aid with Earth-Mars links"); technology.add("The Good, the Bad and the Ugly"); List<String> entertainment = new ArrayList<String>(); entertainment.add("Goldfinch novel set for big screen"); entertainment.add("Anderson stellar in Streetcar"); entertainment.add("Ronstadt receives White House honour"); entertainment.add("Toronto to open with The Judge"); entertainment.add("British dancer return from Russia"); List<String> science = new ArrayList<String>(); science.add("Eggshell may act like sunblock"); science.add("Brain hub predicts negative events"); science.add("California hit by raging wildfires"); science.add("Rosetta's comet seen in close-up"); science.add("Secret of sandstone shapes revealed"); expandableListDetail.put("TECHNOLOGY NEWS", technology); expandableListDetail.put("ENTERTAINMENT NEWS", entertainment); expandableListDetail.put("SCIENCE & ENVIRONMENT NEWS", science); return expandableListDetail; } }

4.編寫主界面的代碼
package com.example.expandablelistviewdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.Toast; public class MainActivity extends Activity { private ExpandableListView elv = null; private MyExpandableListAdapter adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); elv = (ExpandableListView) findViewById(R.id.elv); final HashMap<String,List<String>> itemData = ExpandableListDataPump.getData(); final List<String> title = new ArrayList<String>(itemData.keySet()); adapter = new MyExpandableListAdapter(title,itemData,this); elv.setAdapter(adapter); //去掉箭頭 // elv.setGroupIndicator(null); //收縮 elv.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { Toast.makeText(MainActivity.this,title.get(groupPosition)+" group collapse..... ", 0).show(); } }); //舒展 elv.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { Toast.makeText(MainActivity.this,title.get(groupPosition)+" group expand... ", 0).show(); } }); //子條目點擊 elv.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this,itemData.get(title.get(groupPosition)).get(childPosition)+" click", 0).show(); return false; } }); } }

ok,完成。
這里需要注意的是ExpandableListView有多個監(jiān)聽器,group收縮和舒展和子條目點擊等等,按需監(jiān)聽。
另外,默許情況下顯示的時候有個箭頭,你可以通過setGroupIndicator方法設(shè)置你需要的標(biāo)識。

源碼地址:https://github.com/Rowandjj/ExpandableListViewDemo




生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产精品入口 | 国产精品成人在线 | 九九看片 | 日韩久久av | 欧美99久久精品乱码影视 | 成人欧美一区二区三区黑人免费 | www高清| 久久精品夜夜夜夜夜久久 | 亚洲午夜精品 | 亚洲欧洲激情在线乱码蜜桃 | 欧美一级精品片在线看 | 欧美性大战久久久久久久蜜臀 | 久久99精品久久久久久久久久久久 | 精品久久久久久综合日本 | 成人国产精品久久久 | 国产一区二区成人 | 久久日本 | 亚洲一区二区在线视频 | 亚洲欧美日韩在线播放 | 国产精品久久久久久久午夜片 | 免费观看黄 | 国产精品初高中精品久久 | 欧美日韩国产精品一区 | 国产日韩欧美一区二区三区乱码 | 91精彩视频在线观看 | 国产精品高潮在线观看 | 91香蕉视频污在线观看 | 国产a级大片 | 免费的三级毛片 | 欧美区一| 国产理论一区二区三区 | 9999精品| 国产欧美精品一区二区 | 精品成人一区二区 | 黄色免费高清视频 | 性色av一区二区三区 | 91色网站| 欧美色欧美亚洲另类七区 | 欧美精品一区三区 | 欧美大片一区二区三区 | 一本色道久久综合亚洲二区三区 |