android 自己实现的一个类似expandablelistview的一个控件

因为之前的项目中刚好需要一个控件类似expandablelistview那样的一个分布图,但是搜过网上的一些资料后觉得不是很方便。于是自己用listview跟gridview写了一个。效果的话跟自己之前预期中的还好。先贴代码上去,方便以后自己要是忘记了就不好了


先是布局文件



    
	


其中 在listview里面定义了一个水平方向上只有两个item的girdview。

然后再是实现了其中的view


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CarTypeListView extends ListView implements OnItemClickListener{

	private ListAdapter listAdapter;
	
	private Handler handler;
	
	private static final int WHAT_GETTYPES = 1;
	
	private JSONObject carBrand;
	
	private ICallback girdItemClick;
	
	public void setGirdItemClick(ICallback girdItemCallback){
		this.girdItemClick = girdItemCallback;
	}
	
	public CarTypeListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}
	
	public CarTypeListView(Context context) {
		super(context);
		initView(context);
	}
	private void initView(Context context){
		this.setDividerHeight(1);
		this.setFocusable(false);
		this.setClickable(false);
		LinearLayout emptyView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.widget_list_emptyview, null);
		emptyView.setVisibility(View.GONE);
		this.setEmptyView(emptyView);
		listAdapter = new ListAdapter();
		this.setAdapter(listAdapter);
	}
	public void refreshData(JSONObject carBrand){
		this.listAdapter.setDatas(null);
		this.carBrand = carBrand;
		this.getCarTypes(carBrand.getString("CUID"));
	}
	
	private void getCarTypes(String brandId) {
		Map params = new HashMap();
		params.put("brandId", brandId);
		params.put("emm", LogMgr.getInstance(getContext()).getModelPath("CarTypeListView"));
		HttpReq req = new HttpReq(EnvMgr.getAppCtx()+"/CarAction/getTypes.do?",params);
		req.setWhat(WHAT_GETTYPES);
//		req.setShowMask(true);
		req.setParams(params);
		HttpUtils.get(this.getContext(), req, getMyHandler());
	}
	
	private Handler getMyHandler() {
		if(this.handler == null) {
			this.handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					String rp = msg.getData()
							.getString(HttpUtils.RESPONSE_ROOT);
					JSONObject jo = JSONObject.parseObject(rp);
					switch (msg.what) {
					case WHAT_GETTYPES:
						setTypes(jo);
						break;
					default:
						break;
					}
				}
			};
		}
		return this.handler;
	}
	
	private void setTypes(JSONObject jo) {
		List datas = new ArrayList();
		if (jo.getBooleanValue("success")) {
			JSONArray array = jo.getJSONArray("result");
			Map> dataMap = new HashMap>();
			for(int i = 0;i list = dataMap.get(carBrand);
				if(list == null){
					list = new ArrayList();
					dataMap.put(carBrand,list);
				}
				list.add(object);
			}
			for(String car:dataMap.keySet()){
				JSONObject data = new JSONObject();
				data.put("MANUFACTURER",car);
				List items = dataMap.get(car);
				data.put("content",items);
				datas.add(data);
				}
			listAdapter.setDatas(datas);
		} else {

		}
	}

	private class ListAdapter extends BaseAdapter{

		private List datas;
		
		public void setDatas(List data){
			if(data != null){
				this.datas = data;
			}
			this.notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return datas == null?0:datas.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			GroupHolder holder = null;
			if(convertView == null){
				holder = new GroupHolder();
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.common_car_type, null);
				holder.groupView = (TextView) convertView.findViewById(R.id.car_type_name);
				holder.gridView = (NestGridView) convertView.findViewById(R.id.car_type_content);
				holder.gridView.setTag(holder);
				convertView.setTag(holder);
			}else{
				holder = (GroupHolder) convertView.getTag();
			}
			holder.groupPosition = position;
			JSONObject object = (JSONObject) getItem(position);
			List contents = (List) object.get("content");
			holder.groupView.setText(object.getString("MANUFACTURER"));
			GridAdapter adapter = new GridAdapter(getContext(),contents);
			holder.gridView.setAdapter(adapter);
			holder.gridView.setOnItemClickListener(CarTypeListView.this);
			return convertView;
		}
		
	}
	
	private class GroupHolder{
		int groupPosition;
		TextView groupView;
		NestGridView gridView;
	}
	
	
	private class GridAdapter extends BaseAdapter{
		
		private Context context;
		
		private List contents;
		
		public GridAdapter(Context context,List contents){
			this.context = context;
			this.contents = contents;
			this.notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return contents == null?0:contents.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ChildHolder holder = null;
			if(convertView == null){
				holder = new ChildHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.common_cartype_item, null);
				holder.typeImg = (ImageView) convertView.findViewById(R.id.icon);
				holder.typeName = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			}else{
				holder = (ChildHolder) convertView.getTag();
			}
			JSONObject object = (JSONObject) getItem(position);
			String image = object.getString("IMG");
			if(StringUtils.isNotBlank(image)){
				holder.typeImg.setVisibility(View.VISIBLE);
				ImageReq req = new ImageReq(holder.typeImg,EnvMgr.getImageServerCtx()+image);
				req.setFailedBitmap(R.drawable.ico_err_picture);
				ImageUtils.get(context, req);
			}else{
				holder.typeImg.setVisibility(View.GONE);
			}
			
			holder.typeName.setText(object.getString("LABEL_CN"));
			return convertView;
		}
		
	}
	
	
	private class ChildHolder{
		TextView typeName;
		ImageView typeImg;
	}


	@Override
	public void onItemClick(AdapterView parent, View convertView, int position, long id) {
		JSONObject object = (JSONObject) parent.getAdapter().getItem(position);
		if(object != null && girdItemClick != null){
			this.girdItemClick.onCallback(this,object,carBrand,position);
		}
	}
	
	
}

然后再是girdview中item的布局文件





    

        


这样就大功告成了~ 效果图就是这样子的(ps:为方便别人了解 所以我涂鸦了一下~)



你可能感兴趣的:(android)