作者:mobiledu2502885517 | 来源:互联网 | 2023-09-24 15:20
在安卓中有一个ExpandableListView类,通过它我们能够实现listview的分发二级下拉菜单效果,
首先我们需要在布局文件声明它
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:cOntext=".MainActivity">
android:id="@+id/expandableListView"
android:layout_
android:layout_
android:background="@drawable/channel_expandablelistview_bg"
android:cacheColorHint="#00000000"
android:divider="#ebebeb"
android:divider
android:footerDividersEnabled="false"
android:groupIndicator="@null" />
然后在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文件
android:layout_
android:layout_
android:background="#ffffff"
android:orientation="horizontal"
android:padding="10dip" >
android:id="@+id/channel_line"
android:layout_
android:layout_
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#79BEF7" />
android:id="@+id/channel_group_name"
android:layout_
android:layout_
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/channel_line"
android:textColor="#8e8e8e"
android:textSize="18sp" />
android:id="@+id/channel_imageview_orientation"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:src="@drawable/channel_expandablelistview_bottom_icon" />
channel_expandablelistview_item.xml文件
android:id="@+id/channel_item_child_gridView"
android:layout_
android:layout_
android:background="#f0f0f0"
android:numColumns="4" >
channel_gridview_item.xml文件
android:id="@+id/channel_gridview_item"
android:layout_
android:layout_
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> setGridViewData(String[] data) {
ArrayList> gridItem = new ArrayList>();
for (int i = 0; i HashMap hashMap = new HashMap();
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