Android Getting Exact Scroll Position in Listview

Android getting exact scroll position in ListView

Okay, I found a workaround, using the following code:

View c = listview.getChildAt(0);
int scrolly = -c.getTop() + listview.getFirstVisiblePosition() * c.getHeight();

The way it works is it takes the actual offset of the first visible list item and calculates how far it is from the top of the view to determine how much we are "scrolled into" the view, so now that we know that we can calculate the rest using the regular getFirstVisiblePosition method.

Android ListView current scroll location Y pixels

There is no notion of Y scroll for a ListView in Android simply because the total height of the content is unknown. Only the height of the displayed content is known.

However it is possible to get the current position/Y scroll of a visible item using the following hack:

getListView().getChildAt(0).getTop();

ListView get scroll position?

Implement OnScrollListener for your listview.

@Override
public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount) {
//firstvisible is your first visible item in the list
}

Maintain/Save/Restore scroll position when returning to a ListView

Try this:

// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop());

// ...

// restore index and position
mList.setSelectionFromTop(index, top);

Explanation:

ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, and if you want to restore the exact scroll position of the list you need to get this offset. So ListView.getChildAt(0) returns the View for the top list item, and then View.getTop() - mList.getPaddingTop() returns its relative offset from the top of the ListView. Then, to restore the ListView's scroll position, we call ListView.setSelectionFromTop() with the index of the item we want and an offset to position its top edge from the top of the ListView.

Exact y-Position of an Element in ListView

Try this with Listview itemview.

Create method for showing popupwindow in adaper class.

 private void initiatePopupWindow(View v) {
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.popup,null);
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, 400, 600, true);
// display the popup in the center
pw.showAtLocation(v, Gravity.TOP, 0, v.getTop() + v.getMeasuredHeight() + 300); //300 is haif of popwindow height to place popupwindow top line below item bottomline.
TextView mResultText = (TextView) layout.findViewById(R.id.result_txt);
Button cancelButton = (Button) layout.findViewById(R.id.btn_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
}
});

} catch (Exception e) {
e.printStackTrace();
}
}

And add click event to view in item in listview.

 textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initiatePopupWindow(itemView);//itemview is your entire row view of listview.
}

Output

Sample Image

Hope it helps.!

How to detect scroll position of ListView in Flutter

I used NotificationListener that is a widget that listens for notifications bubbling up the tree. Then use ScrollEndNotification, which indicates that scrolling has stopped.

For scroll position I used _scrollController that type is ScrollController.

NotificationListener(
child: ListView(
controller: _scrollController,
children: ...
),
onNotification: (t) {
if (t is ScrollEndNotification) {
print(_scrollController.position.pixels);
}
//How many pixels scrolled from pervious frame
print(t.scrollDelta);

//List scroll position
print(t.metrics.pixels);
},
),

Android ListView Scroll Position in Pixel Detect

Check my answer to this question.

View.getScrollY()

is a method inherited from View, and the doc says:

Return the scrolled top position of this view. This is the top edge of the displayed part of your view.

Therefore for full screen lists it will always return 0.



Related Topics



Leave a reply



Submit