android 之ExpandableListView详解
生活随笔
收集整理的這篇文章主要介紹了
android 之ExpandableListView详解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ExpandableListView是一種可應(yīng)用于某種環(huán)境的下拉列表。
實(shí)例代碼:
package com.example.lenovo.expandablelistview_demo;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView;public class MainActivity extends AppCompatActivity {private ExpandableListView expandableListView;private MyBaseExpandableListAdapter adapter;private String[] group = {"我的好友","陌生人","黑名單"};private String[][] children = {{"小王","急急急","kl","考慮"},{"流域","蠟筆","一扭"},{"ren","du","處矛"}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.expandableListView = (ExpandableListView) this.findViewById(R.id.expandableListView);adapter = new MyBaseExpandableListAdapter();this.expandableListView.setAdapter(adapter);}private final class MyBaseExpandableListAdapter extends BaseExpandableListAdapter{@Override//返回組中元素的個(gè)數(shù)public int getGroupCount() {return group.length;}@Override//根據(jù)組的索引返回當(dāng)前組中子元素的個(gè)數(shù)public int getChildrenCount(int groupPosition) {return children[groupPosition].length;}@Override//返回指定組索引處的元素值public Object getGroup(int groupPosition) {return group[groupPosition];}@Override//返回指定組索引和子元素索引對(duì)應(yīng)的值public Object getChild(int groupPosition, int childPosition) {return children[groupPosition][childPosition];}@Override//返回組的id值public long getGroupId(int groupPosition) {return groupPosition;}@Override//返回指定組中的子元素指定索引的值public long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return false;}@Override//當(dāng)繪制組的View對(duì)象時(shí)自動(dòng)調(diào)用的方法/*** groupPosition :組的索引值* isExpanded:當(dāng)前組下的元素是否被展開**/public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {View view = View.inflate(MainActivity.this,R.layout.group_view,null);TextView textView_group = (TextView) view.findViewById(R.id.textView_group);//得到組名String groupName = group[groupPosition];textView_group.setText(groupName);return view;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {View view = View.inflate(MainActivity.this,R.layout.child_view,null);TextView textView_child = (TextView) view.findViewById(R.id.textView_child);String childrenName = children[groupPosition][childPosition];textView_child.setText(childrenName);return view;}@Override//子元素是否被選中,默認(rèn)值為false,表示不能被選中public boolean isChildSelectable(int groupPosition, int childPosition) {return true;}} }總結(jié)
以上是生活随笔為你收集整理的android 之ExpandableListView详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 之图文混排+GridVi
- 下一篇: 面向对象的一个实例 (银行管理系统)