Implemented drag & drop reordering

This commit is contained in:
daniel oeh 2013-02-18 19:11:33 +01:00
parent 89d4bdc9c6
commit ef50c412c9
6 changed files with 125 additions and 16 deletions

View File

@ -0,0 +1,35 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res/de.danoeh.antennapod"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.mobeta.android.dslv.DragSortListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"
dslv:collapsed_height="2dp"
dslv:drag_enabled="true"
dslv:drag_handle_id="@id/drag_handle"
dslv:drag_scroll_start="0.33"
dslv:drag_start_mode="onDown"
dslv:float_alpha="0.6"
dslv:max_drag_scroll_speed="0.5"
dslv:remove_enabled="true"
dslv:remove_mode="flingRemove"
dslv:slide_shuffle_speed="0.3"
dslv:sort_enabled="true"
dslv:track_drag_sort="false"
dslv:use_default_controller="true" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/no_feeds_label" />
</LinearLayout>

View File

@ -11,7 +11,7 @@
android:scaleType="centerCrop" />
<View
android:id="@+id/dragHandle"
android:id="@id/drag_handle"
android:layout_width="@dimen/dragview_length"
android:layout_height="@dimen/dragview_length"
android:layout_alignParentRight="true"
@ -32,7 +32,7 @@
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_toLeftOf="@id/dragHandle"
android:layout_toLeftOf="@id/drag_handle"
android:layout_toRightOf="@id/imgvFeedimage"
android:ellipsize="end"
android:lines="2"
@ -46,7 +46,7 @@
android:layout_below="@id/txtvTitle"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_toLeftOf="@id/dragHandle"
android:layout_toLeftOf="@id/drag_handle"
android:layout_toRightOf="@id/imgvFeedimage"
android:ellipsize="end"
android:maxLines="1"

View File

@ -10,5 +10,5 @@
<item name="download_all_item" type="id"/>
<item name="clear_history_item" type="id"/>
<item name="organize_queue_item" type="id"/>
<item name="drag_handle" type="id" />
</resources>

View File

@ -2,7 +2,10 @@ package de.danoeh.antennapod.activity;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
@ -15,6 +18,7 @@ import android.widget.TextView;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.mobeta.android.dslv.DragSortListView;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
@ -33,12 +37,66 @@ public class OrganizeQueueActivity extends SherlockListActivity {
protected void onCreate(Bundle savedInstanceState) {
setTheme(PodcastApp.getThemeResourceId());
super.onCreate(savedInstanceState);
setContentView(R.layout.organize_queue);
DragSortListView listView = (DragSortListView) getListView();
listView.setDropListener(dropListener);
listView.setRemoveListener(removeListener);
adapter = new OrganizeAdapter(this, 0, FeedManager.getInstance()
.getQueue());
setListAdapter(adapter);
}
@Override
protected void onPause() {
super.onPause();
try {
unregisterReceiver(contentUpdate);
} catch (IllegalArgumentException e) {
}
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(FeedManager.ACTION_QUEUE_UPDATE);
filter.addAction(FeedManager.ACTION_FEED_LIST_UPDATE);
registerReceiver(contentUpdate, filter);
}
private BroadcastReceiver contentUpdate = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
};
private DragSortListView.DropListener dropListener = new DragSortListView.DropListener() {
@Override
public void drop(int from, int to) {
FeedManager manager = FeedManager.getInstance();
manager.moveQueueItem(OrganizeQueueActivity.this, from, to, false);
adapter.notifyDataSetChanged();
}
};
private DragSortListView.RemoveListener removeListener = new DragSortListView.RemoveListener() {
@Override
public void remove(int which) {
FeedManager manager = FeedManager.getInstance();
manager.removeQueueItem(OrganizeQueueActivity.this,
(FeedItem) getListAdapter().getItem(which));
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
@ -112,7 +170,6 @@ public class OrganizeQueueActivity extends SherlockListActivity {
TextView title;
TextView feedTitle;
ImageView feedImage;
View dragHandle;
}
}

View File

@ -765,16 +765,29 @@ public class FeedManager {
}
}
public void moveQueueItem(final Context context, FeedItem item, int delta) {
/**
* Moves the queue item at the specified index to another position. If the
* indices are out of range, no operation will be performed.
*
* @param from
* index of the item that is going to be moved
* @param to
* destination index of item
* @param broadcastUpdate
* true if the method should send a queue update broadcast after
* the operation has been performed. This should be set to false
* if the order of the queue is changed through drag & drop
* reordering to avoid visual glitches.
*/
public void moveQueueItem(final Context context, int from, int to,
boolean broadcastUpdate) {
if (AppConfig.DEBUG)
Log.d(TAG, "Moving queue item");
int itemIndex = queue.indexOf(item);
int newIndex = itemIndex + delta;
if (newIndex >= 0 && newIndex < queue.size()) {
FeedItem oldItem = queue.set(newIndex, item);
queue.set(itemIndex, oldItem);
Log.d(TAG, "Moving queue item from index " + from + " to index "
+ to);
if (from >= 0 && from < queue.size() && to >= 0 && to < queue.size()) {
FeedItem item = queue.remove(from);
queue.add(to, item);
dbExec.execute(new Runnable() {
@Override
public void run() {
PodDBAdapter adapter = new PodDBAdapter(context);
@ -783,10 +796,11 @@ public class FeedManager {
adapter.close();
}
});
}
if (broadcastUpdate) {
sendQueueUpdateBroadcast(context, item);
}
}
}
public boolean isInQueue(FeedItem item) {
return queue.contains(item);

View File

@ -63,6 +63,9 @@ public class EpisodesFragment extends SherlockFragment {
filter.addAction(FeedManager.ACTION_FEED_LIST_UPDATE);
getActivity().registerReceiver(contentUpdate, filter);
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
@Override