Please evaluate the results of simultaneous implementation of onClick, LongClick, and onTouch.

Asked 2 years ago, Updated 2 years ago, 52 views

I am studying Android programming from the basics by making a simple notepad application.

Make listview an object in the main activity and the list view sets the adapter. Inside the adapter, notes entered by Sayo are added as items in the list view and displayed on the screen.

I'm going to add a touch event, an on-click event, and so on at first Listview.setOnItemClickListener and listview.setLongClick~~~ of the main activity class were registered, and public boolean onTouchEvent was implemented within the itemView class (inherited the linear layout)

However, because the three things did not work well at the same time, we removed the main activity contents using gestures through Gesture Detector.OnGestureListener through several googling and implemented only in itemView. The result is the code below.

Part of the itemView class (touch implementation part)

  public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {

        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        X_down = e1.getX();
        X_up = e2.getX();
        if (!isSwiped) {
            if ((X_down - X_up) > 0) {
                button.setVisibility(View.VISIBLE);
                button.startAnimation(translateLeftAnim);
                isSwiped = !isSwiped;
            }
        } } else {
            if ((X_down - X_up) < 0) {
                button.startAnimation(translateRightAnim);
                button.setVisibility(View.INVISIBLE);
                isSwiped = !isSwiped;
            }
        }
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        AlertDialog dialog = createDialogBox(textView);
        dialog.show();
    }

We haven't decided on the internal movements yet. I only checked the normal operation using toast or log.

In this way, we have confirmed that the button is operated by clicking, long clicking, and dragging the delete button to the side.

Now there's a problem, if you swipe to the left, you'll see the delete button, but if you click the delete button, you'll have to tell the main activity that it's clicked, and tell me which memo to delete, but I don't know how. I've thought about a few things

This reminds me, is there any other way?

Oh, and I applied the touch event to itemView now, and there is this method, but there is also a way to do it through the listview.set~listener of the main activity, which method is better? I think the above problem will be easily solved by registering a listener in mainActivity.

android

2022-09-22 22:15

2 Answers

I want to recommend the otto library, but you said you didn't want to use the library Let me explain another way.

Design pattern includes Opener pattern.

You can use this pattern to implement it. (I won't explain the pattern separately.) Please refer to the link above.)

The itemView class is Observerble The main activity will be Observer. Java provides Observable and Observer APIs for easy implementation of Observer patterns. When a click event or touch event occurs, you can send a notifyObserver() to the main activity to detect the event.

If the above observer pattern seems too heavy, the simplest way is to make your own event listener using the interface and call it. To explain briefly with the code, it's the following formula.

Create one listener in the
//itemView class.I defined the name of the listener as I please.
public interface MyItemClickListener {
        void onDeleteButtClick(intindex); // Function to invoke when the Delete button is clicked
}

//And create a method that sets the listener in the itemView class. I'm going to set it up during the main activity.  
MyItemClickListener listener;
public void setMyItemClickListener (MyItemClickListener listener) { 
    this.listener = listener;
}

And in the method called when you click the delete button,
Click listener.onDeleteButtClick() to invoke the main activity. 

And the main activity is to implement the listener defined in itemView.

public class MainActivity extends Activity implements MyItemClickListener {
protected void onCreate(Bundle savedInstanceState) {
    ...
    itemView.setOnMyItemClickListener(this);

    }

@Override
public void onDeleteButtClick(int index){
//If you called listener.onDeleteButtClick() when you clicked the delete button in itemView, this method would be called.
//Since I took over the index value, there will be an arrayList that shows in the listView. Delete the data value from the arrayList
//adapter.notifyDataSetChanged() will update the view. 
}
}

I don't know if it's explained properly. I hope it was helpful.


2022-09-22 22:15

I don't know how you implemented it, but if you process the delete button in the main activity, it would be more convenient to register the touch and click listener in the main activity.

So, if you receive a swipe event, even if you save the index of the item in advance, you can get the index that you saved in advance when the delete button is pressed and remove the item from the list.


2022-09-22 22:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.