二级目录ExpandableListView的用法

这篇博客详细解析了ExpandableListView的工作原理,包括其继承结构和适配器机制,并介绍了如何设置监听器来捕获点击事件。此外,还讨论了在布局文件中添加ExpandableListView的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍了ExpandableListView的用法

1、分析ExpandableListView的原理

1)ExpandableListView的继承层次

ExpandableListView->ListView->AbsListView->AdapterView->ViewGroup->View

BaseExpandableListAdapter->ExpandableListAdapter(HeterogeneousExpandableList)

setAdapter(ListAdapter)被弃用,换成了setAdapter(ExpandableListAdapter)

BaseExpandableListAdapter中使用了DataSetObservable来广播数据变化,类似于BaseAdapter

2)新增下面几个listener来监听点击事件

OnGroupCollapseListener:监听group折叠事件
OnGroupExpandListener:监听group展开事件
OnGroupClickListener:监听group点击事件
OnChildClickListener:监听child点击事件

2、 具体代码实现

在布局文件添加ExpandableListView控件

<ExpandableListView 
        android:id="@+id/expandablelistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#ffaaaaaa"
        android:dividerHeight="1dp"
        android:childDivider="#ffaaaaaa"
        android:layout_marginTop="20dp"
        android:groupIndicator="@null"/>
在Activity中给ExpandableListView设置内容

public class ExpandableListViewDemo extends Activity {
	private static final String TAG = ExpandableListViewDemo.class.getSimpleName();
	ExpandableListView mExpandableListView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_expandablelistviewdemo);
		mExpandableListView = (ExpandableListView) findViewById(R.id.expandablelistview);
		mExpandableListView.setAdapter(mExpandableListAdapter);
		mExpandableListView.setOnChildClickListener(mOnChildClickListener);
		mExpandableListView.setOnGroupClickListener(mOnGroupClickListener);
		mExpandableListView.setOnGroupExpandListener(mOnGroupExpandListener);
	}
	private OnGroupExpandListener mOnGroupExpandListener = new OnGroupExpandListener() {
		@Override
		public void onGroupExpand(int groupPosition) {
			Log.d(TAG, "onGroupExpand-"+groupPosition);
			for(int i=0;i<mExpandableListView.getChildCount();i++){
				if(i != groupPosition){
					mExpandableListView.collapseGroup(i);
				}
			}
		}
	};
	private OnGroupClickListener mOnGroupClickListener = new OnGroupClickListener() {
		@Override
		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {
			Log.d(TAG, "onGroupClick-"+groupPosition);
			if(!parent.isGroupExpanded(groupPosition)){
				parent.expandGroup(groupPosition);
			}else{
				parent.collapseGroup(groupPosition);
			}
			return true;
		}
	};
	private OnChildClickListener mOnChildClickListener = new OnChildClickListener() {
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			Log.d(TAG, groupPosition+"-"+childPosition);
			Intent intent = new Intent(ExpandableListViewDemo.this, WebViewActivity.class);
			intent.putExtra("filename", parent.getItemAtPosition(groupPosition+childPosition+1).toString());
			CharSequence title = null;
			try {
				title = ExpandableListViewDemo.this.getString((Integer) R.string.class.getField("filename"
							+parent.getItemAtPosition(groupPosition+childPosition+1).toString().replace(".", "_"))
							.get(R.string.class.newInstance()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			intent.putExtra("title", title);
			startActivity(intent);
			return true;
		}
	};
	private ExpandableListAdapter mExpandableListAdapter = new BaseExpandableListAdapter() {
		private String[] chineseFont = {"一、","二、","三、"};
		private String[] types = {"1.2.html","1.3.html","1.7.html"};
		private String[][] jobs = {{"1.2.1.html","1.2.2.html","1.2.3.html","1.2.4.html","1.2.5.html"},
				{"1.3.1.html","1.3.2.html","1.3.3.html","1.3.4.html"},
				{"1.7.1.html","1.7.2.html","1.7.3.html","1.7.4.html"}};
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		@Override
		public boolean hasStableIds() {
			return true;
		}
		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			Log.d(TAG, "getGroupView-"+groupPosition+"-"+isExpanded);
			LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			TextView tv = new TextView(ExpandableListViewDemo.this);
			tv.setLayoutParams(lp);
			tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			tv.setPadding(80, 20, 0, 20);
			tv.setTextSize(22);
			tv.setTextColor(Color.WHITE);
			CharSequence title = null;
			try {
				title = chineseFont[groupPosition]+ExpandableListViewDemo.this.getString((Integer) R.string.class.getField("filename"
							+types[groupPosition].replace(".", "_"))
							.get(R.string.class.newInstance()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			tv.setText(title);
			if(isExpanded){
				tv.setBackgroundColor(getResources().getColor(R.color.title_bg_new));
			}else{
				tv.setBackgroundColor(Color.TRANSPARENT);
			}
			return tv;
		}
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}
		@Override
		public int getGroupCount() {
			return types.length;
		}
		@Override
		public Object getGroup(int groupPosition) {
			return types[groupPosition];
		}
		@Override
		public int getChildrenCount(int groupPosition) {
			return jobs[groupPosition].length;
		}
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			TextView tv = new TextView(ExpandableListViewDemo.this);
			tv.setLayoutParams(lp);
			tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			tv.setPadding(110, 15, 0, 15);
			tv.setTextSize(18);
			tv.setTextColor(Color.WHITE);
			CharSequence title = null;
			try {
				title = (childPosition+1)+"、"+ExpandableListViewDemo.this.getString((Integer) R.string.class.getField("filename"
							+jobs[groupPosition][childPosition].replace(".", "_"))
							.get(R.string.class.newInstance()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			tv.setText(title);
			tv.setTextColor(Color.WHITE);
			return tv;
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return jobs[groupPosition][childPosition];
		}
	};
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值