Android:更多滚动问题

时间:2010-04-09 22:43:08

标签: android

好吧,因为我之前问过的this question,我发现你无法知道WebView的滚动位置。我按照建议把我的内容放在ListView中。现在出现了一个新问题。

我想要做的就是告诉视图的某个部分滚动到屏幕的下边缘和上边缘。当图像出现在屏幕底部或消失在屏幕顶部上方时,我想播放声音效果。

这是我使用ListView的代码。问题是onScroll没有被一致地调用。似乎只有在我抬起手指等待,然后再次开始滚动时才会调用它。

同时,onScrollStateChanged并不总是被调用---它似乎只在滚动的第一帧被调用。

不管我是否触摸屏幕,是否有某种方法可以在滚动的每一帧上连续获取滚动信息?我只是想知道滚动动画是否已被激活,即使它是一个像素,这样我就可以查看图像元素的位置,看看它是否在屏幕上。

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;

     //
     // Will not bind views while the list is scrolling
     //
     //
    private class SlowAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public SlowAdapter(Context context) {
            mContext = context;
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

         //
         // The number of items in the list is determined by the number of speeches
         // in our array.
         //
         // @see android.widget.ListAdapter#getCount()
         //
        public int getCount() {
            return 0;//mStrings.length;
        }

         //
         // Since the data comes from an array, just returning the index is
         // sufficent to get at the data. If we were using a more complex data
         // structure, we would return whatever object represents one row in the
         // list.
         // 
         // @see android.widget.ListAdapter#getItem(int)
         //
        public Object getItem(int position) {
            return position;
        }

         //
         // Use the array index as a unique id.
         // 
         // @see android.widget.ListAdapter#getItemId(int)
         //
        public long getItemId(int position) {
            return position;
        }

         //
         // Make a view to hold each row.
         // 
         // @see android.widget.ListAdapter#getView(int, android.view.View,
         //      android.view.ViewGroup)
         //
        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.setText(mStrings[position]);
                // Null tag means the view has the correct data
                text.setTag(null);
            } else {
                text.setText("Loading...");
                // Non-null tag means the view still needs to load its data
                text.setTag(this);
            }

            return text;
        }

         //
         //Remember our context so we can use it when constructing views.
         //
        private Context mContext;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageListAdapter ila = new ImageListAdapter(this);

        // Add four items
        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));
        // Display it
        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);

        // Bottom edge appears at top of screen
        if (firstItemOnScreen == 2 && previousFirstItemOnScreen != 2) {
            tvt.playEffect(2, 0);
        }

        // Top edge appears at top of screen
        // AND
        // Bottom edge appears at bottom of screen
        if (firstItemOnScreen == 1 && lastItemOnScreen == 3) {
            if (lastItemOnScreen != previousLastItemOnScreen || firstItemOnScreen != previousFirstItemOnScreen)
                tvt.playEffect(2, 0);


        }

        // Top edge appears at bottom of screen
        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;
        }
    }

1 个答案:

答案 0 :(得分:0)

这个答案来得很晚,但在Android中实现滚动的方式我认为您可能需要将ListView子类化为您想要的。你可以覆盖scrollTo和scrollBy并检查你的条件(确保在适当的时候调用super.scrollTo和super.scrollBy。)