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

在安卓中有一个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



  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值