DevStore首页 > DEV资源 > 源码下载 > 源码详情

【Android】实现新闻分类(二级下拉菜单)效果

 
紫豪 | 2014-11-05 16:41    浏览量(17467)    评论(13)    收藏(26)
功能分类:特效 支持平台:Android 运行环境:Eclipse
开发语言:Java 开发工具:Eclipse 源码大小:1011.04KB
下载源码 1514 人下载

源码简介

利用ExpandableListView和GridView,实现类似新闻客户端二级下拉菜单效果。

源码截图

  • 运行效果
DevStore所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与DevStore无关。

源码片段

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
	public String[] group = { "常用", "搞笑", "原创", "资讯", "体育", "游戏", "汽车", "娱乐" };
	public String[][] gridViewChild = {
			{ "明星八卦", "雷人囧事", "网络红人", "科技资讯", "美女花边", "游戏达人", "美女车模", "疯狂恶搞" },
			{ "疯狂恶搞", "搞笑综艺", "原创搞笑", "爆笑宠物", "雷人囧事" },
			{ "原创热点", "原创影视", "音乐动画", "火星搞笑", "校园作品", "网络红人", "拍客" },
			{ "社会资讯", "国内资讯", "国际资讯", "财富资讯", "科技资讯" },
			{ "篮球天地", "足球世界", "综合体育", "极限运动", "武术摔角", "美女花边" },
			{ "网络游戏", "电子竞技", "单机电玩", "游戏达人", "工会战队" },
			{ "新车速递", "车型推荐", "改装酷玩", "汽车广告", "评测报告", "美女车模" },
			{ "明星八卦", "影视资讯" } };
	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 < data.length; 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;
	}
}
  •   赞(3) 赞 +1 赞(3) 已赞
  •   收藏(26) 收藏 +1 已收藏 取消
  •   下载(1514)
评论(13)
lidan950830

lidan950830 2016-05-16 13:19:30

如果要变成多选呢
回复(0) 赞(0) 赞(1)
1348793083

1348793083 2015-11-17 19:30:28

66666666
回复(0) 赞(0) 赞(1)
Darker_IT

Darker_IT 2015-03-27 14:17:33

这个很不错!值得下载
回复(0) 赞(1) 赞(2)
deng8877

deng8877 2015-02-26 15:06:28

学习了。谢谢分享
回复(0) 赞(0) 赞(1)
lionel

lionel 2014-11-23 20:12:32

高人啊,这都行
回复(0) 赞(0) 赞(1)
  • 推荐
  • 审核
  • 收录
  • 获取活力值

源码上传作者

  • 138 能力值
  • 58 源码
  • 10 评测
  • 0 资料

热门源码