Search code examples
androidandroid-gallery

Android Gallery view listeners add delay?


I have implemented gallery and listview. Gallery has images and when i click on image using setOnItemSelectedListener Listview is updated which has info regarding image selected.

Problem: When I scroll through gallery rapidly it updates my listview rapidly. But according to me, listview should wait for gallery to stop moving on any One image and then update its contents.

Anyone know how to do it or add time between updation of listview ? Please help..


Solution

  • You can try setOnItemClickListener instead. It will be fired only when you actually "click" on image, not just select it.

        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Do what you want
            }
        });
    

    Update:

    If you want an action with delay, instead of onItemClickListener implementation, you have (at least) two choices. One is to run separate Thread with timeout (Thread.sleep()) and after it is done, you can use Handler to update your activity state. Another option is to use AsyncTask, that is technically the same approach, it only wraps Runnable task and Handler in one inner class. The simplest implementation might be like this:

        ListUpdater listUpdater; 
    
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
    
        // We have to run this thread to add delay before updating activity views
        if (listUpdater != null) { // Cancel previous task
            listUpdater.cancel(true);
        }
        if (listUpdater == null || position != listUpdater.getPosition()) {
            listUpdater = null;
            listUpdater = new ListUpdater(position);
    
            listUpdater.execute();
        }
    }
    
    class ListUpdater extends AsyncTask<String, String, String> {
        private int position;
    
        public int getPosition() {
            return position;
        }
    
        public ListUpdater(int position) {
            this.position = position;
        }
    
        @Override
        protected String doInBackground(String... params) { 
            try {
                Thread.sleep(600); // set delay time here
            } catch (InterruptedException e) {
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
            if (!isCancelled()) {
                // If another thread is not running, 
                //  that is if another item was not selected,
                //  update the activity 
    
                // Add code here to update the activity 
                // This method works in UI Thread, so you don't need to use Handler
                // For example:
                Toast.makeText(YourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(result);
        }
    }
    

    This is an example of implementation the delay. It is not necessary precise with your needs, but you can have some ideas from it how to make it.