码迷,mamicode.com
首页 > 移动开发 > 详细

基于android_support_v7_widgeet_RecyclerView的瀑布流效果

时间:2015-05-06 10:47:13      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }
}

 

基于android_support_v7_widgeet_RecyclerView的瀑布流效果

标签:

原文地址:http://www.cnblogs.com/aiyobucuoo/p/4481104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!