com.zhongsou.souyue.adapter.GalleryNewsPagerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.zhongsou.souyue.adapter.GalleryNewsPagerAdapter.java

Source

/*
 Copyright (c) 2013 Roman Truba
    
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to the following conditions:
    
 The above copyright notice and this permission notice shall be included in all copies or substantial
 portions of the Software.
    
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.zhongsou.souyue.adapter;

import android.animation.Animator;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zhongsou.souyue.R;
import com.zhongsou.souyue.activity.GalleryNewsActivity;
import com.zhongsou.souyue.im.util.MyDisplayImageOption;
import com.zhongsou.souyue.im.util.PhotoUtils;
import com.zhongsou.souyue.module.GalleryNewsItem;
import com.zhongsou.souyue.module.GalleryNewsList;
import com.zhongsou.souyue.module.GalleryRecommendItem;
import com.zhongsou.souyue.ui.gallery.touchview.UrlTouchImageView;
import com.zhongsou.souyue.ui.gallery.touchview.UrlTouchImageView.DownLoadLinstener;

import java.util.ArrayList;
import java.util.List;

/**
 * ?adapter,?
 */
public class GalleryNewsPagerAdapter extends PagerAdapter implements ImageLoadingListener {

    private GalleryNewsList mNewsList;
    private Context mContext;
    private Animator mCurrentAnimator;
    private int mShortAnimationDuration;

    public static final String RELATE_TAG = "relate";

    //adapter
    private final GalleryRecommendAdapter mAdapter;

    /**
     * ???????
     *
     * @param context
     * @param mNewsList
     */
    public GalleryNewsPagerAdapter(Context context, GalleryNewsList mNewsList) {
        this.mContext = context;
        mShortAnimationDuration = mContext.getResources().getInteger(android.R.integer.config_shortAnimTime);
        this.mNewsList = mNewsList;
        mAdapter = new GalleryRecommendAdapter(mContext, mNewsList.getRelate(), (GalleryNewsActivity) mContext);
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        if (RELATE_TAG.equals(getItem(position))) {
            //??
            View lastPage = View.inflate(mContext, R.layout.gallerynews_activity_recommend, null);
            initRecommendView(lastPage);
            collection.addView(lastPage);
            return lastPage;
        } else {
            //???
            UrlTouchImageView iv = new UrlTouchImageView(mContext);
            iv.setDownLoadLinstener((DownLoadLinstener) mContext);
            iv.setBgUrl(mNewsList.getContent().get(position).getUrl());
            collection.addView(iv, 0);
            return iv;
        }
    }

    private void initRecommendView(View lastPage) {
        ListView listView = (ListView) lastPage.findViewById(R.id.gallery_recommend_list);
        listView.setAdapter(mAdapter);
    }

    @Override
    public int getCount() {
        //recommendlist ??+1;
        return mNewsList.getContent().size() + (mNewsList.getRelate() == null ? 0 : 1);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0.equals(arg1);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public Object getItem(int pos) {
        if (pos < mNewsList.getContent().size()) {
            return mNewsList.getContent().get(pos).getUrl();
        }
        return RELATE_TAG;
    }

    /**
     * ???apapter
     */

    private class GalleryRecommendAdapter extends BaseAdapter {

        private final List<GalleryRecommendItem> mList;
        private final Context mContext;
        private final OnItemClickListener mListener;

        public GalleryRecommendAdapter(Context mContext, List<GalleryNewsItem> relate,
                OnItemClickListener listener) {
            this.mList = new ArrayList<GalleryRecommendItem>();
            this.mContext = mContext;
            mListener = listener;
            initmList(relate);
        }

        private void initmList(List<GalleryNewsItem> relate) {
            //relate?5com.zhongsou.souyue.module.GalleryNewsList.getRelate(),
            //
            if (relate == null)
                return;
            //????
            //line 1
            mList.add(new GalleryRecommendItem(GalleryRecommendItem.TYPE_RECOMMENDLIST_DOUBLE, relate.get(0),
                    relate.get(1)));
            //line 2
            mList.add(new GalleryRecommendItem(GalleryRecommendItem.TYPE_RECOMMEND_LIST_SINGLE, relate.get(2)));
            //line 3
            mList.add(new GalleryRecommendItem(GalleryRecommendItem.TYPE_RECOMMENDLIST_DOUBLE, relate.get(3),
                    relate.get(4)));
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * getview ?viewholder.???listview
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            if (mList.get(position).type == GalleryNewsItem.TYPE_RECOMMEND_LIST_SINGLE) {
                view = View.inflate(mContext, R.layout.gallerynews_recommend_item_singleline, null);
            } else {
                view = View.inflate(mContext, R.layout.gallerynews_recommend_item, null);
            }
            setViewData(view, mList.get(position));
            return view;
        }

        /**
         * view?
         *
         * @param convertView
         * @param galleryRecommendInfo
         */
        private void setViewData(View convertView, final GalleryRecommendItem galleryRecommendInfo) {
            //findView
            ImageView iv_recommend_1 = (ImageView) convertView.findViewById(R.id.gallery_recommend_image1);
            TextView tv_recommend_1 = (TextView) convertView.findViewById(R.id.gallery_recommend_text1);
            ImageView iv_recommend_2 = (ImageView) convertView.findViewById(R.id.gallery_recommend_image2);
            TextView tv_recommend_2 = (TextView) convertView.findViewById(R.id.gallery_recommend_text2);
            //set listener
            convertView.findViewById(R.id.gallery_recommend_item_1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(galleryRecommendInfo.items.get(0));
                }
            });
            View rightItem = convertView.findViewById(R.id.gallery_recommend_item_2);
            if (rightItem != null) {
                rightItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClick(galleryRecommendInfo.items.get(1));
                    }
                });
            }
            //set data
            if (galleryRecommendInfo.type == GalleryNewsItem.TYPE_RECOMMEND_LIST_SINGLE) {
                //??
                PhotoUtils.showCard(PhotoUtils.UriType.HTTP, galleryRecommendInfo.items.get(0).getImg(),
                        iv_recommend_1, MyDisplayImageOption.getOptions(R.drawable.pic_default),
                        GalleryNewsPagerAdapter.this);
                tv_recommend_1.setText(galleryRecommendInfo.items.get(0).getTitle());
            } else if (galleryRecommendInfo.type == GalleryNewsItem.TYPE_RECOMMENDLIST_DOUBLE) {
                //??
                //
                PhotoUtils.showCard(PhotoUtils.UriType.HTTP, galleryRecommendInfo.items.get(0).getImg(),
                        iv_recommend_1, MyDisplayImageOption.getOptions(R.drawable.pic_default),
                        GalleryNewsPagerAdapter.this);
                tv_recommend_1.setText(galleryRecommendInfo.items.get(0).getTitle());
                //
                PhotoUtils.showCard(PhotoUtils.UriType.HTTP, galleryRecommendInfo.items.get(1).getImg(),
                        iv_recommend_2, MyDisplayImageOption.getOptions(R.drawable.pic_default),
                        GalleryNewsPagerAdapter.this);
                tv_recommend_2.setText(galleryRecommendInfo.items.get(1).getTitle());
            }
        }

    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
        ImageView v = (ImageView) view;
        v.setScaleType(ImageView.ScaleType.CENTER);
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {

    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        ImageView v = (ImageView) view;
        v.setScaleType(ImageView.ScaleType.CENTER_CROP);
    }

    @Override
    public void onLoadingStarted(String imageUri, View view) {
        ImageView v = (ImageView) view;
        v.setScaleType(ImageView.ScaleType.CENTER);
    }

    /**
     * 
     */
    public interface OnItemClickListener {
        void onItemClick(GalleryNewsItem item);
    }

}