//Add drag & drop to SetListFragment

This commit is contained in:
Gleb Maliborsky 2016-04-25 17:31:31 +03:00
parent 3c123f127d
commit 3cc74231c8
18 changed files with 266 additions and 48 deletions

View File

@ -1 +1 @@
BackPack
packwithme

View File

@ -5,18 +5,13 @@
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="1.8" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
<option name="myModules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>

View File

@ -37,37 +37,10 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
<state key="ScopeChooserConfigurable.UI">
<settings>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View File

@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/BackPack2.iml" filepath="$PROJECT_DIR$/BackPack2.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/packwithme.iml" filepath="$PROJECT_DIR$/packwithme.iml" />
</modules>
</component>
</project>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="" />
</component>
</project>

View File

@ -15,6 +15,7 @@ import hikapro.com.backpack.model.entities.Set;
import hikapro.com.backpack.presenter.ItemDetailPresenter;
import hikapro.com.backpack.presenter.ItemListPresenter;
import hikapro.com.backpack.presenter.SetListPresenter;
import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener;
import hikapro.com.backpack.view.View;
import hikapro.com.backpack.view.fragments.ItemDetailFragment;
import hikapro.com.backpack.view.fragments.ItemListFragment;

View File

@ -25,6 +25,9 @@ public interface Model {
void notifyDataSetChanged();
void setPresenter(Presenter.SetList presenter);
Presenter.SetList getPresenter();
//GLM
List<hikapro.com.backpack.model.entities.Set> getSet();
}
interface Item extends Base {

View File

@ -114,9 +114,11 @@ public class SetModel implements Model.Set {
// other <--
//GLM
@Override
public List<hikapro.com.backpack.model.entities.Set> getSet()
{
return iList;
}
}

View File

@ -2,7 +2,10 @@ package hikapro.com.backpack.presenter;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import hikapro.com.backpack.model.Model;
@ -34,6 +37,11 @@ public interface Presenter {
void setModel(Model.Set model);
void notifyDataSetChanged();
void showMessage(String message);
//GLM_add_resources_SetList
void onItemDismiss(int position);
boolean onItemMove(int fromPosition, int toPosition);
void onStartDrag(RecyclerView.ViewHolder viewHolder);
}
interface ItemList extends Base {

View File

@ -2,21 +2,29 @@ package hikapro.com.backpack.presenter;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Toast;
import java.lang.ref.WeakReference;
import java.util.Collections;
import hikapro.com.backpack.R;
import hikapro.com.backpack.model.SetModel;
import hikapro.com.backpack.model.Model;
import hikapro.com.backpack.model.entities.Set;
import hikapro.com.backpack.presenter.adapters.SetListAdapter;
import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener;
import hikapro.com.backpack.presenter.adapters.helper.SimpleItemTouchHelperCallback;
import hikapro.com.backpack.view.View;
import hikapro.com.backpack.view.fragments.SetListFragment;
import hikapro.com.backpack.view.recycler.SetViewHolder;
/**
@ -28,12 +36,33 @@ public class SetListPresenter implements Presenter.SetList {
private Model.Set model;
private SetListAdapter adapter;
//GLM_add_resources_SetList
private OnStartDragListener mDragStartListener;
private ItemTouchHelper mItemTouchHelper;
public SetListPresenter() {
this.adapter = new SetListAdapter(this);
}
// life cycle -->
//GLM_add_resources_SetList
@Override
public void onItemDismiss(int position) {
model.getSet().remove(position);
adapter.notifyItemRemoved(position);
}
//GLM_add_resources_SetList
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(model.getSet(), fromPosition, toPosition);
adapter.notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onDestroy(boolean isChangingConfiguration) {
view = null;
@ -52,6 +81,12 @@ public class SetListPresenter implements Presenter.SetList {
setRecycler.setAdapter(adapter);
setRecycler.setItemAnimator(new DefaultItemAnimator());
model.executeQuery();
//GLM_add_resources_SetList
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(setRecycler);
return view;
}
@ -81,6 +116,17 @@ public class SetListPresenter implements Presenter.SetList {
showMessage("There is no view in presenter");
}
});
//GLM_add_resources_SetList
holder.cardView.setOnLongClickListener(new android.view.View.OnLongClickListener() {
@Override
public boolean onLongClick(android.view.View v) {
mDragStartListener.onStartDrag(holder);
return false;
}
});
}
@Override
public int getSetsCount() {
@ -128,6 +174,7 @@ public class SetListPresenter implements Presenter.SetList {
@Override
public void setView(View.SetList view) {
this.view = new WeakReference<>(view);
this.mDragStartListener = getView().getOnStartDragListener();
}
@Override
public void setModel(Model.Set model) {
@ -147,5 +194,9 @@ public class SetListPresenter implements Presenter.SetList {
throw new NullPointerException("View is unavailable");
}
//GLM_add_resources_SetList
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
mItemTouchHelper.startDrag(viewHolder);
}
}

View File

@ -4,12 +4,13 @@ import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import hikapro.com.backpack.presenter.SetListPresenter;
import hikapro.com.backpack.presenter.adapters.helper.ItemTouchHelperAdapter;
import hikapro.com.backpack.view.recycler.SetViewHolder;
/**
* Created by tariel on 20/04/16.
*/
public class SetListAdapter extends RecyclerView.Adapter<SetViewHolder> {
public class SetListAdapter extends RecyclerView.Adapter<SetViewHolder> implements ItemTouchHelperAdapter{
private SetListPresenter presenter;
@ -31,4 +32,16 @@ public class SetListAdapter extends RecyclerView.Adapter<SetViewHolder> {
public int getItemCount() {
return presenter.getSetsCount();
}
//GLM_add_resources_SetList
@Override
public void onItemDismiss(int position) {
presenter.onItemDismiss(position);
}
//GLM_add_resources_SetList
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
return presenter.onItemMove(fromPosition, toPosition);
}
}

View File

@ -0,0 +1,12 @@
package hikapro.com.backpack.presenter.adapters.helper;
/**
* Created by N551 on 25.04.2016.
*/
//GLM_add_resources_SetList
public interface ItemTouchHelperAdapter {
boolean onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
}

View File

@ -0,0 +1,12 @@
package hikapro.com.backpack.presenter.adapters.helper;
/**
* Created by N551 on 25.04.2016.
*/
//GLM_add_resources_SetList
public interface ItemTouchHelperViewHolder {
void onItemSelected();
void onItemClear();
}

View File

@ -0,0 +1,12 @@
package hikapro.com.backpack.presenter.adapters.helper;
import android.support.v7.widget.RecyclerView;
/**
* Created by N551 on 25.04.2016.
*/
//GLM_add_resources_SetList
public interface OnStartDragListener {
void onStartDrag(RecyclerView.ViewHolder viewHolder);
}

View File

@ -0,0 +1,93 @@
package hikapro.com.backpack.presenter.adapters.helper;
import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
/**
* Created by N551 on 25.04.2016.
*/
//GLM_add_resources_SetList
public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
public static final float ALPHA_FULL = 1.0f;
private final ItemTouchHelperAdapter mAdapter;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;
}
// Notify the adapter of the move
mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
// Notify the adapter of the dismissal
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// Fade out the view as it is swiped out of the parent's bounds
final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// We only want the active item to change
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Let the view holder know that this item is being moved or dragged
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(ALPHA_FULL);
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Tell the view holder it's time to restore the idle state
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}
}

View File

@ -6,6 +6,7 @@ import android.widget.Toast;
import hikapro.com.backpack.model.entities.Item;
import hikapro.com.backpack.model.entities.Set;
import hikapro.com.backpack.presenter.Presenter;
import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener;
/**
* Created by tariel on 19/04/16.
@ -19,6 +20,9 @@ public interface View {
interface SetList extends Base {
void showItemList(Set set);
void setPresenter(Presenter.SetList presenter);
//GLM_add_resources_SetList
OnStartDragListener getOnStartDragListener();
}
interface ItemList extends Base {

View File

@ -5,18 +5,18 @@ import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import hikapro.com.backpack.R;
import hikapro.com.backpack.model.entities.Set;
import hikapro.com.backpack.presenter.Presenter;
import hikapro.com.backpack.presenter.SetListPresenter;
import hikapro.com.backpack.presenter.adapters.helper.OnStartDragListener;
public class SetListFragment extends Fragment implements hikapro.com.backpack.view.View.SetList {
public class SetListFragment extends Fragment implements hikapro.com.backpack.view.View.SetList, OnStartDragListener{
private Presenter.SetList presenter;
private hikapro.com.backpack.view.View.ActivityCallback activityCallback;
@ -130,4 +130,17 @@ public class SetListFragment extends Fragment implements hikapro.com.backpack.vi
public void setPresenter(Presenter.SetList presenter) {
this.presenter = presenter;
}
//GLM_add_resources_SetList
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
presenter.onStartDrag(viewHolder);
}
//GLM_add_resources_SetList
@Override
public OnStartDragListener getOnStartDragListener() {
return this;
}
}

View File

@ -1,27 +1,53 @@
package hikapro.com.backpack.view.recycler;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import hikapro.com.backpack.R;
import hikapro.com.backpack.presenter.adapters.helper.ItemTouchHelperViewHolder;
/**
* Created by tariel on 20/04/16.
*/
public class SetViewHolder extends RecyclerView.ViewHolder {
public class SetViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder{
public CardView cardView;
public TextView textView;
//GLM_add_resources_SetList
private View view;
private Drawable drawable;
public SetViewHolder(View v) {
super(v);
//
this.view = v;
//
setupViews(v);
}
private void setupViews(View view) {
cardView = (CardView) view.findViewById(R.id.card_view_set);
textView = (TextView) view.findViewById(R.id.set_text);
this.view = view;
drawable = view.getBackground();
}
//GLM_add_resources_SetList
@Override
public void onItemClear() {
view.setBackground(drawable);
}
//GLM_add_resources_SetList
@Override
public void onItemSelected() {
view.setBackgroundColor(Color.GRAY);
}
}