Ok, so because of this question I asked earlier, I found out that you cannot know the scroll position of the WebView. I followed the advice to put my content in a ListView. Now a new problem has appeared.
All I'm trying to do is indicate when a certain part of the view scrolls past the bottom edge and the top edge of the screen. When an image appears at the bottom of the screen or disappears above the top of the screen, I want to play a sound effect.
Here is my code using ListView. The problem is that onScroll is not being called sequentially. It seems to be called only if I lift my finger and wait, and then start scrolling again.
At the same time, onScrollStateChanged is not always called either - it seems to only be called in the first scroll frame.
Is there any way to just get the scroll information for each individual scroll frame, regardless of whether I touch the screen? I just want to know if the scroll animation has been activated, even if it is one pixel, so I can look at the position of the image element and see if it is on the screen or not.
public class Scroll extends ListActivity implements ListView.OnScrollListener {
public ListView listview;
public int firstItemOnScreen;
public int lastItemOnScreen;
public int previousFirstItemOnScreen;
public int previousLastItemOnScreen;
private TextView mStatus;
private boolean mBusy = false;
private class SlowAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public SlowAdapter(Context context) {
mContext = context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
text = (TextView)mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
} else {
text = (TextView)convertView;
}
if (!mBusy) {
text.setTag(null);
} else {
text.setText("Loading...");
text.setTag(this);
}
return text;
}
private Context mContext;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageListAdapter ila = new ImageListAdapter(this);
ila.addItem(new ImageList(
"", getResources().getDrawable(R.drawable.header)));
ila.addItem(new ImageList(
"Test text", null));
ila.addItem(new ImageList(
"", getResources().getDrawable(R.drawable.interstitial1)));
ila.addItem(new ImageList(
"Test text", null));
setListAdapter(ila);
getListView().setOnScrollListener(this);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount){
firstItemOnScreen = getListView().getFirstVisiblePosition();
lastItemOnScreen = getListView().getLastVisiblePosition();
Log.v("List", "first:" + firstItemOnScreen);
Log.v("List", "last:" + lastItemOnScreen);
if (firstItemOnScreen == 2 && previousFirstItemOnScreen != 2) {
tvt.playEffect(2, 0);
}
if (firstItemOnScreen == 1 && lastItemOnScreen == 3) {
if (lastItemOnScreen != previousLastItemOnScreen || firstItemOnScreen != previousFirstItemOnScreen)
tvt.playEffect(2, 0);
}
if (lastItemOnScreen == 2 && previousLastItemOnScreen != 2) {
tvt.playEffect(2, 0);
}
if (firstItemOnScreen == 3 && previousFirstItemOnScreen == 2)
tvt.playEffect(2, 0);
if (previousLastItemOnScreen == 3 && lastItemOnScreen == 2)
tvt.playEffect(2, 0);
if (previousFirstItemOnScreen == 1 && firstItemOnScreen == 2 && previousLastItemOnScreen == 3 && lastItemOnScreen == 3)
tvt.playEffect(2, 0);
if (firstItemOnScreen == 1 && previousFirstItemOnScreen == 1) {
if (lastItemOnScreen ==1 && previousLastItemOnScreen == 2)
tvt.playEffect(2, 0);
}
previousFirstItemOnScreen = firstItemOnScreen;
previousLastItemOnScreen = lastItemOnScreen;
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
mBusy = false;
int count = view.getChildCount();
for (int i=0; i<count; i++) {
ImageListView t = (ImageListView)view.getChildAt(i);
if (t.getTag() != null) {
t.setTag(null);
}
}
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
mBusy = true;
break;
case OnScrollListener.SCROLL_STATE_FLING:
mBusy = true;
break;
}
}