Android newbie: touch events in gridview

I use the following code to work with gridview (slightly modified from http://developer.android.com/resources/tutorials/views/hello-gridview.html ). I want to replace the onClicklistener and onClick () method with their "touch" equivalents, i.e. Touchlistener and onTouch (), so when I touch an element in a gridview, the image of the element changes and double-tapping on the same element takes it back to its original state.

How should I do it? I cannot get my code to do this. The click editor works to some extent, but the touch doesn't. Please, help.

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);

        imageView.setOnClickListener(new View.OnClickListener()
            {

              @Override
              public void onClick(View view) 
              {

                  if(position==0)
                  {
                                  //do this
                              }
                              else
                              {
                                //do this
                              }
                           }
                    });

    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};
}
+3
3

OnTouchListener . MotionEvent, ACTION_UP, ACTION_MOVE ACTION_DOWN, , , ...

public void addListenerToGrid() {
    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {

            int action = me.getActionMasked();
            float currentXPosition = me.getX();
            float currentYPosition = me.getY();
            int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
                        if (action == MotionEvent.ACTION_UP) {
                            // Key was pressed here
                        }

            return true;

}

+7

, jaydeepfifadra, setOnItemClickListener , . ( , , ).

gridView.setOnItemClickListener(

    new OnItemClickListener(){
        public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
            ((Image)view.setSelected(!(Image)view.getSelected()));
        }
    });
);

100%, , view.setSelected() . , . ..

R.drawable.sample_2, :

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

    <item android:drawable="@drawable/image_unselected" />
</selector>

, , . . setOnDoubleClickItemListener().

, GestureDetector SimpleOnGestureListener (MyGestureListener SimpleOnGestureListener), , .

.

ontouch, onfling ?

gestureListener onTouchListener .

+2

Use onItemSelectListenergridView.

+1
source

Source: https://habr.com/ru/post/1739670/


All Articles