Android: more scrolling

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;

     //
     // 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;
        }
    }
+3
source share
1 answer

, Android. , ListView, , . scrollTo scrollBy ( super.scrollTo super.scrollBy ).

0

Source: https://habr.com/ru/post/1740536/


All Articles