正文  UI设计 > ListView >

Android_ExpandableListView_子item响应点击事件

 1.满足条件 如果使用ExpandableListView,需要子item响应一个事件,比如重新启动一个新的activity,需要满足下面的条件: (1).修改Adapter返回值 ...

 

1.满足条件

如果使用ExpandableListView,需要子item响应一个事件,比如重新启动一个新的activity,需要满足下面的条件:

(1).修改Adapter返回值

覆写BaseExpandableListAdapter的isChildSelectable()的返回值为true;

(2).绑定监听器

调用ExpandableListView对象的setOnChildClickListener()方法,为其绑定监听器

2.示意图

\\

3.代码实现

 

public class MovieAdapter extends BaseExpandableListAdapter {
	private List list;
	private LayoutInflater inflater;

	public MovieAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		list = new ArrayList();
	}

	public void addList(List list) {
		this.list.addAll(list);
	}
	public List getList() {
		return list;
	}
	private class ViewHolder {

		TextView tvDay;
		TextView tvMonth;
		View vLine;
		ImageView ivPic;
		TextView tvTitle;
		TextView tvType;
		TextView tvDirector;
		/** 预告片 */
		Button btForenotice;
		/** 提醒 */

		public ViewHolder(View view) {
			tvDay = (TextView) view.findViewById(R.id.tv_incoming_day);
			vLine = view.findViewById(R.id.left_green_line);
			tvMonth = (TextView) view.findViewById(R.id.tv_month);
			ivPic = (ImageView) view.findViewById(R.id.iv_incoming_movie);
			tvTitle = (TextView) view.findViewById(R.id.tv_incoming_title);
			tvType = (TextView) view.findViewById(R.id.tv_incoming_movieType);
			tvDirector = (TextView) view.findViewById(R.id.tv_incoming_director);
			btForenotice = (Button) view.findViewById(R.id.bt_incoming_forenotice);
		}
	}

	private class ChildViewHolder {

		ImageView ivPic;
		TextView tvTitle;

		public ChildViewHolder(View view) {
			ivPic = (ImageView) view.findViewById(R.id.pic);
			tvTitle = (TextView) view.findViewById(R.id.title);
		}
	}

	@Override
	public int getGroupCount() {
		return list.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return list.get(groupPosition).videos.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return list.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return list.get(groupPosition).videos.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@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.item_incoming_movies, null);
			ViewHolder holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}
		ViewHolder holder = (ViewHolder) convertView.getTag();

		IncomingBean bean = list.get(groupPosition);
		if (bean.isDay) {
			holder.tvDay.setText(bean.day);
			holder.tvDay.setVisibility(View.VISIBLE);
		} else {
			holder.tvDay.setVisibility(View.GONE);
		}
		if (bean.isMonth) {
			holder.tvMonth.setText(bean.month + 月);
			holder.tvMonth.setVisibility(View.VISIBLE);
			holder.vLine.setVisibility(View.VISIBLE);
		} else {
			holder.tvMonth.setVisibility(View.GONE);
			holder.vLine.setVisibility(View.GONE);
		}
		holder.tvTitle.setText(bean.title);

		StringBuilder sb = new StringBuilder();
		if (bean.wantedCount > 0) {
			sb.append(bean.wantedCount).append(人想看-);
		}
		sb.append(bean.type);
		holder.tvType.setText(sb.toString());
		holder.tvDirector.setText(导演: + bean.director);
		holder.btForenotice.setEnabled(bean.isVideo);

		holder.ivPic.setTag(bean.image);// 防止错位
		MyVolley.getImageLoader().get(bean.image,
				ImageLoader.getImageListener(holder.ivPic, R.drawable.defaultposter_local, 0));

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_movies_child, null);
			ChildViewHolder holder = new ChildViewHolder(convertView);
			convertView.setTag(holder);
		}
		ChildViewHolder holder = (ChildViewHolder) convertView.getTag();

		VideoBean bean = list.get(groupPosition).videos.get(childPosition);
		holder.tvTitle.setText(bean.title);

		holder.ivPic.setTag(bean.image);// 防止错位
		MyVolley.getImageLoader().get(bean.image,
				ImageLoader.getImageListener(holder.ivPic, R.drawable.defaultposter_local, 0));
		return convertView;
	}

	/**ExpandableListView 如果子条目需要响应click事件,必需返回true*/
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
listView.setOnChildClickListener(new OnChildClickListener() {
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Intent intent = new Intent(getActivity(), PlayActivity.class);
		intent.setData(Uri.parse(adapter.getList().get(groupPosition).videos.get(childPosition).url));
		startActivity(intent);
		return true;
	}
});