The issue I am having is that the expandable list view is able to scroll down properly, but when I try scrolling up on the expandable list view, it starts the refresh. I am able to scroll up on the expandable list when it first swipe down, and then up again. I want to know if it is possible to only refresh the expandable list when they are at the top of the list or to properly scroll up. I know that a list view has a onScrollListener, so is there something similar to expandable lists? Any help or other suggestion are welcome
Thanks
swipeRefreshLayout.setEnabled(false);
expandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int topRowVerticalPosition = (expandableListView == null || expandableListView.getChildCount() == 0) ? 0 : expandableListView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
}
});
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean allow = false;
if(visibleItemCount>0) {
long packedPosition = elvMounters.getExpandableListPosition(firstVisibleItem);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
allow = groupPosition==0 && childPosition==-1 && elv.getChildAt(0).getTop()==0;
}
mSwipeRefreshLayout.setEnabled(allow);
}
Related
I am trying to apply swipe refresh in expandable listView, when the list view is scrolled to bottom and I try to scroll up, then it doesn't scroll up, the swipe refresh layout activates, How to solve this problem? any idea.!!!
swipeRefreshLayout.setEnabled(false);
expandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int topRowVerticalPosition = (expandableListView == null || expandableListView.getChildCount() == 0) ? 0 : expandableListView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
}
});
I have added a Button at the bottom of screen. It is a "Go To Top" button.
I want to make it hidden when user is at top of the list i.e on first row, and show it when he scrolls down to beyond second row.
I can add onscroll listner to the ListView. But I am not sure how to check for row number
Can anyone provide an example how to achieve it?
you can use setOnScrollListener
setOnScrollListener
Set the listener that will receive notifications every time the list scrolls.
sample code
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) {
fab.setVisibility(View.INVISIBLE);
}else {
fab.setVisibility(View.VISIBLE);
}
}
});
You can try something like this
Find out if ListView is scrolled to the bottom?
private int preLast;
// Initialization stuff.
yourListView.setOnScrollListener(this);
// ... ... ...
#Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
final int visibleItemCount, final int totalItemCount)
{
switch(lw.getId())
{
case R.id.your_list_id:
// Make your calculation stuff here. You have all your
// needed info from the parameters of this function.
// Sample calculation to determine if the last
// item is fully visible.
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount)
{
if(preLast!=lastItem)
{
//to avoid multiple calls for last item
Log.d("Last", "Last");
preLast = lastItem;
}
}
}
}
What I Want
I want to detect if the ListView have been scrolled "fully" to the bottom. By the word "fully", I mean that the last element of the list should be fully visible.
What I Did
This is what I did.
list.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
switch (view.getId()) {
case R.id.encList:
final int lastItem = firstVisibleItem + visibleItemCount;
if (lastItem == totalItemCount) {
if (preLast != lastItem) { //to avoid multiple calls for last item
Log.d("Log", "Last reached");
preLast = lastItem;
}
} else
preLast = lastItem;
}
}
});
The Problem
My code works to some extent but doesn't provide the exact result I wish for. The log gets printed when the last element is visible, but I want it to be printed only when the last element is fully visible.
What modifications do I need in this code?
Put this
if (list.getLastVisiblePosition() ==
list.getAdapter().getCount() -1 &&
list.getChildAt(list.getChildCount() - 1).getBottom() <= list.getHeight()){
//Scrolled end
}
in your onScroll
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.
When I combine both libraries in an activities, there is a problem.
When scroll to bottom is fine but when scroll to top there is a problem.
For example, the listview section in in middle and scroll up, the happen should be listview scroll up but it happened the actionbar refresh.
I suspected the both libraries's gesture overlap and causing this problem.
If I use them separately, both of them work brilliant.
Use the following snippet.This might Help you
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) {
if (listview.getChildCount() > 0 && listview.getChildAt(0).getTop() == 0
&& listview.getFirstVisiblePosition() == 0) {
mPullToRefreshLayout.setEnabled(true);
}else{
mPullToRefreshLayout.setEnabled(false);
}
}
});