Android 中比较好的adapter的写法

package com.cqotc.zlt.adapter;

 

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

 

import com.ab.util.AbDateUtil;

import com.cqotc.zlt.R;

import com.cqotc.zlt.bean.EventType;

import com.cqotc.zlt.bean.NoticeBean;

import com.cqotc.zlt.http.NHIL;

import com.cqotc.zlt.http.StringHttpResponseListener;

import com.cqotc.zlt.utils.image.ImageUtil;

import com.cqotc.zlt.view.ScaleImageView;

 

import java.util.List;

 

import de.greenrobot.event.EventBus;

 

/**

* Created by Administrator on 2016/3/28.

*/

public class ActiveMessageAdapter extends BaseAdapter {

private Context mContext;

private List mBeans = null;

private OnClickListener mOnClickListener;

 

public ActiveMessageAdapter(Context mContext) {

this.mContext = mContext;

}

 

public void setData(List data) {

mBeans = data;

notifyDataSetChanged();

}

 

public void setListener(OnClickListener listener) {

mOnClickListener = listener;

}

 

@Override

public int getCount() {

if (mBeans != null) {

return mBeans.size();

}

return 0;

}

 

@Override

public NoticeBean getItem(int position) {

return mBeans.get(position);

}

 

@Override

public long getItemId(int position) {

return position;

}

 

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder = null;

final NoticeBean bean = mBeans.get(position);

if (convertView == null) {

convertView = LayoutInflater.from(mContext).inflate(R.layout.item_active_notice, null);

viewHolder = new ViewHolder(convertView);

convertView.setTag(viewHolder);

} else {

viewHolder = (ViewHolder) convertView.getTag();

}

viewHolder.idTitleTv.setText(bean.getTitle());

viewHolder.idDescribeTv.setText(bean.getDescription());

viewHolder.idTimeTv.setText(AbDateUtil.getStringByFormat(bean.getPublishTime(), AbDateUtil.dateFormatYMD));

ImageUtil.display(mContext, bean.getHeadImg(), viewHolder.idImageview);

 

if(bean.isIsRead()) {

viewHolder.viewUnreadSpace.setVisibility(View.GONE);

viewHolder.idUnread.setVisibility(View.GONE);

} else {

viewHolder.viewUnreadSpace.setVisibility(View.VISIBLE);

viewHolder.idUnread.setVisibility(View.VISIBLE);

}

 

convertView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (!bean.isIsRead()) {

setItemIsReaded(bean);

}

if (mOnClickListener != null) {

mOnClickListener.clickItem(bean);

}

}

});

return convertView;

}

 

private void setItemIsReaded(final NoticeBean noticeBean) {

NHIL.setNoticeRead(mContext, noticeBean.getNoticeCode(), new StringHttpResponseListener() {

@Override

public void onSuccess(int statusCode, String content) {

noticeBean.setIsRead(true);

notifyDataSetChanged();

EventBus.getDefault().post(EventType.MESSAGE_COUNT);

}

 

@Override

public void onFailure(int statusCode, String content) {

 

}

 

});

}

 

public interface OnClickListener {

void clickItem(NoticeBean noticeBean);

}

 

static class ViewHolder {

protected View viewUnreadSpace;

protected TextView idTitleTv;

protected TextView idTimeTv;

protected ScaleImageView idImageview;

protected TextView idDescribeTv;

protected TextView idUnread;

 

ViewHolder(View rootView) {

initView(rootView);

}

 

private void initView(View rootView) {

viewUnreadSpace = rootView.findViewById(R.id.view_unread_space);

idTitleTv = (TextView) rootView.findViewById(R.id.id_title_tv);

idTimeTv = (TextView) rootView.findViewById(R.id.id_time_tv);

idImageview = (ScaleImageView) rootView.findViewById(R.id.id_imageview);

idDescribeTv = (TextView) rootView.findViewById(R.id.id_describe_tv);

idUnread = (TextView) rootView.findViewById(R.id.id_unread);

}

}

}

你可能感兴趣的:(Android)