ListView comes to first position again after scrolling - android

Hi all I have listview in my application but after scrolling to bottom it again sets to first position.
listView = (ListView) findViewById(R.id.list);
start = listView.getFirstVisiblePosition();
listView.setSelectionFromTop(start, 0);
after removing following code it works fine but after scrolling setOnItemClickListener does not work.
listView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE )
{
listView.invalidateViews();
}
}
#Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {}
});
Any suggestion will be appreciated. Thanks in advance

Try this way,hope this will help you to solve your problem.
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) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if((lastInScreen == totalItemCount)){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
listView.smoothScrollToPosition(0);
}
},1000); // change (1000) value as per your requirement like : when list shown last item then after 1 second list scrolled to 0 position
}
}
});

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;
}
});

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) {
}
});
}

Need to hide a textView while scrolling a grid view

I'm trying to hide a textView while scrolling a grid view and make the same textview visible again when the user stops scrolling.
Can anyone help me fixing this issue..?
implement OnScrollListener and then inside the onScroll make the textView invisible
and in the OnScrollStateChanged method check if the scroll stopped and make it visibile again
private OnScrollListener mScrollListener = new OnScrollListener() {
#Override
public void onScrollStateChanged(ViewGroup view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
textView.setVisibility = View.VISIBLE;
}
#Override
public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
textView.setVisibility = View.INVISIBLE
}
};
You can use setVisibility in OnScrollListener
Example:
gridview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
textview.setVisibility(View.VISIBLE);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
textview.setVisibility(View.GONE);
}
});
Hope it helps.
gridView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
textview.setVisibility(View.VISIBLE);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
textview.setVisibility(View.GONE);
}
});
We use this code in my app and it work , I hope it will useful to you.
Try this code:
private OnScrollListener mScrollListener = new OnScrollListener() {
#Override
public void onScrollStateChanged(ViewGroup view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
tv.setVisibility(View.VISIBLE);
}
}
#Override
public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalount)
{
tv.setVisibility(View.INVISIBLE);
}
});
Try this:
Private OnScrollListener mScrollListener = new OnScrollListener() {
#Override
public void onScrollStateChanged(ViewGroup view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
mTextView.setVisibility = View.VISIBLE;
}
#Override
public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mTextView.setVisibility = View.INVISIBLE
}
};
You need to set this method on your gridview. it works for me. I am sure it will help you.
gridAdmin.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
text1.setVisibility = View.VISIBLE;
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
text1.setVisibility = View.INVISIBLE
}
});

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