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

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


先是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/car_type_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/theme_h4"
        android:layout_marginLeft="@dimen/dp10"
        android:layout_marginTop="@dimen/dp6"
        android:textColor="@color/theme_font_thin4"/>
	<common.widget.grid.NestGridView 
	    android:id="@+id/car_type_content"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:numColumns="2"
	    />
</LinearLayout>

其中 在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<String, String> params = new HashMap<String, String>();
		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<JSONObject> datas = new ArrayList<JSONObject>();
		if (jo.getBooleanValue("success")) {
			JSONArray array = jo.getJSONArray("result");
			Map<String,List<JSONObject>> dataMap = new HashMap<String,List<JSONObject>>();
			for(int i = 0;i<array.size();i++){
				JSONObject object = array.getJSONObject(i);
				String carBrand = object.getString("MANUFACTURER");
				List<JSONObject> list = dataMap.get(carBrand);
				if(list == null){
					list = new ArrayList<JSONObject>();
					dataMap.put(carBrand,list);
				}
				list.add(object);
			}
			for(String car:dataMap.keySet()){
				JSONObject data = new JSONObject();
				data.put("MANUFACTURER",car);
				List<JSONObject> items = dataMap.get(car);
				data.put("content",items);
				datas.add(data);
				}
			listAdapter.setDatas(datas);
		} else {

		}
	}

	private class ListAdapter extends BaseAdapter{

		private List<JSONObject> datas;
		
		public void setDatas(List<JSONObject> 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<JSONObject> contents = (List<JSONObject>) 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<JSONObject> contents;
		
		public GridAdapter(Context context,List<JSONObject> 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的布局文件



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@drawable/view_theme3"
     >
    <ImageView
            android:id="@+id/icon"
            android:layout_width="@dimen/list_item_pic"
            android:layout_height="@dimen/list_item_pic"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/dp6"
            android:layout_marginRight="@dimen/dp6"
            android:layout_marginTop="@dimen/dp6"
            android:contentDescription="@null"
            android:scaleType="fitCenter"
            android:visibility="gone"
             />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="@dimen/dp8"
            android:paddingRight="@dimen/dp8"
            android:paddingBottom="@dimen/dp8"
            android:singleLine="true"
            android:ellipsize="end"
            android:textColor="@drawable/text_theme1"
            android:textSize="@dimen/theme_h4" />
</LinearLayout>

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值