//java 程序 package com.spaceflight.propertymanage.activities;
import android.os.Bundle; import android.support.annotation.Nullable; import android.view.View; import android.widget.ListView; import android.widget.Toast;
import com.spaceflight.propertymanage.R; import com.spaceflight.propertymanage.adapter.CommonAdapter; import com.spaceflight.propertymanage.base.BaseActivity; import com.spaceflight.propertymanage.holder.ViewHolder;
import java.util.ArrayList; import java.util.HashMap;
/**
public class CheckManageActivity extends BaseActivity implements View.OnClickListener {
private ListView mListView;
private ArrayList<HashMap<String, String>> mList;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_manage);
addTestData();
initView();
}
private void addTestData() {
mList = new ArrayList<>();
HashMap<String, String> map = new HashMap<>();
map.put("name", "核查");
map.put("date", "2017-10-12");
map.put("type", "test");
mList.add(map);
HashMap<String, String> map1 = new HashMap<>();
map1.put("name", "核查01");
map1.put("date", "2017-10-13");
map1.put("type", "test");
mList.add(map1);
HashMap<String, String> map2 = new HashMap<>();
map2.put("name", "核查02");
map2.put("date", "2017-10-14");
map2.put("type", "test");
mList.add(map2);
HashMap<String, String> map3 = new HashMap<>();
map3.put("name", "核查03");
map3.put("date", "2017-10-30");
map3.put("type", "test");
mList.add(map3);
}
private void initView() {
CommonAdapter commonAdapter = new CommonAdapter(this, mList, R.layout.item_check_manage) {
@Override
public void convert(ViewHolder holder, int position) {
HashMap<String, String> map = mList.get(position);
holder.getView(R.id.tv_check_manage_complete).setOnClickListener(CheckManageActivity.this);
holder.getView(R.id.tv_check_manage_modify).setOnClickListener(CheckManageActivity.this);
holder.setText(R.id.tv_check_manage_name, map.get("name"));
holder.setText(R.id.tv_check_manage_type, map.get("type"));
holder.setText(R.id.tv_check_manage_date, map.get("date"));
}
};
mListView = findView(R.id.lv_check_manage);
mListView.setAdapter(commonAdapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_check_manage_complete:
Toast.makeText(this, "complete", Toast.LENGTH_SHORT).show();
break;
case R.id.tv_check_manage_modify:
Toast.makeText(this, "modify", Toast.LENGTH_SHORT).show();
break;
}
}
}
//适配器 CommenAdaper
package com.spaceflight.propertymanage.adapter;
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter;
import com.spaceflight.propertymanage.holder.ViewHolder;
import java.util.List;
/**
public abstract class CommonAdapter
protected Context mContext;
protected List mData;
protected int mLayoutId;
public CommonAdapter(Context context, List data, int layoutId) {
mContext = context;
mData = data;
mLayoutId = layoutId;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public T getItem(int i) {
return mData.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.getHolder(mContext, convertView, mLayoutId, parent, position);
convert(holder, position);
return holder.getConvertView();
}
/**
* get holder convert
*/
public abstract void convert(ViewHolder holder, int position);
}
package com.spaceflight.propertymanage.holder;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 公共 ViewHolder
* Created by rootBing on 2017/11/30 0030.
*/
public class ViewHolder {
private SparseArray mViews;
private Context mContext;
private View mConvertView;
private int mPosition;
/**
* init holder
*/
public ViewHolder(Context context, int layoutId, ViewGroup parent, int position) {
mConvertView = LayoutInflater.from(context).inflate(layoutId,parent,false);
mViews = new SparseArray<>();
mPosition = position;
mConvertView.setTag(this);
}
/**
* 获取viewHolder
*/
public static ViewHolder getHolder(Context context, View convertView,
int layoutId, ViewGroup parent, int position) {
if(convertView == null){
return new ViewHolder(context,layoutId,parent,position);
}else{
ViewHolder holder = (ViewHolder)convertView.getTag();
holder.mPosition = position;
return holder;
}
}
public View getConvertView(){
return mConvertView;
}
/**
* get view
*/
public T getView(int viewId){
View view = mViews.get(viewId);
if(view == null){
view = mConvertView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T)view;
}
/**
* set text
*/
public ViewHolder setText(int viewId, String text){
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
/**
* set image res
*/
public ViewHolder setImageResource(int viewId,int resId){
ImageView iv = getView(viewId);
iv.setImageResource(resId);
return this;
}
/**
* set image bitmap
*/
public ViewHolder setImageBitmap(int viewId,Bitmap bitmap){
ImageView iv = getView(viewId);
iv.setImageBitmap(bitmap);
return this;
}
}