0

I want some delay between two thing inside an OnItemClick,but actually nothing happens. If I run it, and click ,everything stops as long as is set the sleep, then it changes the pic at the same time.I would like to change the first pic. then Sleep then change the other.

 gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           // clicks=0;

            int row_no = position / 3;
            int col_no = position % 3;

            if(clicks==0)
            {
                ((ImageView) gv.getChildAt(position)).setImageResource(R.drawable.kor);
                matrix[row_no][col_no]=1;
               // clicks++;
            }

           if(gepLephet())
            {
                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally{
                    int posAi=gep();
                    ((ImageView) gv.getChildAt(posAi)).setImageResource(R.drawable.iksz);
                    matrix[posAi/3][posAi%3]=2;
                }

               // Toast.makeText(getApplicationContext(),"Random: "+posAi, Toast.LENGTH_SHORT).show();
            }

           /* else if(clicks==1)
            {
                ((ImageView) gv.getChildAt(position)).setImageResource(R.drawable.iksz);
                matrix[row_no][col_no]=2;
                clicks--;
            }*/
            // Toast.makeText(getApplicationContext(), "position: " + position +"Random: "+posAi, Toast.LENGTH_SHORT).show();
          // if(matrix[0][0]==matrix[0][1]&&matrix[0][1]==matrix[0][2]){text.setText("WIN");}
            if(isWinO())
            {
                text.setText("O WIN");
            }
            if(isWinX())
            {
                text.setText("X WIN");
            }



        }
        ;
    });
Csabi Vidó
  • 146
  • 1
  • 12

1 Answers1

4

onItemClick method runs inside UI thread so when you call Thread.sleep UI tread falls asleep and nothing changes on your screen. Never stop UI thread it will perceived by user as lag or bug. To change UI after some delay you can use postDelayed method of View. Sample:

View view;
view.postDelayed(new Runnable() {
    @Override public void run() {
        //code here will be run after 400ms delay
    }
}, 400);
eleven
  • 6,779
  • 2
  • 32
  • 52