ViewStub用于ListView和GirdView

     ViewStub是一个轻量级的View控件,懒View,想用、想显示出来的时候就占用内存,用不到的时候就是不存在,不会加载到内存中去,要是在大量的ListView和GridView中能用到ViewStub,相比include就可以大大减少内存占用。

    但是在ListView和GridView中使用容易出错,要了解ViewStub的特性才能把它用好。下面讲一个Demo:

    ViewStub只能被infalte一次,一旦构建后就变成view了,由于listview和gridview的复用原因,一个item会被多次的getView,如果你在getView中infalte,那么滚动listview或者gridview时由于Viewstub被多次infalte就会crash。

package com.michael.viewstubdemo;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
/**
 * ViewStub 也是个View,但是懒加载,可以提前加入到 xml的布局文件中,但是不会加载,它的默认情况下是 View.GONE 当你把他设置成 View.Visable它会调用自己的inflate()方法
 * 这时候才 真正去构建自己,注意它的inflate()方法只能被调用一次!;所以为了提高性能,有个元素不需要一开始就加载可以考虑用 ViewStub,此外还有一个merg元素也是懒加载。
 * @author danxingxi  [email protected]
 */
public class MainAdapter extends BaseAdapter {
	private List<Map<String, String>> data;
	private LayoutInflater layoutInflater;

	public MainAdapter(Context context, List<Map<String, String>> data) {
		this.data = data;
		layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	class ViewHolder {
		RelativeLayout rl_list_item;// 点击展开或收起ViewStub
		
		View viewStub;// 点击后显示的更多的操作   --注意这里用View作为数据类型,而不用ViewStub的用意!
		ImageButton ibCompelete;
		ImageButton ibImportant;
		ImageButton ibNotify;
		ImageButton ibLove;
		ImageButton ibShare;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
//			convertView = layoutInflater.inflate(R.layout.list_item_main_task,null);
			convertView = View.inflate(parent.getContext(),R.layout.list_item_main_task,null);
			viewHolder.rl_list_item = (RelativeLayout) convertView.findViewById(R.id.rl_list_item);
			viewHolder.viewStub = (ViewStub) convertView.findViewById(R.id.vs_detail_operations);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.viewStub.setVisibility(View.GONE);
		}

		viewHolder.rl_list_item.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (viewHolder.viewStub.getVisibility() == View.GONE) { //ViewStub初始状态时Gone
					
					if(viewHolder.viewStub instanceof ViewStub){//ViewStub只能被infalte一次,一旦构建后就变成view了。
						viewHolder.viewStub = ((ViewStub)viewHolder.viewStub).inflate();
					}
					viewHolder.viewStub.setVisibility(View.VISIBLE);
					
				} else {
					viewHolder.viewStub.setVisibility(View.GONE);
				}
			}
		});

		return convertView;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}


你可能感兴趣的:(ListView,ViewStub)