How to implement the removal of elements as in the main menu of the android: pinch the item and drag it to the top?
I'm trying to do this, but there is a problem.

I have a ListView , when I pinch on some element, the RemoveBar fragment RemoveBar - it is responsible for the deletion layout, in which there is an ImageView .

 taskListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Log.d("CURR_TEST","itemClickLong"); dragItem((Task) parent.getItemAtPosition(position),view); TaskHolder.getInstance().putTask((Task) parent.getItemAtPosition(position)); sendCommand(FactoryCommandFragment.getInstance().getCommand("drag,"+parent.getItemIdAtPosition(position))); return true; } }); taskListView.setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { Log.d("dragEvent", "event : " + event.getAction()); Log.d("Bad Listener worked","rrr"); switch (event.getAction()) { case DragEvent.ACTION_DRAG_ENDED: draggedItemView.setVisibility(View.VISIBLE); break; // case DragEvent.ACTION_DROP: // sendCommand(FactoryCommandFragment.getInstance().getCommand("drag_cancel,l")); default: } return true; } }); 

The ImageView image also has OnDragListener . when the user omits the item above it. The item is deleted.

 public class RemoveBarFragment extends BarFragment { FragmentManager mFragmentManager; TopBarFragment topBarFragment; private final static String LOG_TAG = "BarFragment/RemoveBarFragment"; private void init(){ thisView.findViewById(R.id.removeImageButton).setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { Log.d(LOG_TAG, "drag_event : " + event.getAction()); Log.v("check","listener worked"); switch (event.getAction()) { case DragEvent.ACTION_DROP: sendCommand(FactoryCommandFragment.getInstance().getCommand("dragged,remove")); break; case DragEvent.ACTION_DRAG_ENDED: sendCommand(FactoryCommandFragment.getInstance().getCommand("drag_cancel,l")); break; default: } return true; } }); thisView.getFocusables(View.FOCUS_UP); Log.v("check", "listener set"); executeCommand(FactoryCommandFragment.getInstance().getTopCommand("top,default")); redraw(); } public void redraw(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment thisView = inflater.inflate(R.layout.fragment_remove_bar, container, false); init(); return thisView; } public void executeCommand(Command command) { if (thisView == null) { Log.d(LOG_TAG, "Command suspended"); SuspendedCommand suspended = new SuspendedCommand(); suspended.execute(command); Log.d(LOG_TAG, "Command executed"); return; } for (int i = 0; i < command.getCount(); i++) { String commandString = command.getCommand(i); Log.d(LOG_TAG, "command - " + commandString); String delim = "[,]"; String[] tokens = commandString.split(delim); if (tokens.length == 0) { throw new IllegalArgumentException("build commandString[" + i + "] is empty"); } if (tokens[0].equalsIgnoreCase("redraw")) { if (tokens.length == 1) { redraw(); } } } } } 

For unknown reasons. When I pinch an element for the first time and drag it onto a fragment for deletion, it is not deleted, but deleted a second time. I checked through the debugger, I looked at the thisView object from the RemoveBarFragment class, so OnDragListener is not set to it the first time, but only from the second.

In general, I tested, if I hold my finger on one of the elements for the first time and start to pull it, OnDragListener will not register in RemoveBarFragment until I RemoveBarFragment my finger. Why this happens is not clear. How to make so that from the first time deleted?

    1 answer 1

    Because in init() thisView is not yet assigned and == null

     public void executeCommand(Command command) { if (thisView == null) { //true Log.d(LOG_TAG, "Command suspended"); SuspendedCommand suspended = new SuspendedCommand(); suspended.execute(command); 

    And thisView is assigned after practicing init();
    I think this is the case