Android Open Source - inside-list-view-talk Inside List View Animated Items Transient






From Project

Back to project page inside-list-view-talk.

License

The source code is released under:

MIT License

If you think the Android project inside-list-view-talk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.flipper.example.insidelist.activity;
// ww w .j av  a2 s .  co m
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import com.flipper.example.insidelist.R;
import com.flipper.example.insidelist.adapter.DebugAdapter;
import com.flipper.example.insidelist.component.DebugListView;
import com.flipper.example.insidelist.model.DataModel;
import com.flipper.example.insidelist.rendermodel.RenderDebug;

import java.util.ArrayList;

/**
 * @author: Fernando Cejas <fcejas@gmail.com>
 * android10.org
 */
public class InsideListViewAnimatedItemsTransient extends InsideBaseListActivity {

    private static final int INIT_VALUES = 100;
    ArrayList<DataModel> listValues = new ArrayList<DataModel>();
    DebugAdapter adapter;
    private DebugListView lv_list;
    private CheckBox cb_vpa;
    private CheckBox cb_setTransientState;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inside_list_transient);

        mapGUI();
    }

    private void mapGUI() {
        cb_vpa = (CheckBox) findViewById(R.id.cb_vpa);
        cb_setTransientState = (CheckBox) findViewById(R.id.cb_setTransientState);

        lv_list = (DebugListView) findViewById(R.id.ll_list);
        initAdapter();

        lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                final DataModel item = (DataModel) parent.getItemAtPosition(position);
                if (cb_vpa.isChecked()) {
                    view.animate().setDuration(1000).alpha(0).
                            withEndAction(new Runnable() {
                                @Override
                                public void run() {
                                    listValues.remove(item);
                                    adapter.notifyDataSetChanged();
                                    view.setAlpha(1);
                                }
                            });
                } else {
                    // Here's where the problem starts - this animation will animate a View object.
                    // But that View may get recycled if it is animated out of the container,
                    // and the animation will continue to fade a view that now contains unrelated
                    // content.
                    ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.ALPHA, 0);
                    anim.setDuration(1000);
                    if (cb_setTransientState.isChecked()) {
                        // Here's the correct way to do this: if you tell a view that it has
                        // transientState, then ListView ill avoid recycling it until the
                        // transientState flag is reset.
                        // A different approach is to use ViewPropertyAnimator, which sets the
                        // transientState flag internally.
                        view.setHasTransientState(true);
                    }
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            listValues.remove(item);
                            adapter.notifyDataSetChanged();
                            view.setAlpha(1);
                            if (cb_setTransientState.isChecked()) {
                                view.setHasTransientState(false);
                            }
                        }
                    });
                    anim.start();
                }
            }

        });
    }

    private void initAdapter() {

        //init avatars(this works because resids are consecutive,
        //not good practices)
        //init values to insert
        for (int i = 0; i < INIT_VALUES; i++) {
            DataModel data = new DataModel();
            data.setAvatar(R.drawable.avatar_1 + i);
            data.setTitle("pos " + i);

            listValues.add(data);
        }

        adapter = new DebugAdapter(getApplicationContext(), listValues);
        adapter.registerRender(DataModel.class, RenderDebug.class);

        adapter.setGeneralInfoListener(this);
        lv_list.setAdapter(adapter);
    }
}




Java Source Code List

com.flipper.example.insidelist.activity.InsideBaseListActivity.java
com.flipper.example.insidelist.activity.InsideListViewAnimatedItemsTransient.java
com.flipper.example.insidelist.activity.InsideListViewAnimatedItems.java
com.flipper.example.insidelist.activity.InsideMainActivity.java
com.flipper.example.insidelist.activity.NoRecycleActivity.java
com.flipper.example.insidelist.activity.RecicleBitmapActivity.java
com.flipper.example.insidelist.activity.RecycleActivity.java
com.flipper.example.insidelist.activity.ViewHolderActivity.java
com.flipper.example.insidelist.adapter.BaseInsideAdapter.java
com.flipper.example.insidelist.adapter.DebugAdapter.java
com.flipper.example.insidelist.adapter.OptionsAdapter.java
com.flipper.example.insidelist.adapter.RecicleBitmapAdapter.java
com.flipper.example.insidelist.adapter.ViewHolderAdapterAnimated.java
com.flipper.example.insidelist.builder.RenderBuilder.java
com.flipper.example.insidelist.component.DebugListView.java
com.flipper.example.insidelist.model.DataModel.java
com.flipper.example.insidelist.model.RenderOptionData.java
com.flipper.example.insidelist.model.RenderViewData.java
com.flipper.example.insidelist.rendermodel.GeneralInfoListener.java
com.flipper.example.insidelist.rendermodel.RenderAnimated.java
com.flipper.example.insidelist.rendermodel.RenderBase.java
com.flipper.example.insidelist.rendermodel.RenderBitmapCache.java
com.flipper.example.insidelist.rendermodel.RenderDebug.java
com.flipper.example.insidelist.rendermodel.RenderOptionView.java