ExpandableListView顾名思义既是可扩展的ListView,我们可用来实现菜单设置之类的功能。前些时间做项目用到了,现在给大家分享一下 ExpandableListView的实现方式。
先上效果图:
展开后:
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView android:id="@+id/newsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@color/transparent"
android:drawSelectorOnTop="false"
android:groupIndicator="@null"
>
</ExpandableListView>
</LinearLayout>
ExpandableListView父栏目的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="32dip"
android:background="@drawable/bg_group"
android:padding="5dip"
>
<TextView android:id="@+id/group_text"
android:textSize="22dip"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView android:id="@+id/group_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="3dip"
/>
</LinearLayout>
子栏目的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="5dip"
>
<TextView android:id="@+id/child"
android:layout_marginLeft="10dip"
android:textSize="22dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
下面看主要代码:
package com.cloay.news;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import com.cloay.news.bean.NewsListAdapter;
import com.cloay.news.service.MainService;
/**
* 实现栏目列表界面(使用ExpendListView实现)
* @author Cloay
* 2012-3-3
* 下午09:38:12
*/
public class HomeActivity extends NewsReaderActivity{
private ExpandableListView newsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
MainService.allActivity.add(this);
newsList = (ExpandableListView) findViewById(R.id.newsList);
NewsListAdapter adapter = new NewsListAdapter(HomeActivity.this);
newsList.setAdapter(adapter);
newsList.setOnChildClickListener(new OnChildClickListener() {//为item设置监听注意必须在adapter中 isChildSelectable()return true详见下面代码
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// System.out.println("groupPosition-->:" + groupPosition);
// System.out.println("childPosition-->:" + childPosition);
Intent intent = new Intent(HomeActivity.this, ContentActivity.class);
intent.putExtra("groupPosition", groupPosition);
intent.putExtra("childPosition", childPosition);
HomeActivity.this.startActivity(intent);
return true;
}
});
}
}
为ExpandableListView提供数据的Adapter:
package com.cloay.news.bean;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.cloay.news.R;
import com.cloay.news.constants.Constants;
/**
* ExpandableListView添加数据
* @author Cloay
* 2012-3-4
* 下午08:10:29
*/
public class NewsListAdapter extends BaseExpandableListAdapter{
@SuppressWarnings("rawtypes")
private List<List> childList = new ArrayList<List>();
private LayoutInflater inflater;
public NewsListAdapter(Context context) {
super();
//初始化数据
initData(context);
}
/**
* 为group及child提供数据初始化
* @param context
*/
private void initData(Context context) {
//设置父栏目标题
Constants.group.add(context.getResources().getString(R.string.sina_news));
Constants.group.add(context.getResources().getString(R.string.sina_s));
Constants.group.add(context.getResources().getString(R.string.sina_t));
Constants.group.add(context.getResources().getString(R.string.sina_j));
Constants.group.add(context.getResources().getString(R.string.sina_e));
Constants.group.add(context.getResources().getString(R.string.sina_f));
//每一个子栏目的标题
Constants.child1.add(context.getResources().getString(R.string.sina_china));
Constants.child1.add(context.getResources().getString(R.string.sina_world));
Constants.child2.add(context.getResources().getString(R.string.sina_s_all));
Constants.child2.add(context.getResources().getString(R.string.sina_s_football));
Constants.child2.add(context.getResources().getString(R.string.sina_s_nba));
Constants.child3.add(context.getResources().getString(R.string.sina_t));
Constants.child3.add(context.getResources().getString(R.string.sina_t_internet));
Constants.child3.add(context.getResources().getString(R.string.sina_t_mobile));
Constants.child4.add(context.getResources().getString(R.string.sina_j_all));
Constants.child4.add(context.getResources().getString(R.string.sina_j_china));
Constants.child4.add(context.getResources().getString(R.string.sina_j_world));
Constants.child5.add(context.getResources().getString(R.string.sina_e_all));
Constants.child6.add(context.getResources().getString(R.string.sina_f_w));
Constants.child6.add(context.getResources().getString(R.string.sina_f_m));
childList.add(Constants.child1);
childList.add(Constants.child2);
childList.add(Constants.child3);
childList.add(Constants.child4);
childList.add(Constants.child5);
childList.add(Constants.child6);
inflater = LayoutInflater.from(context);
}
public NewsListAdapter(String[] group){
}
@Override
public int getGroupCount() {
return Constants.group.size(); //父栏目大小
}
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size(); //子栏目数量
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.group, null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.group_image);
//设置展开时的图标
image.setImageResource(R.drawable.mm_submenu_down_normal);
if(!isExpanded){
image.setImageResource(R.drawable.mm_submenu_normal);
}
TextView textView = (TextView) convertView.findViewById(R.id.group_text);
textView.setText(Constants.group.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.child, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.child);
textView.setText((childList.get(groupPosition)).get(childPosition).toString());
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true; //返回true才能点击child
}
}
通过以上代码我们就能实现想要的了有什么问题请留言,大家一起交流学习!
说明:转载请注明出处!