package com.terton.android.culture.subscription; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper; import com.terton.android.culture.R; import com.terton.android.culture.modal.Article; import java.util.ArrayList; import java.util.List; public class Dynamic2Adapter extends RecyclerView.Adapter<Dynamic2Adapter.ViewHolder> implements View.OnClickListener { private LayoutInflater layoutInflater; private List<Article> _items; public Dynamic2Adapter(Context context) { this(context, null); } public Dynamic2Adapter(Context context, List<Article> items) { layoutInflater = LayoutInflater.from(context); _items = items; if (_items == null) { _items = new ArrayList<Article>(); } } public List<Article> getItems() { if (_items == null) { _items = new ArrayList<Article>(); } return _items; } public void setItems(List<Article> items) { this._items = items; } @Override public Dynamic2Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_dynamic, viewGroup, false); ViewHolder vh = new ViewHolder(view); view.setOnClickListener(this); return vh; } @Override public void onBindViewHolder(Dynamic2Adapter.ViewHolder holder, int position) { Article article = (Article) getItem(position); if (article != null) { holder.itemView.setTag(article); String picUrl = article.getPicUrl(); if (picUrl != null) { UrlImageViewHelper.setUrlDrawable(holder.dynamic_image, picUrl); holder.dynamic_pic.setVisibility(View.VISIBLE); } else { holder.dynamic_pic.setVisibility(View.GONE); } holder.dynamic_title.setText(article.getTitle()); } } @Override public int getItemCount() { return getItems().size(); } public Object getItem(int position) { if (position < getItemCount()) { return getItems().get(position); } return null; } @Override public void onClick(View v) { if (null != mOnItemClickListener) { mOnItemClickListener.onClick(v, (Article) v.getTag()); } } public static class ViewHolder extends RecyclerView.ViewHolder { ImageView dynamic_image; TextView dynamic_title; ImageView dynamic_favorite; RelativeLayout dynamic_pic; public ViewHolder(View itemView) { super(itemView); dynamic_image = (ImageView) itemView.findViewById(R.id.dynamic_image); dynamic_title = (TextView) itemView.findViewById(R.id.dynamic_title); dynamic_favorite = (ImageView) itemView.findViewById(R.id.dynamic_favorite); dynamic_pic = (RelativeLayout) itemView.findViewById(R.id.dynamic_pic); } } private OnRecyclerViewItemClickListener mOnItemClickListener = null; public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { this.mOnItemClickListener = listener; } public static interface OnRecyclerViewItemClickListener { void onClick(View view, Article doc); } }
时间: 2024-12-14 07:06:31