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

来源:互联网 发布:网络感情骗术 台湾 编辑:程序博客网 时间:2024/05/21 23:46

因为之前的项目中刚好需要一个控件类似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() {@Overridepublic 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();}@Overridepublic int getCount() {return datas == null?0:datas.size();}@Overridepublic Object getItem(int position) {return datas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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();}@Overridepublic int getCount() {return contents == null?0:contents.size();}@Overridepublic Object getItem(int position) {return contents.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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;}@Overridepublic 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:为方便别人了解 所以我涂鸦了一下~)



0 0
原创粉丝点击