国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ExpandableListView+Dialog實(shí)現(xiàn)彈出二級(jí)下拉列表選項(xiàng)。可自由用于Activity和Fragment中

2019-11-06 09:34:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

話說(shuō)今天剛是本人失業(yè)第二天。已經(jīng)感覺有點(diǎn)無(wú)聊了。對(duì)于這次失業(yè)挺突然的。不過(guò)想通之后也沒啥可惋惜的。畢竟身懷技術(shù)走到哪都不怕找不到飯碗。作為技術(shù)人員要做的首要就是不斷提高自身技術(shù)水平,以適應(yīng)這個(gè)瞬息萬(wàn)變的社會(huì)。 本人是15年下半年入行,喜歡這行也是興趣所向。喜歡那種解決問題后的成就感,完成一個(gè)功能后的喜悅感。但是覺得自己又比較矛盾,自己家確實(shí)很喜歡編程,但是卻對(duì)上班編程感覺不是那么特別喜歡,原因是:雖然也是同樣的寫程序,上班是要根據(jù)公司領(lǐng)導(dǎo)的安排而去寫一些有時(shí)候自己不是很喜歡的代碼或者是沒有技術(shù)含量的代碼。而自己寫的話肯定是寫一些自己喜歡或者自己感興趣的方向去深究和鉆研。希望自己有一天實(shí)現(xiàn)經(jīng)濟(jì)自由,可以隨意學(xué)習(xí)自己喜歡的技術(shù)吧。

好了上面叨叨了很多,下來(lái)看今天的正題:一個(gè)ExpandableListView+Dialog實(shí)現(xiàn)的彈出二級(jí)下拉菜單選項(xiàng)的功能。直接老規(guī)矩先看效果圖:

這里寫圖片描述

ExpandableListView相信大家從單詞上都能看出來(lái),里面有l(wèi)istview。沒錯(cuò),這里給大家大概說(shuō)一下有興趣的可以繼續(xù)查閱資料了解,ExpandableListView是繼承自ListView。這樣一說(shuō)估計(jì)心里都有個(gè)大概了。

下來(lái)看看具體實(shí)現(xiàn): 這里我們是在Activity里面實(shí)現(xiàn)的 1.首先在 activity_main中添加基本的一些布局,用于后面觸發(fā)事件和顯示結(jié)果數(shù)據(jù)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:orientation="vertical" tools:context="diaog.com.expandablelistviewdialog.MainActivity"> <EditText android:id="@+id/editText_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:textSize="22sp" /> <EditText android:id="@+id/editText_child" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:textSize="22sp" /> <Button android:id="@+id/btnShowDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Show" android:textSize="22sp" /></LinearLayout>

這個(gè)布局沒什么可說(shuō)的。 2. expandablelistview_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ExpandableListView android:id="@+id/ew_dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/white" /></LinearLayout>

這里說(shuō)下它跟listview不同的是要自己獨(dú)立寫一個(gè)layout。下來(lái)是兩個(gè)xml布局文件,一個(gè)顯示一級(jí)目錄一個(gè)顯示對(duì)應(yīng)的二級(jí)目錄

group_item.xml

<?xml version="1.0" encoding="utf-8"?><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/group_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:padding="5dp" android:textSize="24sp" /></LinearLayout>

child_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_margin="5dp" android:layout_height="wrap_content" android:textSize="18sp" /></LinearLayout>

3.下來(lái)是adapter,這里是繼承自BaseExpandableListAdapter

PRivate Context context; private List<String> listGroup = new ArrayList<String>(); private List<List<String>> listChild = new ArrayList<List<String>>(); public MyExpandableListAdapter(Context context, List<String> listGroup, List<List<String>> listChild) { super(); this.context = context; this.listGroup = listGroup; this.listChild = listChild; } @Override public int getGroupCount() { // TODO Auto-generated method stub return listGroup.size(); } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return listChild.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return listGroup.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @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 false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = LinearLayout.inflate(context, R.layout.group_item, null); TextView text_group = (TextView) convertView.findViewById(R.id.group_text); text_group.setText(listGroup.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = LinearLayout.inflate(context, R.layout.child_item, null); TextView text_child = (TextView) convertView.findViewById(R.id.text); text_child.setText(listChild.get(groupPosition).get(childPosition)); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; }

基本跟listview的baseadapter很相似。這里要注意的就是getGroupView和getChildView方法。從方法名稱可以看到,一個(gè)是獲取一級(jí)目錄layout的一個(gè)是獲取其下的二級(jí)目錄的layout。這里我都是飲用了xml的layout的,之前在網(wǎng)上也看到很多人在這里是直接寫一個(gè)getGenericView(String s)方法

public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,50); lp.height=36; TextView text = new TextView(activity); text.setLayoutParams(lp); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setTextColor(Color.BLACK); text.setTextSize(22); text.setText(s); return text; }

不過(guò)我本人還是習(xí)慣盡量講布局和代碼分開,那樣感覺可拓展性比較好,例如將來(lái)的二級(jí)或者一級(jí)目錄不是一行文字而是一個(gè)復(fù)雜的布局呢。

4.下來(lái)是為了方便,對(duì)此功能做了一個(gè)封裝,創(chuàng)建一個(gè)utils類將彈出dialog和添加數(shù)據(jù)等方法進(jìn)行封裝。

public class SecondaryMemuUtils { private Context context; private ExpandableListView expandableListView; // Layout expandablelist.xml private List<String> listGroup = new ArrayList<String>(); private List<List<String>> listChild = new ArrayList<List<String>>(); private String[] groupDate;//一級(jí)目錄數(shù)據(jù) private String[][] childDate;//對(duì)應(yīng)的二級(jí)目錄數(shù)據(jù) public SecondaryMemuUtils(Context context, ExpandableListView expandableListView, List<String> listGroup, List<List<String>> listChild, String[] groupDate, String[][] childDate) { super(); this.context = context; this.expandableListView = expandableListView; this.listGroup = listGroup; this.listChild = listChild; this.groupDate = groupDate; this.childDate = childDate; } /** * 添加二級(jí)菜單數(shù)據(jù) * * @param groupDate */ public void addDate(String[] groupDate) { // TODO Auto-generated method stub for (int i = 0; i < groupDate.length; i++) { String group_text = groupDate[i];// 獲取每一個(gè)一級(jí)菜單text listGroup.add(group_text); List<String> children = new ArrayList<String>(); if (listGroup.get(i).equals(group_text)) { mforTwoArrays(children,childDate,i);// children.add("公路建設(shè)");// children.add("鐵路建設(shè)");// children.add("礦產(chǎn)開發(fā)");// children.add("旅游開發(fā)"); } else if (listGroup.get(i).equals(group_text)) { mforTwoArrays(children,childDate,i);// children.add("河湖養(yǎng)殖");// children.add("毀林草開墾"); } else if (listGroup.get(i).equals(group_text)) { mforTwoArrays(children,childDate,i);// children.add("水污染");// children.add("大氣污染");// children.add("土壤污染");// children.add("固體廢棄物污染");// children.add("噪聲污染"); } else { mforTwoArrays(children,childDate,i);// children.add("放牧");// children.add("砍伐");// children.add("捕撈");// children.add("狩獵");// children.add("道路交通等"); } listChild.add(children); } } /** * 循環(huán)添加數(shù)據(jù) * @param children * @param arrs * @param j */ private void mforTwoArrays(List<String> children,String[][] arrs, int j){ String[] arr2 = arrs[j]; for (int c = 0; c < arr2.length; c++) { children.add(arr2[c]); } } /** * 類型 * @param editText_group * @param editText_child */ public void createExpandableListViewDialog(final EditText editText_group,final EditText editText_child) { // 設(shè)給彈出窗口的view final Dialog dialog; // 彈出類似Spinner選擇項(xiàng)的窗口 View viewList = ((Activity) context).getLayoutInflater().inflate( R.layout.expandablelistview_item, null); dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消dialog的標(biāo)題欄 dialog.show(); dialog.setContentView(viewList); expandableListView = (ExpandableListView) viewList.findViewById(R.id.ew_dialog); // 綁定ExpandableListView的數(shù)據(jù) MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(context,listGroup,listChild); expandableListView.setAdapter(mAdapter); /** * 1 單擊事件 返回 false表示不觸發(fā)單擊事件 */ expandableListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return false; } }); /** * Child 單擊事件 */ expandableListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) throws RuntimeException { Toast.makeText(context, listGroup.get(groupPosition) + "--->>>" + listChild.get(groupPosition) .get(childPosition).toString(), Toast.LENGTH_SHORT) .show(); // editText1.setText(listGroup.get(groupPosition) // + "下的" // + listChild.get(groupPosition).get(childPosition) // .toString()); editText_group.setText(listGroup.get(groupPosition)); editText_child.setText(listChild.get(groupPosition) .get(childPosition).toString()); dialog.dismiss(); return true; } }); }}

這里為了更好的封裝,寫了一個(gè)mforTwoArrays方法。將傳進(jìn)來(lái)的數(shù)據(jù)自動(dòng)循環(huán)添加進(jìn)去。這樣我們就可以傳任意的string類型的二級(jí)目錄數(shù)組數(shù)據(jù)了。

private void mforTwoArrays(List<String> children,String[][] arrs, int j){ String[] arr2 = arrs[j]; for (int c = 0; c < arr2.length; c++) { children.add(arr2[c]); } }

對(duì)于一級(jí)目錄的數(shù)據(jù)添加則比較簡(jiǎn)單

public void addDate(String[] groupDate) { // TODO Auto-generated method stub for (int i = 0; i < groupDate.length; i++) { String group_text = groupDate[i];// 獲取每一個(gè)一級(jí)菜單text listGroup.add(group_text);

就可以循環(huán)添加每一個(gè)一級(jí)目錄列表了。 至此算是大功告成,下來(lái)我們?cè)跍y(cè)試一下吧。 在MainActivity中調(diào)用,首先我們得創(chuàng)建菜單二二級(jí)菜單的數(shù)據(jù)。和裝它們的集合list。然后調(diào)用我們寫好的工具類里面的createExpandableListViewDialog和addDate方法。就是如此簡(jiǎn)單:

public class MainActivity extends AppCompatActivity { private Button btnShowDialog; private ExpandableListView expandableListView; // private List<String> listGroup = new ArrayList<String>(); private List<List<String>> listChild = new ArrayList<List<String>>(); private String[] groupDate = { "開放建設(shè)", "農(nóng)牧漁業(yè)活動(dòng)", "環(huán)境污染", "其他" }; private String[][] childDate = { { "房地產(chǎn)", "公路建設(shè)", "鐵路建設(shè)", "礦產(chǎn)開發(fā)", "旅游開發(fā)", "管理建設(shè)及河道治理" }, { "河湖養(yǎng)殖", "毀林草開墾" }, { "水污染", "大氣污染", "土壤污染", "固體廢棄物污染", "噪聲污染" }, { "放牧", "砍伐", "采挖", "捕撈", "狩獵", "道路交通等" } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editText_child = (EditText) findViewById(R.id.editText_child); final EditText editText_group = (EditText) findViewById(R.id.editText_group); btnShowDialog = (Button) findViewById(R.id.btnShowDialog); btnShowDialog.setOnClickListener(new View.OnClickListener() { SecondaryMemuUtils secondaryMemuUtils = new SecondaryMemuUtils( MainActivity.this, expandableListView, listGroup, listChild, groupDate, childDate); @Override public void onClick(View view) { listChild.clear(); listGroup.clear(); secondaryMemuUtils.addDate(groupDate);// 添加二級(jí)菜單數(shù)據(jù) secondaryMemuUtils.createExpandableListViewDialog(editText_group,editText_child); } }); } }

是不是感覺調(diào)用的代碼不多呢。最后在奉上

源碼


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 综艺| 苏州市| 广丰县| 齐齐哈尔市| 太保市| 凤台县| 久治县| 余干县| 岚皋县| 栾城县| 那坡县| 南涧| 临城县| 前郭尔| 南郑县| 开封县| 额敏县| 方城县| 中山市| 洛阳市| 云林县| 山东| 广西| 淮安市| 资源县| 女性| 蓬溪县| 和田县| 嵊州市| 罗源县| 祁门县| 平顶山市| 雷州市| 南汇区| 南涧| 万全县| 五常市| 古田县| 石首市| 河东区| 鲁山县|