Android中ListView实现分类二级下拉菜单的效果

原创 2015年07月09日 15:47:09

在安卓中有一个ExpandableListView类,通过它我们能够实现listview的分发二级下拉菜单效果,





首先我们需要在布局文件声明它


<pre name="code" class="html"><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=".MainActivity">
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/channel_expandablelistview_bg"
    android:cacheColorHint="#00000000"
    android:divider="#ebebeb"
    android:dividerHeight="1dp"
    android:footerDividersEnabled="false"
    android:groupIndicator="@null" />
</RelativeLayout>






然后在activity中初始化它

public class MainActivity extends Activity {
ExpandableListView mExpandableListView;
ExpandableListViewAdapter mExpandableListViewAdapter;
private static final String TAG = "Main";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fdgf);
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
mExpandableListViewAdapter = new ExpandableListViewAdapter(this);
           
mExpandableListView.setAdapter(mExpandableListViewAdapter);   //设置它的adapter
                mExpandableListView.expandGroup(0);   //默认打开第一条item

                //设置父item的点击事件
mExpandableListView
.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent,
View v, int groupPosition, long id) {
return false;
}
});

                //设置子item的点击事件
mExpandableListView
.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent,
View v, int groupPosition, int childPosition,
long id) {
Log.e(TAG, "groupPosition=" + groupPosition
+ ",childPosition=" + childPosition);
return false;
}
});

}


}




首先来定义几个布局文件


channel_expandablelistview.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:padding="10dip" >


    <View
        android:id="@+id/channel_line"
        android:layout_width="3dp"
        android:layout_height="15dp"
        android:layout_marginLeft="5dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="#79BEF7" />


    <TextView
        android:id="@+id/channel_group_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/channel_line"
        android:textColor="#8e8e8e"
        android:textSize="18sp" />


    <ImageView
        android:id="@+id/channel_imageview_orientation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:src="@drawable/channel_expandablelistview_bottom_icon" />


</RelativeLayout>





channel_expandablelistview_item.xml文件


<?xml version="1.0" encoding="utf-8"?>
<com.gdwanlian.pullscrolldemo.list.CustomGridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/channel_item_child_gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0"
    android:numColumns="4" >


</com.gdwanlian.pullscrolldemo.list.CustomGridView>






channel_gridview_item.xml文件


<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/channel_gridview_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#f0f0f0"
    android:paddingBottom="8dip"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:paddingTop="8dip"
    android:textColor="@color/channel_gridview_item_click"
    android:textSize="15sp" />




在adapter中我们需要做的事就是对我们要显示的内容进行布局和初始化

/**
 * 自定义Adapter
 * 
 * @author zihao
 * 
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
public String[] group = { "测试1", "测试2", "测试3", "测试4", "测试5", "测试6", "测试7", "测试8" };
public String[][] gridViewChild = {
{ "测试1", "测试1", "测试1", "测试1", "测试1", "测试1", "测试1", "测试1" },
{ "测试2", "测试2", "测试2", "测试2", "测试2" },
{ "测试3", "测试3", "测试3", "测试3", "测试3", "测试3", "测试3" },
{ "测试4", "测试4", "测试4", "测试4", "测试4" },
{ "测试5", "测试5", "测试5", "测试5", "测试5", "测试5" },
{ "测试6", "测试6", "测试6", "测试6", "测试6" },
{ "测试7", "测试7", "测试7", "测试7", "测试7", "测试7" },
{ "测试8", "测试8" } };
String[][] child = { { "" }, { "" }, { "" }, { "" }, { "" }, { "" },
{ "" }, { "" } };
LayoutInflater mInflater;
Context context;


public ExpandableListViewAdapter(Context context) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
this.context = context;
}


@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return child[groupPosition][childPosition];
}


@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
mViewChild = new ViewChild();
convertView = mInflater.inflate(
R.layout.channel_expandablelistview_item, null);
mViewChild.gridView = (GridView) convertView
.findViewById(R.id.channel_item_child_gridView);
convertView.setTag(mViewChild);
} else {
mViewChild = (ViewChild) convertView.getTag();
}


SimpleAdapter mSimpleAdapter = new SimpleAdapter(context,
setGridViewData(gridViewChild[groupPosition]),
R.layout.channel_gridview_item,
new String[] { "channel_gridview_item" },
new int[] { R.id.channel_gridview_item });
mViewChild.gridView.setAdapter(mSimpleAdapter);
setGridViewListener(mViewChild.gridView);
mViewChild.gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
return convertView;
}


/**
* 设置gridview点击事件监听
* 
* @param gridView
*/
private void setGridViewListener(final GridView gridView) {
gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (view instanceof TextView) {
// 如果想要获取到哪一行,则自定义gridview的adapter,item设置2个textview一个隐藏设置id,显示哪一行
TextView tv = (TextView) view;
Toast.makeText(context,
"position=" + position + "||" + tv.getText(),
Toast.LENGTH_SHORT).show();
Log.e("hefeng", "gridView listaner position=" + position
+ "||text=" + tv.getText());
}
}
});
}


/**
* 设置gridview数据
* 
* @param data
* @return
*/
private ArrayList<HashMap<String, Object>> setGridViewData(String[] data) {
ArrayList<HashMap<String, Object>> gridItem = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < data.length; i++) {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("channel_gridview_item", data[i]);
gridItem.add(hashMap);
}
return gridItem;
}


@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return child[groupPosition].length;
}


@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return group[groupPosition];
}


@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group.length;
}


@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
mViewChild = new ViewChild();
convertView = mInflater.inflate(
R.layout.channel_expandablelistview, null);
mViewChild.textView = (TextView) convertView
.findViewById(R.id.channel_group_name);
mViewChild.imageView = (ImageView) convertView
.findViewById(R.id.channel_imageview_orientation);
convertView.setTag(mViewChild);
} else {
mViewChild = (ViewChild) convertView.getTag();
}


if (isExpanded) {
mViewChild.imageView
.setImageResource(R.drawable.channel_expandablelistview_top_icon);
} else {
mViewChild.imageView
.setImageResource(R.drawable.channel_expandablelistview_bottom_icon);
}
mViewChild.textView.setText(getGroup(groupPosition).toString());
return convertView;
}


@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}


@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}


ViewChild mViewChild;


static class ViewChild {
ImageView imageView;
TextView textView;
GridView gridView;
}
}




源码地址http://download.csdn.net/detail/ganziqian/8884263



ListView多级子菜单的实现

在网上看了很多关于多级子菜单的实现方式,有自定义ListView实现的,还有一些使用的PopWindow实现的,总得来说可能对于刚刚上手Android开发的人来说可能看这些有些不明白,下面我将使用传统...

Android二级分类列表ListView

1.atypackage com.louis.classifymenuview;import android.os.Bundle; import android.support.v7.app.AppC...

Android RecyclerView 二级列表实现

Android RecyclerView 二级列表实现 简述 实现基础 实现思路 实现过程 二级列表数据格式 RecyclerView Item状态 ViewHolder 其它属性和方法 getIte...
  • yuhys
  • yuhys
  • 2017年04月18日 15:05
  • 5196

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Android——Android实现多级菜单联动操作

感觉好久没有写博客了,这段时间工作很忙,只有晚上才有时间写,demo做出来后马上动手分享给大家了,其实这几天还是很累的,这身体吃不消了【大哭】。。。额!好了,今天是分享一个android实现三级菜单联...

CategoryFragment中一级分类、二级分类的实现

到目前为止,已经实现了HomeFragment和HotFragment的大部分功能,剩下的点击后进入具体栏目的功能后面会具体实现。现在来实现CategoryFragment的界面,CategoryFr...

android两级分类列表且子列表item不同布局实现

想必大家开发过程中一定碰到过这样的界面 上面这个图, 看到后分析一下,  最上面是一个滚动轮播图, 肯定滑动的时候是要跟着可以滑动的,这个应该就是一个headview  下面这部分 ,要求是这样的...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

ExpandListview应用(实现二级列表和实现新闻分类)

ExpandListView在我们开发中使用非常常见,但原生的ExpandListView往往会达不到我们使用的效果,我们会以两个例子为例来理解ExpandListView,现在我们看下如何做呢? 实...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android中ListView实现分类二级下拉菜单的效果
举报原因:
原因补充:

(最多只允许输入30个字)