Kushal
Kushal

Reputation: 8508

Listview rows animating multiple times with Fade-in animation

I am working with Fade In Animation in ListView

But I am facing strange error :

Some row are animating again but getView() is not called for them.. How is this possible ?

enter image description here

My custom Adapter is as follows : MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {

    private Context mContext;
    private Animation mAnim;
    private String[] mData;
    private ListView mList;


    public MyAdapter(Context context, int resource, String[] objects, ListView mList) {
        super(context, resource, objects);
        this.mContext = context;
        mAnim = AnimationUtils.loadAnimation(context, R.anim.fade_in);
        this.mData = objects;
        this.mList = mList;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);

        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
        } else {
            mHolder = (ViewHolder) convertView.getTag();

        }
        view.setTag(mHolder);

        mHolder.mTextview = (TextView) view.findViewById(android.R.id.text1);

        mHolder.mTextview.setText(mData[position]);

        if (mList.getFirstVisiblePosition() < position && position <= (mList.getFirstVisiblePosition() + mList.getChildCount())) {
            Log.w("list", "scroll down..");
            view.startAnimation(mAnim);

        } else if (position < mList.getFirstVisiblePosition()) {
            Log.w("list", "scroll up..");
            view.startAnimation(mAnim);
        }


        return view;
    }

    static class ViewHolder {
        private TextView mTextview;
    }
}

Any help is greatly appriciated..

Thank you

Upvotes: 1

Views: 952

Answers (2)

Ram Mandal
Ram Mandal

Reputation: 1959

Try doing this.

ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.ALPHA,0,1);
    animator.setDuration(1000);
    animator.start();

Hope this help YOu

Upvotes: 2

Kushal
Kushal

Reputation: 8508

@Ram Mondal answer is absolutely correct..

But I also tried with using ViewPropertyAnimation and it worked :

 view.setAlpha(0);   // Initially setting alpha to 0.0 value
    ViewHolder mHolder;
    if (convertView == null) {
        mHolder = new ViewHolder();
    } else {
        mHolder = (ViewHolder) convertView.getTag();
    }
    view.setTag(mHolder);
    mHolder.mTextview = (TextView) view.findViewById(android.R.id.text1);
    mHolder.mTextview.setText(mData[position]);

    // this was trick : this worked very correctly
    view.animate().setDuration(500).alpha(1).withEndAction(new Runnable() {
        @Override
        public void run() {
            // any work to do after animation completed
        }
    });

Thanks to Google IO ListView Animation Video DevBytes : Video and Thanks to Chat Hasse sir for sharing wonderful knoledge..

Upvotes: 1

Related Questions