Hide fab button with ListView when scrolling down [duplicate] - android

This question already has answers here:
How to detect if a listview is scrolling up or down in android?
(6 answers)
Closed 5 years ago.
How can I hide fab button when scrolling listView?
I'm using this code at the moment, but it hides FAB button whenever I'm touching screen and scrolling, I need it to hide FAB button when scrolling down and when scrolling a bit up it has to be shown again
current code:
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
floatingActionButton.hide();
}else{
floatingActionButton.show();
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});

try this you should use onScroll instead of onScrollStateChanged
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// add here your logic like this
// int lastItem = firstVisibleItem + visibleItemCount;
if (firstVisibleItem < 2) {
floatingActionButton.setVisibility(View.INVISIBLE);
}else {
floatingActionButton.setVisibility(View.VISIBLE);
}
}
});

Related

How can I know when a custom ListView is scrolling Up?

I can detected when I scroll until the very bottom using
.size() - 1 of my elem. But if I start to scroll up at the middle of the list, how can I know it, in the method GetView() of course.
Found a way to do it which works more or less well...
list.setOnScrollListener(new AbsListView.OnScrollListener() {
private int mLastFirstVisibleItem;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(mLastFirstVisibleItem<firstVisibleItem) /* DOWN */
{
scrolling = false;
}
if(mLastFirstVisibleItem>firstVisibleItem) /* UP*/
{
scrolling = true;
}
mLastFirstVisibleItem=firstVisibleItem;
}
});

Synchronize scrolling of two ListViews in Android

I need something like a table with two columns and dynamic no. of rows (which increases and decreases according to the length of data i receive ). Currently what I have done is, I added to listviews in a horizontal linear layout so it looks like a table. But the issue is both the listview's scrolling is independent of other. I need to lock the scrolling so if i scroll both will not move independently.
All the data in the listview are readonly (just for display purpose).
is it possible to sync the scroll or is there any better way by which my requirements can be met?
If you you want to stop the first list view scroll while scrolling the 2nd list and vice versaa other.Then put the Scroll listner for the lsitviews and block the other listview scrolling. here is example
listview1.setOnScrollListener(new OnScrollListener() {
private int mLastFirstVisibleItem;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
listview1.setEnabled(true);
listview2.setEnabled(true);
Log.i("a", "scrolling stopped...");
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
listview2.setEnabled(false);
}
});
Same way for the second list view
listview2.setOnScrollListener(new OnScrollListener() {
private int mLastFirstVisibleItem;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
listview1.setEnabled(true);
listview2.setEnabled(true);
Log.i("a", "scrolling stopped...");
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
listview1.setEnabled(false);
}
});

ExpandableListView last group's last child visible on screen event?

How can i get the event that is written in title? I want to know if that RawLastChild is visible on screen or not.
How can i catch event?
You can use this code for expandablelistview to see the last element
mExpandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount
, int totalItemCount) {
if(firstVisibleItem+visibleItemCount>=totalItemCount){
//the last item is visible
}
}
});
Following code snippet will solve your problem. Tested and working fine!
expandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
if (!absListView.canScrollList(View.SCROLL_AXIS_VERTICAL) && scrollState == SCROLL_STATE_IDLE) {
//When List reaches bottom
loadMoreData();
}
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}

android Listview listen to header view y position changes

I m looking to fire some function when the listview header view top edge appears on the phone.
How can you listen to header view y axis position changes ?
lv.setOnScrollListener(new OnScrollListener() {
private int lastFirstVisibleItem;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(lastFirstVisibleItem<firstVisibleItem)
{
Log.i("SCROLLING DOWN","TRUE");
}
if(lastFirstVisibleItem>firstVisibleItem)
{
Log.i("SCROLLING UP","TRUE");
}
lastFirstVisibleItem=firstVisibleItem;
}
});
listView.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean topOfFirst = listView.getChildAt(0).getTop() == 0;
if (topOfFirst) {
//Do something
}
}
}
The header should take position 0 in a ListView so the boolean will be set to true when the top of position 0 is visible.
I found another way. You can use view.getTop() and view.getBottom() , in this case the view will be the inflated list header.

Android listview scroll listener last item position

i have listview witch contain 40 items and i want to write scroll listener method.i mean.when scroll position will be last item position(40th item) i would to show Toast message.
i wrote some code .i can show Toast message in a last item position,but then when i scroll up i again can show Toast message
this is a my source
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (list.getLastVisiblePosition() == totalItemCount - 1
) {
Toast.makeText(getActivity(), "Last", Toast.LENGTH_SHORT)
.show();
try {
} catch (Exception e) {
}
}
}
});
how i can change code to can show toast message only last item position?
thanks
Try this Hope it helps ..
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
//your Toast
}
}
This a detailed code for scroll listener
ListView lv = (ListView)findViewById(R.id.list_view);
lv.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//Check if the last view is visible
if (++firstVisibleItem + visibleItemCount > totalItemCount) {
//load more content
}
}
});

Categories

Resources