Skip to content

Commit

Permalink
Implemented SLH adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Jun 15, 2014
1 parent 3040c46 commit 5ca1977
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.nhaarman.listviewanimations.ArrayAdapter;
import com.nhaarman.listviewanimations.itemmanipulation.OnDismissCallback;
import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeDismissAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.SwingBottomInAnimationAdapter;

import java.util.ArrayList;

Expand All @@ -48,8 +48,8 @@ protected void onCreate(final Bundle savedInstanceState) {

mGoogleCardsAdapter = new GoogleCardsAdapter(this);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new SwipeDismissAdapter(mGoogleCardsAdapter, this));
swingBottomInAnimationAdapter.setInitialDelayMillis(300);
swingBottomInAnimationAdapter.setAbsListView(listView);
swingBottomInAnimationAdapter.getViewAnimator().setInitialDelayMillis(300);

listView.setAdapter(swingBottomInAnimationAdapter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import android.widget.ImageView;

import com.nhaarman.listviewanimations.ArrayAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.SwingBottomInAnimationAdapter;

public class GridViewActivity extends ActionBarActivity {

Expand All @@ -33,7 +33,7 @@ protected void onCreate(final Bundle savedInstanceState) {
GridView gridView = (GridView) findViewById(R.id.activity_gridview_gv);
SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new MyAdapter(this, getItems()));
swingBottomInAnimationAdapter.setAbsListView(gridView);
swingBottomInAnimationAdapter.setInitialDelayMillis(300);
swingBottomInAnimationAdapter.getViewAnimator().setInitialDelayMillis(300);
gridView.setAdapter(swingBottomInAnimationAdapter);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import android.widget.TextView;

import com.nhaarman.listviewanimations.ArrayAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.util.StickyListHeadersAdapterDecorator;
import com.nhaarman.listviewanimations.swinginadapters.StickyListHeadersAdapterDecorator;
import com.nhaarman.listviewanimations.swinginadapters.simple.SLHAlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.util.StickyListHeadersListViewWrapper;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
Expand All @@ -28,7 +28,7 @@ protected void onCreate(final Bundle savedInstanceState) {
integers.add(i);
}

AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(integers);
SLHAlphaInAnimationAdapter animationAdapter = new SLHAlphaInAnimationAdapter(integers);
StickyListHeadersAdapterDecorator stickyListHeadersAdapterDecorator = new StickyListHeadersAdapterDecorator(animationAdapter);
stickyListHeadersAdapterDecorator.setListViewWrapper(new StickyListHeadersListViewWrapper(mListView));
mListView.setAdapter(stickyListHeadersAdapterDecorator);
Expand All @@ -46,7 +46,7 @@ public View getHeaderView(final int i, final View view, final ViewGroup viewGrou

@Override
public long getHeaderId(final int i) {
return i;
return i/10;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import com.haarman.listviewanimations.R;
import com.nhaarman.listviewanimations.ArrayAdapter;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingLeftInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingRightInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.ScaleInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.SwingBottomInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.SwingLeftInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.SwingRightInAnimationAdapter;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.haarman.listviewanimations.MyListActivity;
import com.haarman.listviewanimations.R;
import com.nhaarman.listviewanimations.ArrayAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.widget.DynamicListView;

public class DragAndDropActivity extends MyListActivity {
Expand All @@ -41,8 +41,8 @@ protected void onCreate(final Bundle savedInstanceState) {

final ArrayAdapter<Integer> adapter = createListAdapter();
AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(adapter);
animAdapter.setInitialDelayMillis(300);
animAdapter.setAbsListView(listView);
animAdapter.getViewAnimator().setInitialDelayMillis(300);
listView.setAdapter(animAdapter);

Toast.makeText(this, "Long press an item to start dragging", Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.haarman.listviewanimations.MyListActivity;
import com.haarman.listviewanimations.R;
import com.nhaarman.listviewanimations.itemmanipulation.ExpandableListItemAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.simple.AlphaInAnimationAdapter;

import java.util.List;

Expand All @@ -48,7 +48,7 @@ protected void onCreate(final Bundle savedInstanceState) {
mExpandableListItemAdapter = new MyExpandableListItemAdapter(this, getItems());
AlphaInAnimationAdapter alphaInAnimationAdapter = new AlphaInAnimationAdapter(mExpandableListItemAdapter);
alphaInAnimationAdapter.setAbsListView(getListView());
alphaInAnimationAdapter.setInitialDelayMillis(500);
alphaInAnimationAdapter.getViewAnimator().setInitialDelayMillis(500);
getListView().setAdapter(alphaInAnimationAdapter);

Toast.makeText(this, R.string.explainexpand, Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.nhaarman.listviewanimations.swinginadapters;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.nhaarman.listviewanimations.BaseAdapterDecorator;
import com.nhaarman.listviewanimations.util.AnimatorUtil;
import com.nhaarman.listviewanimations.util.ListViewWrapper;
import com.nhaarman.listviewanimations.util.StickyListHeadersListViewWrapper;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
* A {@link com.nhaarman.listviewanimations.BaseAdapterDecorator} which can be used to animate header views provided by a
* {@link se.emilsjolander.stickylistheaders.StickyListHeadersAdapter}.
*/
public class StickyListHeadersAdapterDecorator extends BaseAdapterDecorator<StickyListHeadersListView> implements StickyListHeadersAdapter {

/**
* Alpha property.
*/
private static final String ALPHA = "alpha";

/**
* The decorated {@link se.emilsjolander.stickylistheaders.StickyListHeadersAdapter}.
*/
@NonNull
private final StickyListHeadersAdapter mStickyListHeadersAdapter;

/**
* The {@link com.nhaarman.listviewanimations.swinginadapters.ViewAnimator} responsible for animating the Views.
*/
@Nullable
private ViewAnimator<StickyListHeadersListView> mViewAnimator;

/**
* Create a new {@code StickyListHeadersAdapterDecorator}, decorating given {@link android.widget.BaseAdapter}.
*
* @param baseAdapter the {@code BaseAdapter} to decorate. If this is a {@code BaseAdapterDecorator}, it should wrap an instance of
* {@link se.emilsjolander.stickylistheaders.StickyListHeadersAdapter}.
*/
public StickyListHeadersAdapterDecorator(@NonNull final BaseAdapter baseAdapter) {
super(baseAdapter);

BaseAdapter adapter = baseAdapter;
while (adapter instanceof BaseAdapterDecorator) {
adapter = ((BaseAdapterDecorator<StickyListHeadersListView>) adapter).getDecoratedBaseAdapter();
}

if (!(adapter instanceof StickyListHeadersAdapter)) {
throw new IllegalArgumentException(adapter.getClass().getCanonicalName() + " does not implement StickyListHeadersAdapter");
}

mStickyListHeadersAdapter = (StickyListHeadersAdapter) adapter;
}

/**
* @deprecated use {@link #setStickyListHeadersListView(se.emilsjolander.stickylistheaders.StickyListHeadersListView)} instead.
*/
@Override
@Deprecated
public void setAbsListView(@NonNull final AbsListView absListView) {
super.setAbsListView(absListView);
}

/**
* Sets the {@link se.emilsjolander.stickylistheaders.StickyListHeadersListView} that this adapter will be bound to.
*/
public void setStickyListHeadersListView(@NonNull final StickyListHeadersListView listView) {
ListViewWrapper<StickyListHeadersListView> stickyListHeadersListViewWrapper = new StickyListHeadersListViewWrapper(listView);
setListViewWrapper(stickyListHeadersListViewWrapper);
}

@Override
public void setListViewWrapper(@NonNull final ListViewWrapper<StickyListHeadersListView> listViewWrapper) {
super.setListViewWrapper(listViewWrapper);
mViewAnimator = new ViewAnimator<>(listViewWrapper);
}

@Override
public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
if (getListViewWrapper() == null) {
throw new IllegalStateException("Call setStickyListHeadersListView() on this AnimationAdapter first!");
}
assert mViewAnimator != null;

if (convertView != null) {
mViewAnimator.cancelExistingAnimation(convertView);
}

View itemView = mStickyListHeadersAdapter.getHeaderView(position, convertView, parent);

animateViewIfNecessary(position, itemView, parent);
return itemView;
}

/**
* Animates given View if necessary.
*
* @param position the position of the item the View represents.
* @param view the View that should be animated.
* @param parent the parent the View is hosted in.
*/
private void animateViewIfNecessary(final int position, @NonNull final View view, @NonNull final ViewGroup parent) {
Animator[] childAnimators;
if (getDecoratedBaseAdapter() instanceof AnimationAdapter) {
childAnimators = ((AnimationAdapter<StickyListHeadersListView>) getDecoratedBaseAdapter()).getAnimators(parent, view);
} else {
childAnimators = new Animator[0];
}
Animator alphaAnimator = ObjectAnimator.ofFloat(view, ALPHA, 0, 1);

assert mViewAnimator != null;
mViewAnimator.animateViewIfNecessary(position, view, AnimatorUtil.concatAnimators(childAnimators, new Animator[0], alphaAnimator));
}

@Override
public long getHeaderId(final int i) {
return mStickyListHeadersAdapter.getHeaderId(i);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2014 Niek Haarman
*
* 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.nhaarman.listviewanimations.swinginadapters.simple;

import android.support.annotation.NonNull;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.nhaarman.listviewanimations.swinginadapters.simple.generic.AlphaInAnimationAdapterGen;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

public class SLHAlphaInAnimationAdapter extends AlphaInAnimationAdapterGen<StickyListHeadersListView> {

public SLHAlphaInAnimationAdapter(@NonNull final BaseAdapter baseAdapter) {
super(baseAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2013 Niek Haarman
*
* 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.nhaarman.listviewanimations.swinginadapters.simple;

import android.support.annotation.NonNull;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.nhaarman.listviewanimations.swinginadapters.simple.generic.ScaleInAnimationAdapterGen;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

public class SLHScaleInAnimationAdapter extends ScaleInAnimationAdapterGen<StickyListHeadersListView> {

public SLHScaleInAnimationAdapter(@NonNull final BaseAdapter baseAdapter) {
super(baseAdapter);
}

public SLHScaleInAnimationAdapter(@NonNull final BaseAdapter baseAdapter, final float scaleFrom) {
super(baseAdapter, scaleFrom);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2013 Niek Haarman
*
* 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.nhaarman.listviewanimations.swinginadapters.simple;

import android.support.annotation.NonNull;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.nhaarman.listviewanimations.swinginadapters.simple.generic.SwingBottomInAnimationAdapterGen;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
* An implementation of the AnimationAdapter class which applies a
* swing-in-from-bottom-animation to views.
*/
public class SLHSwingBottomInAnimationAdapter extends SwingBottomInAnimationAdapterGen<StickyListHeadersListView> {

public SLHSwingBottomInAnimationAdapter(@NonNull final BaseAdapter baseAdapter) {
super(baseAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2013 Niek Haarman
*
* 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.nhaarman.listviewanimations.swinginadapters.simple;

import android.support.annotation.NonNull;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.nhaarman.listviewanimations.swinginadapters.simple.generic.SwingLeftInAnimationAdapterGen;

import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
* An implementation of the AnimationAdapter class which applies a
* swing-in-from-the-left-animation to views.
*/
@SuppressWarnings("UnusedDeclaration")
public class SLHSwingLeftInAnimationAdapter extends SwingLeftInAnimationAdapterGen<StickyListHeadersListView> {

public SLHSwingLeftInAnimationAdapter(@NonNull final BaseAdapter baseAdapter) {
super(baseAdapter);
}
}
Loading

0 comments on commit 5ca1977

Please sign in to comment.