Android getting exact scroll position in ListView

前端 未结 8 1216
误落风尘
误落风尘 2020-12-01 02:56

I\'d like to get the exact, pixel position of the ListView scroll. And no, I am not referring to the first visible position.

Is there a way to achieve this?

相关标签:
8条回答
  • 2020-12-01 03:37

    Saarraz1's answer will only work if all the rows in the listview are of the same height and there's no header (or it is also the same height as the rows).

    Note that once the rows disappear at the top of the screen you don't have access to them, as in you won't be able to keep track of their height. This is why you need to save those heights (or accumulated heights of all). My solution requires keeping a Dictionary of heights per index (it is assumed that when the list is displayed the first time it is scrolled to the top).

    private Dictionary<Integer, Integer> listViewItemHeights = new Hashtable<Integer, Integer>();
    
    private int getScroll() {
        View c = listView.getChildAt(0); //this is the first visible row
        int scrollY = -c.getTop();
        listViewItemHeights.put(listView.getFirstVisiblePosition(), c.getHeight());
        for (int i = 0; i < listView.getFirstVisiblePosition(); ++i) {
            if (listViewItemHeights.get(i) != null) // (this is a sanity check)
                scrollY += listViewItemHeights.get(i); //add all heights of the views that are gone
        }
        return scrollY;
    }
    
    0 讨论(0)
  • 2020-12-01 03:38

    I had faced the similar problem, That I wanted to place the Vertical Seekbar at current scrolled value of ListView. So I have my own solution like this.

    First Create Class

    public abstract class OnScrollPositionChangedListener implements AbsListView.OnScrollListener {
        int pos;
        public  int viewHeight = 0;
        public  int listHeight = 0;
    
        @Override
        public void onScroll(AbsListView v, int i, int vi, int n) {
            try {
                if(viewHeight==0) {
                    viewHeight = v.getChildAt(0).getHeight();
                    listHeight = v.getHeight();
    
                }
                pos = viewHeight * i;
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                onScrollPositionChanged(pos);
            }
        }
        @Override public void onScrollStateChanged(AbsListView absListView, int i) {}
        public abstract void onScrollPositionChanged(int scrollYPosition);
    }
    

    Then use it in Main Activity like this.

    public class MainActivity extends AppCompatActivity {
        SeekBar seekBar;
        ListView listView;
        OnScrollPositionChangedListener onScrollPositionChangedListener = new OnScrollPositionChangedListener() {
            @Override
            public void onScroll(AbsListView v, int i, int vi, int n) {
                super.onScroll(v, i, vi, n);
                //Do your onScroll stuff
            }
    
            @Override
            public void onScrollPositionChanged(int scrollYPosition) {
                //Enjoy having access to the amount the ListView has scrolled
    
                seekBar.setProgress(scrollYPosition);
    
    
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            seekBar = (SeekBar) findViewById(R.id.mySeekBar);
            listView = (ListView) findViewById(R.id.listView);
            final String[] values = new String[]{"Android List View",
                    "Adapter implementation",
                    "Simple List View In Android",
                    "Create List View Android",
                    "Android Example",
    
            };
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, android.R.id.text1, values);
            listView.setAdapter(adapter);
            listView.setOnScrollListener(onScrollPositionChangedListener);
    
    
    
            seekBar.postDelayed(new Runnable() {
                @Override
                public void run() {
                    seekBar.setMax((onScrollPositionChangedListener.viewHeight * values.length) - onScrollPositionChangedListener.listHeight);
                }
            }, 1000);
            seekBar.setEnabled(false);
    
        }
    }
    

    in App Gradle

    compile 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:0.7.0'
    

    In XML Layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
    
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.centsol.charexamples.MainActivity">
    
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fadeScrollbars="false"
    
    
            android:scrollbars="none"
    
            android:layout_alignParentTop="true"
    
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
    
        </ListView>
        <!-- This library requires pair of the VerticalSeekBar and VerticalSeekBarWrapper classes -->
        <com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper
            android:layout_width="wrap_content"
    
            android:layout_alignParentRight="true"
            android:layout_height="match_parent">
            <com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar
                android:id="@+id/mySeekBar"
                android:layout_width="0dp"
                android:progressDrawable="@drawable/progress"
                android:thumb="@drawable/thumb"
                android:layout_height="0dp"
                android:splitTrack="false"
    
                app:seekBarRotation="CW90" /> <!-- Rotation: CW90 or CW270 -->
        </com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper>
        <View
            android:layout_width="1dp"
            android:visibility="visible"
            android:layout_alignParentRight="true"
            android:layout_marginTop="16dp"
            android:layout_marginRight="2.5dp"
            android:layout_marginBottom="16dp"
            android:background="@android:color/black"
            android:layout_height="match_parent" />
    </RelativeLayout>
    

    background xml

        <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <solid android:color="@android:color/transparent"/>
    
    </shape>
    

    fill xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <solid android:color="@android:color/transparent" />
    
    
    </shape>
    

    progress xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@android:id/background"
            android:drawable="@drawable/background"/>
        <item android:id="@android:id/progress">
            <clip android:drawable="@drawable/fill" />
        </item>
    
    </layer-list>
    

    thumb xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
    
    
    <solid android:color="@android:color/holo_red_dark" />
        <size
            android:height="5dp"
            android:width="5dp" />
    
    </shape>
    
    0 讨论(0)
提交回复
热议问题