Back to project page ListViewAnimations.
The source code is released under:
Apache License
If you think the Android project ListViewAnimations listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Niek Haarman// ww w .j av a2 s.c om * * 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.haarman.listviewanimations.itemmanipulation; import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.TextView; import android.widget.Toast; import com.haarman.listviewanimations.MyListActivity; import com.haarman.listviewanimations.R; import com.nhaarman.listviewanimations.ArrayAdapter; import com.nhaarman.listviewanimations.itemmanipulation.DynamicListView; import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovedListener; import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.TouchViewDraggableManager; import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.OnDismissCallback; import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SimpleSwipeUndoAdapter; import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.UndoAdapter; import com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter; import java.util.Arrays; public class DynamicListViewActivity extends MyListActivity { private static final int INITIAL_DELAY_MILLIS = 300; private int mNewItemCount; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dynamiclistview); DynamicListView listView = (DynamicListView) findViewById(R.id.activity_dynamiclistview_listview); listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.activity_dynamiclistview_header, listView, false)); /* Setup the adapter */ ArrayAdapter<String> adapter = new MyListAdapter(this); SimpleSwipeUndoAdapter simpleSwipeUndoAdapter = new SimpleSwipeUndoAdapter(adapter, this, new MyOnDismissCallback(adapter)); AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(simpleSwipeUndoAdapter); animAdapter.setAbsListView(listView); assert animAdapter.getViewAnimator() != null; animAdapter.getViewAnimator().setInitialDelayMillis(INITIAL_DELAY_MILLIS); listView.setAdapter(animAdapter); /* Enable drag and drop functionality */ listView.enableDragAndDrop(); listView.setDraggableManager(new TouchViewDraggableManager(R.id.list_row_draganddrop_touchview)); listView.setOnItemMovedListener(new MyOnItemMovedListener(adapter)); listView.setOnItemLongClickListener(new MyOnItemLongClickListener(listView)); /* Enable swipe to dismiss */ listView.enableSimpleSwipeUndo(); /* Add new items on item click */ listView.setOnItemClickListener(new MyOnItemClickListener(listView)); } private static class MyListAdapter extends ArrayAdapter<String> implements UndoAdapter { private final Context mContext; MyListAdapter(final Context context) { mContext = context; for (int i = 0; i < 20; i++) { add(mContext.getString(R.string.row_number, i)); } } @Override public long getItemId(final int position) { return getItem(position).hashCode(); } @Override public boolean hasStableIds() { return true; } @Override public View getView(final int position, final View convertView, final ViewGroup parent) { View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.list_row_dynamiclistview, parent, false); } ((TextView) view.findViewById(R.id.list_row_draganddrop_textview)).setText(getItem(position)); return view; } @NonNull @Override public View getUndoView(final int position, final View convertView, @NonNull final ViewGroup parent) { View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false); } return view; } @NonNull @Override public View getUndoClickView(@NonNull final View view) { return view.findViewById(R.id.undo_row_undobutton); } } private static class MyOnItemLongClickListener implements AdapterView.OnItemLongClickListener { private final DynamicListView mListView; MyOnItemLongClickListener(final DynamicListView listView) { mListView = listView; } @Override public boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position, final long id) { if (mListView != null) { mListView.startDragging(position - mListView.getHeaderViewsCount()); } return true; } } private class MyOnDismissCallback implements OnDismissCallback { private final ArrayAdapter<String> mAdapter; @Nullable private Toast mToast; MyOnDismissCallback(final ArrayAdapter<String> adapter) { mAdapter = adapter; } @Override public void onDismiss(@NonNull final ViewGroup listView, @NonNull final int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { mAdapter.remove(position); } if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText( DynamicListViewActivity.this, getString(R.string.removed_positions, Arrays.toString(reverseSortedPositions)), Toast.LENGTH_LONG ); mToast.show(); } } private class MyOnItemMovedListener implements OnItemMovedListener { private final ArrayAdapter<String> mAdapter; private Toast mToast; MyOnItemMovedListener(final ArrayAdapter<String> adapter) { mAdapter = adapter; } @Override public void onItemMoved(final int originalPosition, final int newPosition) { if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(getApplicationContext(), getString(R.string.moved, mAdapter.getItem(newPosition), newPosition), Toast.LENGTH_SHORT); mToast.show(); } } private class MyOnItemClickListener implements AdapterView.OnItemClickListener { private final DynamicListView mListView; MyOnItemClickListener(final DynamicListView listView) { mListView = listView; } @Override public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) { mListView.insert(position, getString(R.string.newly_added_item, mNewItemCount)); mNewItemCount++; } } }