0

I am developing an Android app, in which I use a GridView to draw multiple items. For drawing the items I've written an adapter - ImageAdapter. Every image is an integer number, which is the resource of the image which should be drawn on the specific position.

I have images for my own button (one image for "button up", and one for "button down"). I want to update the image "button up" to "button down" while the button is touched. Therefore I am using an OnTouchListener.

However when I update the image in my ImageAdapter and call the method notifyDataSetChanged (which is a method of the class BaseAdapter) the listener doesn't give me the event when the button isn't touched any more. That's because the resource to which the OnTouchListener was set was changed to the new image "button down".

However, if I do not call notifyDataSetChanged(), everything is great, but then the button image is not going to be updated.

My code:

private void setTouchListenerForButtons(
  Project project, final ImageAdapter imgadapt, 
  IConnection currentConnection, boolean editmode
  ) {
    for(int i = 0; i < imgadapt.getCount(); i++) {
        View v = project.getGui().getGridView().getChildAt(i);

        try {
            Object tag = v.getTag();

            int p = 0;

            if (tag != null) { // Button
                p = (Integer) tag;

                final int btnPosition = p;
                project.getGui().getGridView().getChildAt(btnPosition).setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Toast.makeText(getContext(), event.getAction() + "", Toast.LENGTH_SHORT).show();
                        switch (event.getAction()) {

                            case MotionEvent.ACTION_DOWN:
                                Log.d(LOG_TAG, "Button down");
                                imgadapt.update(R.drawable.button_on, btnPosition);
                                imgadapt.notifyDataSetChanged(); // that's the PROBLEM
                                break;

                            case MotionEvent.ACTION_UP:
                                Log.d(LOG_TAG, "Button up");
                                imgadapt.update(R.drawable.button_off, btnPosition);
                                imgadapt.notifyDataSetChanged();
                                break;
                        }

                        return true;
                    }
                });
            }
        } catch (NullPointerException e) {
        }

    }
}

How can I resolve this problem?

James
  • 4,644
  • 5
  • 37
  • 48
Sibo
  • 17
  • 5

1 Answers1

0

Don't use setOnTouchListener in this case. Create a selector:

drawable/selector_btn_default.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_default_pressed"
        android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_default"/>
</selector>

and set it as android:src of specific image.

<ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/selector_btn_default"/>

By the way, it is totally not good to call notifyDataSetChanged for this kind of use. It is wrong to refresh the whole GridView just to change an image on single item.

nuuneoi
  • 1,788
  • 1
  • 17
  • 14
  • Just one last question: How is it possible to run two different methods - depending on the status of the button? If the image is touched, method 1 should be run, when the button is not touched any more, method 2 should be run. – Sibo Mar 12 '15 at 16:44
  • In that case you need to setOnTouchListener but you have to handle the event wisely since it will consume the event and will cause many following problem along for example, GridView might not be able to scroll, etc. – nuuneoi Mar 12 '15 at 16:50
  • I've found out how to do it: http://stackoverflow.com/questions/12590169/android-press-and-hold-button-needs-to-change-states-custom-xml-selector-u – Sibo Mar 12 '15 at 17:39