com.timecontents.smartnotice.animate.AnimatedListViewAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.timecontents.smartnotice.animate.AnimatedListViewAdapter.java

Source

/*
 * Copyright (c) 2013 Kyle W. Banks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.timecontents.smartnotice.animate;

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

import org.json.simple.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.timecontents.smartnotice.*;
import com.timecontents.smartnotice.bean.ViewHolder;
import com.timecontents.smartnotice.view.LinkEllipseTextView;

/**
 * Manages a List of objects to display in the AnimatedListView. When a new view needs to be displayed, it will
 * request that the AnimatedListViewObjectMapper populate the view, and then animated it.
 */
public class AnimatedListViewAdapter extends ArrayAdapter<JSONObject> {

    private LayoutInflater _inflater;

    private int _resourceId;
    private List<JSONObject> _objects;
    private AnimatedListViewObjectMapper _objectMapper;

    ArrayList<JSONObject> _jsonArray;

    private int lastPosition = -1;

    public AnimatedListViewAdapter(Context context, int resourceId, List<JSONObject> objects,
            AnimatedListViewObjectMapper objectMapper) {
        super(context, resourceId, objects);

        this._inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this._resourceId = resourceId;
        this._objects = objects;
        this._objectMapper = objectMapper;

        _jsonArray = (ArrayList<JSONObject>) objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position > _objects.size()) {
            return null;
        }

        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            v = _inflater.inflate(_resourceId, parent, false);

            holder = new ViewHolder();
            holder.tvTitle = (TextView) v.findViewById(R.id.tv_notice_title);
            holder.tvDate = (TextView) v.findViewById(R.id.tv_notice_date);
            holder.tvContent = (LinkEllipseTextView) v.findViewById(R.id.tv_notice_content_ellipse);
            holder.imgContent = (ImageView) v.findViewById(R.id.img_notice_content);
            holder.imgFileIcon = (ImageView) v.findViewById(R.id.img_notice_file_icon);
            holder.imgType = (ImageView) v.findViewById(R.id.img_notice_type_icon); // ? ?  ?

            v.setTag(holder);
        } else
            holder = (ViewHolder) v.getTag();

        JSONObject object = _objects.get(position);
        _objectMapper.bindObjectToView(object, holder, v);

        if (v != null) {
            Animation animation = new TranslateAnimation(0, 0, (position > lastPosition) ? 100 : -100, 0);
            animation.setDuration(400);
            v.startAnimation(animation);
        }
        lastPosition = position;

        return v;
    }

}