Java tutorial
/* * Copyright 2015 Yuriy Yunikov * * 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 ua.yyunikov.android.view; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.content.Context; import android.content.res.TypedArray; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import ua.yyunikov.android.adapters.AdditionRemovalAdapter; /** * ListView (currently works only with {@link String} objects) with add and remove buttons and appropriate insert/delete animations. * For this {@link android.widget.ListView} to work correctly, please specify {@link ua.yyunikov.android.adapters.AdditionRemovalAdapter} as adapter. */ public class AdditionRemovalListView extends ListView { private static final int ANIMATION_TIME = 300; public AdditionRemovalListView(final Context context) { this(context, null); } public AdditionRemovalListView(final Context context, final AttributeSet attrs) { super(context, attrs); init(context, attrs); } // TODO implement add public void add(final AdditionRemovalAdapter.Item item) { final AdditionRemovalAdapter adapter = (AdditionRemovalAdapter) getAdapter(); adapter.addToPosition(item, 1); // adding to first position so 0 item will be for adding new items adapter.notifyDataSetChanged(); } public void remove(final AdditionRemovalAdapter.Item item) { final AdditionRemovalAdapter adapter = (AdditionRemovalAdapter) getAdapter(); adapter.remove(item); adapter.notifyDataSetChanged(); } public void removeWithAnimation(final AdditionRemovalAdapter.Item item, final View itemView) { final ObjectAnimator anim = ObjectAnimator.ofFloat(itemView, View.ALPHA, 0); anim.setDuration(ANIMATION_TIME); ViewCompat.setHasTransientState(itemView, true); anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { remove(item); itemView.setAlpha(1); ViewCompat.setHasTransientState(itemView, false); } }); anim.start(); } private void init(final Context context, final AttributeSet attrs) { initAttrs(context, attrs); } private void initAttrs(final Context context, final AttributeSet attrs) { final TypedArray attrsArray = context.obtainStyledAttributes(attrs, R.styleable.AdditionRemovalListView); attrsArray.recycle(); } @Override public void setAdapter(final ListAdapter adapter) { if (!(adapter instanceof AdditionRemovalAdapter)) { throw new IllegalArgumentException( "Adapter should be instance of " + AdditionRemovalAdapter.class.getSimpleName()); } super.setAdapter(adapter); } }