Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright, I have found a solution. Note that this is merely <em>a</em> solution, and not necessarily the <em>best</em> solution.</p> <p>My solution is to just ignore all the logical events (like onScroll or onAnimationEnd, since I couldn't get any of them to work anyway), and listen to changes in a child view's location. When the child view is stationary, the animation has ended.</p> <p>An actual benefit of doing it this way is that this works for both dragging and flinging.</p> <p>A problem is that the onItemSelected function will be called from another thread than your UI thread. Solve that by using your activity's runOnUIThread function, as shown in the example.</p> <p>The way to listen for changes (note that this is not the regular <strong>onItemSelected</strong> function, but rather my own <strong>onItemReallySelected</strong>):</p> <pre><code>galArt.setOnItemReallySelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) { _activity.runOnUiThread(new Runnable() { public void run() { //Do your stuff here ... } }); } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { //... or here. } }); </code></pre> <p>My implementation of the Android Gallery:</p> <pre><code>import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.Gallery; public class ArtGallery extends Gallery { OnItemSelectedListener _listener; Timer _timer = new Timer(); public ArtGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ArtGallery(Context context, AttributeSet attrs) { super(context, attrs); } public ArtGallery(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_UP){ setTimer(); } return super.onTouchEvent(event); } private int _lastScrollX = Integer.MIN_VALUE; private void setTimer() { //Cancel existing tasks (if any), and create a new timer. _timer.cancel(); _timer = new Timer(); //Schedule our animation check. _timer.schedule(new TimerTask() { @Override public void run() { //Just some value that will change while the animation is running. int x = getSelectedView().getLeft(); if(_lastScrollX != x){ //Value has changed; save current value, and reset the timer. _lastScrollX = x; setTimer(); }else{ //The value hasn't changed during the last 50ms. That probably means that the animation has stopped. fireOnSelected(); } } }, 50); } public void setOnItemReallySelectedListener(OnItemSelectedListener listener){ _listener = listener; } //This function is copied from the Android Gallery source code, and works exactly like the original one. private void fireOnSelected() { if (_listener == null) return; int selection = this.getSelectedItemPosition(); if (selection &gt;= 0) { _listener.onItemSelected(this, getSelectedView(), selection, getAdapter().getItemId(selection)); } else { _listener.onNothingSelected(this); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload