import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
-import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
listAdapter.setProfile(profile);
});
listAdapter.notifyDataSetChanged();
- new ItemTouchHelper(new ItemTouchHelper.Callback() {
- @Override
- public int getMovementFlags(@NonNull RecyclerView recyclerView,
- @NonNull RecyclerView.ViewHolder viewHolder) {
- int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
- // the top item is always there (date and description)
- if (viewHolder.getAdapterPosition() > 0) {
- if (viewModel.getAccountCount() > 2) {
- flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
- ItemTouchHelper.START | ItemTouchHelper.END);
- }
- }
-
- return flags;
- }
- @Override
- public boolean onMove(@NonNull RecyclerView recyclerView,
- @NonNull RecyclerView.ViewHolder viewHolder,
- @NonNull RecyclerView.ViewHolder target) {
- return false;
- }
- @Override
- public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
- if (viewModel.getAccountCount() == 2)
- Snackbar.make(list, R.string.msg_at_least_two_accounts_are_required,
- Snackbar.LENGTH_LONG)
- .setAction("Action", null)
- .show();
- else {
- int pos = viewHolder.getAdapterPosition();
- viewModel.removeItem(pos - 1);
- listAdapter.notifyItemRemoved(pos);
- viewModel.sendCountNotifications(); // needed after items re-arrangement
- viewModel.checkTransactionSubmittable(listAdapter);
- }
- }
- }).attachToRecyclerView(list);
-
viewModel.isSubmittable()
.observe(getViewLifecycleOwner(), isSubmittable -> {
if (isSubmittable) {
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
+import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
+import com.google.android.material.snackbar.Snackbar;
+
import net.ktnx.mobileledger.App;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
implements DescriptionSelectedCallback {
NewTransactionModel model;
private MobileLedgerProfile mProfile;
+ private ItemTouchHelper touchHelper;
+ private RecyclerView recyclerView;
NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
super();
model = viewModel;
String.format(Locale.US, "%d accounts is too little, Calling addRow()", size));
size = addRow();
}
+
+ NewTransactionItemsAdapter adapter = this;
+
+ touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
+ @Override
+ public boolean isLongPressDragEnabled() {
+ return true;
+ }
+ @Override
+ public boolean canDropOver(@NonNull RecyclerView recyclerView,
+ @NonNull RecyclerView.ViewHolder current,
+ @NonNull RecyclerView.ViewHolder target) {
+ final int adapterPosition = target.getAdapterPosition();
+
+ // first and last items are immovable
+ if (adapterPosition == 0)
+ return false;
+ if (adapterPosition == adapter.getItemCount() - 1)
+ return false;
+
+ return super.canDropOver(recyclerView, current, target);
+ }
+ @Override
+ public int getMovementFlags(@NonNull RecyclerView recyclerView,
+ @NonNull RecyclerView.ViewHolder viewHolder) {
+ int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
+ // the top item is always there (date and description)
+ if (viewHolder.getAdapterPosition() > 0) {
+ flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
+ ItemTouchHelper.UP | ItemTouchHelper.DOWN);
+
+ if (viewModel.getAccountCount() > 2) {
+ flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
+ ItemTouchHelper.START | ItemTouchHelper.END);
+ }
+ }
+
+ return flags;
+ }
+ @Override
+ public boolean onMove(@NonNull RecyclerView recyclerView,
+ @NonNull RecyclerView.ViewHolder viewHolder,
+ @NonNull RecyclerView.ViewHolder target) {
+
+ model.swapItems(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+ notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+ return true;
+ }
+ @Override
+ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
+ if (viewModel.getAccountCount() == 2)
+ Snackbar.make(recyclerView, R.string.msg_at_least_two_accounts_are_required,
+ Snackbar.LENGTH_LONG)
+ .setAction("Action", null)
+ .show();
+ else {
+ int pos = viewHolder.getAdapterPosition();
+ viewModel.removeItem(pos - 1);
+ notifyItemRemoved(pos);
+ viewModel.sendCountNotifications(); // needed after items re-arrangement
+ viewModel.checkTransactionSubmittable(adapter);
+ }
+ }
+ });
}
public void setProfile(MobileLedgerProfile profile) {
mProfile = profile;
LinearLayout row = (LinearLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.new_transaction_row,
parent, false);
+
return new NewTransactionItemHolder(row, this);
}
@Override
return true;
}
+ @Override
+ public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
+ super.onAttachedToRecyclerView(recyclerView);
+ this.recyclerView = recyclerView;
+ touchHelper.attachToRecyclerView(recyclerView);
+ }
+ @Override
+ public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
+ touchHelper.attachToRecyclerView(null);
+ super.onDetachedFromRecyclerView(recyclerView);
+ this.recyclerView = null;
+ }
public void descriptionSelected(String description) {
debug("descr selected", description);
if (!accountListIsEmpty())