Android java Listview scroll automatic a slow time












0















I have a listview with some elements, I have to make this list scroll automatically, with a slow scroll.



When he reaches the last element he stops.



If new items are added in the queue, then the listview must restart, until it reaches the last element again.



Any suggestions?










share|improve this question


















  • 2





    Still ListView? Use RecyclerView for better performance.

    – Piyush
    Nov 16 '18 at 7:04











  • add your code and also used listview instand of recyclerview. recyclerview it is latest.

    – Android Team
    Nov 16 '18 at 7:04
















0















I have a listview with some elements, I have to make this list scroll automatically, with a slow scroll.



When he reaches the last element he stops.



If new items are added in the queue, then the listview must restart, until it reaches the last element again.



Any suggestions?










share|improve this question


















  • 2





    Still ListView? Use RecyclerView for better performance.

    – Piyush
    Nov 16 '18 at 7:04











  • add your code and also used listview instand of recyclerview. recyclerview it is latest.

    – Android Team
    Nov 16 '18 at 7:04














0












0








0


1






I have a listview with some elements, I have to make this list scroll automatically, with a slow scroll.



When he reaches the last element he stops.



If new items are added in the queue, then the listview must restart, until it reaches the last element again.



Any suggestions?










share|improve this question














I have a listview with some elements, I have to make this list scroll automatically, with a slow scroll.



When he reaches the last element he stops.



If new items are added in the queue, then the listview must restart, until it reaches the last element again.



Any suggestions?







java android listview android-listview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 7:02









PaulPaul

151210




151210








  • 2





    Still ListView? Use RecyclerView for better performance.

    – Piyush
    Nov 16 '18 at 7:04











  • add your code and also used listview instand of recyclerview. recyclerview it is latest.

    – Android Team
    Nov 16 '18 at 7:04














  • 2





    Still ListView? Use RecyclerView for better performance.

    – Piyush
    Nov 16 '18 at 7:04











  • add your code and also used listview instand of recyclerview. recyclerview it is latest.

    – Android Team
    Nov 16 '18 at 7:04








2




2





Still ListView? Use RecyclerView for better performance.

– Piyush
Nov 16 '18 at 7:04





Still ListView? Use RecyclerView for better performance.

– Piyush
Nov 16 '18 at 7:04













add your code and also used listview instand of recyclerview. recyclerview it is latest.

– Android Team
Nov 16 '18 at 7:04





add your code and also used listview instand of recyclerview. recyclerview it is latest.

– Android Team
Nov 16 '18 at 7:04












3 Answers
3






active

oldest

votes


















0














I suggest, thought your adapter implemented in effective way. so this code is just scrolls listview



you need to try another values of variables



final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()

final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother

final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling

listView.post(new Runnable() {
@Override
public void run() {
new CountDownTimer(totalScrollTime, scrollPeriod ) {
public void onTick(long millisUntilFinished) {
listView.scrollBy(0, heightToScroll);
}

public void onFinish() {
//you can add code for restarting timer here
}
}.start();
}
});





share|improve this answer
























  • It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

    – Paul
    Nov 16 '18 at 7:37











  • you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

    – Aniruddh Parihar
    Nov 16 '18 at 7:43













  • But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

    – Paul
    Nov 16 '18 at 7:47











  • In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

    – Paul
    Nov 16 '18 at 7:57



















0














use the below method to scroll your listview. for example:



scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);


method:



private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){

ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
objectAnimator.setStartDelay(delay);
objectAnimator.start();
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {}

@Override
public void onAnimationEnd(Animator animation) {

}

@Override
public void onAnimationCancel(Animator animation) {}

@Override
public void onAnimationRepeat(Animator animation) {}
});
}


and if new item added to adapter and need to again go to top of listview and again come back to bottom.you can use below code to do this:



myAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
activityListview.setSelection(0);
scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
}
});


To calculate the height of listview use below method



public int getTotalHeightofListView(ListView listView) {

ListAdapter mAdapter = listView.getAdapter();

int totalHeight = 0;

for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);

mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

totalHeight += mView.getMeasuredHeight();

}
return totalHeight;

}





share|improve this answer


























  • I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

    – Paul
    Nov 16 '18 at 13:04











  • Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

    – Paul
    Nov 16 '18 at 13:05











  • In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

    – Paul
    Nov 16 '18 at 13:05











  • I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

    – kumud kala
    Nov 19 '18 at 5:20











  • It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

    – Paul
    Nov 19 '18 at 20:23



















0














Use listview smoothScrollToPosition(int position) function.



Please have a look on this post also
https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332949%2fandroid-java-listview-scroll-automatic-a-slow-time%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I suggest, thought your adapter implemented in effective way. so this code is just scrolls listview



    you need to try another values of variables



    final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()

    final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother

    final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling

    listView.post(new Runnable() {
    @Override
    public void run() {
    new CountDownTimer(totalScrollTime, scrollPeriod ) {
    public void onTick(long millisUntilFinished) {
    listView.scrollBy(0, heightToScroll);
    }

    public void onFinish() {
    //you can add code for restarting timer here
    }
    }.start();
    }
    });





    share|improve this answer
























    • It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

      – Paul
      Nov 16 '18 at 7:37











    • you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

      – Aniruddh Parihar
      Nov 16 '18 at 7:43













    • But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

      – Paul
      Nov 16 '18 at 7:47











    • In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

      – Paul
      Nov 16 '18 at 7:57
















    0














    I suggest, thought your adapter implemented in effective way. so this code is just scrolls listview



    you need to try another values of variables



    final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()

    final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother

    final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling

    listView.post(new Runnable() {
    @Override
    public void run() {
    new CountDownTimer(totalScrollTime, scrollPeriod ) {
    public void onTick(long millisUntilFinished) {
    listView.scrollBy(0, heightToScroll);
    }

    public void onFinish() {
    //you can add code for restarting timer here
    }
    }.start();
    }
    });





    share|improve this answer
























    • It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

      – Paul
      Nov 16 '18 at 7:37











    • you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

      – Aniruddh Parihar
      Nov 16 '18 at 7:43













    • But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

      – Paul
      Nov 16 '18 at 7:47











    • In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

      – Paul
      Nov 16 '18 at 7:57














    0












    0








    0







    I suggest, thought your adapter implemented in effective way. so this code is just scrolls listview



    you need to try another values of variables



    final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()

    final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother

    final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling

    listView.post(new Runnable() {
    @Override
    public void run() {
    new CountDownTimer(totalScrollTime, scrollPeriod ) {
    public void onTick(long millisUntilFinished) {
    listView.scrollBy(0, heightToScroll);
    }

    public void onFinish() {
    //you can add code for restarting timer here
    }
    }.start();
    }
    });





    share|improve this answer













    I suggest, thought your adapter implemented in effective way. so this code is just scrolls listview



    you need to try another values of variables



    final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()

    final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother

    final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling

    listView.post(new Runnable() {
    @Override
    public void run() {
    new CountDownTimer(totalScrollTime, scrollPeriod ) {
    public void onTick(long millisUntilFinished) {
    listView.scrollBy(0, heightToScroll);
    }

    public void onFinish() {
    //you can add code for restarting timer here
    }
    }.start();
    }
    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 7:14









    Aniruddh PariharAniruddh Parihar

    2,21911129




    2,21911129













    • It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

      – Paul
      Nov 16 '18 at 7:37











    • you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

      – Aniruddh Parihar
      Nov 16 '18 at 7:43













    • But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

      – Paul
      Nov 16 '18 at 7:47











    • In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

      – Paul
      Nov 16 '18 at 7:57



















    • It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

      – Paul
      Nov 16 '18 at 7:37











    • you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

      – Aniruddh Parihar
      Nov 16 '18 at 7:43













    • But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

      – Paul
      Nov 16 '18 at 7:47











    • In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

      – Paul
      Nov 16 '18 at 7:57

















    It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

    – Paul
    Nov 16 '18 at 7:37





    It seems to work, but strange things happen. I'll explain I have 21 items. The elements 20,19,18,17 flow exclusively. All others do not appear in scrolling. Another problem is that being that they make a listView.scrollBy when the last items arrive they disappear. The thing I would like to do is block it, do not let it flow anymore until there are no other elements. Something like this: i.imgur.com/NXDjShM.jpg

    – Paul
    Nov 16 '18 at 7:37













    you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

    – Aniruddh Parihar
    Nov 16 '18 at 7:43







    you can check the last visible item of listView and interrupt the timer to stop Scrolling and if you want infinite scroll the you can restart it. and also read the comments in code for better implementation.

    – Aniruddh Parihar
    Nov 16 '18 at 7:43















    But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

    – Paul
    Nov 16 '18 at 7:47





    But it does not solve at the moment because only 4 elements of 21 flow. I also tried to use: listView.setSelection (adapter.getCount () - 1); But it does not work.

    – Paul
    Nov 16 '18 at 7:47













    In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

    – Paul
    Nov 16 '18 at 7:57





    In the meantime I'm trying to think if we can add the following feature: pastebin.com/QiiXtgT2 That when I go up the listview it stops, if I go down she continues. But it seems that it does not work properly or I'm wrong maybe something.

    – Paul
    Nov 16 '18 at 7:57













    0














    use the below method to scroll your listview. for example:



    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);


    method:



    private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){

    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
    objectAnimator.setStartDelay(delay);
    objectAnimator.start();
    objectAnimator.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
    });
    }


    and if new item added to adapter and need to again go to top of listview and again come back to bottom.you can use below code to do this:



    myAdapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
    super.onChanged();
    activityListview.setSelection(0);
    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
    }
    });


    To calculate the height of listview use below method



    public int getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
    View mView = mAdapter.getView(i, null, listView);

    mView.measure(
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    totalHeight += mView.getMeasuredHeight();

    }
    return totalHeight;

    }





    share|improve this answer


























    • I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

      – Paul
      Nov 16 '18 at 13:04











    • Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

      – Paul
      Nov 16 '18 at 13:05











    • In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

      – Paul
      Nov 16 '18 at 13:05











    • I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

      – kumud kala
      Nov 19 '18 at 5:20











    • It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

      – Paul
      Nov 19 '18 at 20:23
















    0














    use the below method to scroll your listview. for example:



    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);


    method:



    private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){

    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
    objectAnimator.setStartDelay(delay);
    objectAnimator.start();
    objectAnimator.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
    });
    }


    and if new item added to adapter and need to again go to top of listview and again come back to bottom.you can use below code to do this:



    myAdapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
    super.onChanged();
    activityListview.setSelection(0);
    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
    }
    });


    To calculate the height of listview use below method



    public int getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
    View mView = mAdapter.getView(i, null, listView);

    mView.measure(
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    totalHeight += mView.getMeasuredHeight();

    }
    return totalHeight;

    }





    share|improve this answer


























    • I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

      – Paul
      Nov 16 '18 at 13:04











    • Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

      – Paul
      Nov 16 '18 at 13:05











    • In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

      – Paul
      Nov 16 '18 at 13:05











    • I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

      – kumud kala
      Nov 19 '18 at 5:20











    • It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

      – Paul
      Nov 19 '18 at 20:23














    0












    0








    0







    use the below method to scroll your listview. for example:



    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);


    method:



    private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){

    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
    objectAnimator.setStartDelay(delay);
    objectAnimator.start();
    objectAnimator.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
    });
    }


    and if new item added to adapter and need to again go to top of listview and again come back to bottom.you can use below code to do this:



    myAdapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
    super.onChanged();
    activityListview.setSelection(0);
    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
    }
    });


    To calculate the height of listview use below method



    public int getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
    View mView = mAdapter.getView(i, null, listView);

    mView.measure(
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    totalHeight += mView.getMeasuredHeight();

    }
    return totalHeight;

    }





    share|improve this answer















    use the below method to scroll your listview. for example:



    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);


    method:



    private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){

    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
    objectAnimator.setStartDelay(delay);
    objectAnimator.start();
    objectAnimator.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
    });
    }


    and if new item added to adapter and need to again go to top of listview and again come back to bottom.you can use below code to do this:



    myAdapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
    super.onChanged();
    activityListview.setSelection(0);
    scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
    }
    });


    To calculate the height of listview use below method



    public int getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
    View mView = mAdapter.getView(i, null, listView);

    mView.measure(
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    totalHeight += mView.getMeasuredHeight();

    }
    return totalHeight;

    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 '18 at 12:35









    Paul

    151210




    151210










    answered Nov 16 '18 at 8:08









    kumud kalakumud kala

    416




    416













    • I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

      – Paul
      Nov 16 '18 at 13:04











    • Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

      – Paul
      Nov 16 '18 at 13:05











    • In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

      – Paul
      Nov 16 '18 at 13:05











    • I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

      – kumud kala
      Nov 19 '18 at 5:20











    • It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

      – Paul
      Nov 19 '18 at 20:23



















    • I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

      – Paul
      Nov 16 '18 at 13:04











    • Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

      – Paul
      Nov 16 '18 at 13:05











    • In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

      – Paul
      Nov 16 '18 at 13:05











    • I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

      – kumud kala
      Nov 19 '18 at 5:20











    • It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

      – Paul
      Nov 19 '18 at 20:23

















    I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

    – Paul
    Nov 16 '18 at 13:04





    I tried the code, it does not seem to work properly. I also tried to replace: ListAdapter mAdapter = listView.getAdapter (); Directly with the adapter I use in the listview, nothing changes. In the video, I used the solution with: ListAdapter mAdapter = listView.getAdapter (); scrollMyTableView (listView, 11, 0, getTotalHeightofListView (listView), 10000); ObjectAnimator objectAnimator = ObjectAnimator.ofInt (v, "scrollY", fromY, toY) .setDuration (8000);

    – Paul
    Nov 16 '18 at 13:04













    Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

    – Paul
    Nov 16 '18 at 13:05





    Video: drive.google.com/file/d/1EKrU_3DeMdpzMR61z1zyPsxaDan0xjpv/view In the first part of the videos you can see, what I'm trying to achieve. The number of elements in the list are 21, [0-20]. As you can see from the video appear only 4 items from 20 to 17. If you pay attention you can see that the scroll in this case drops. As you can see if I click on the elements, all of them appear at the beginning of the element head 20.

    – Paul
    Nov 16 '18 at 13:05













    In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

    – Paul
    Nov 16 '18 at 13:05





    In the second part of the video the one on Youtube is what I would like to implement as a solution. There are a series of messages, elements of the listview. If I get on top I can see the old messages, if in the meantime are added new messages in the queue nothing happens, because I climbed up to see the old ones. If instead I redeem, then until the last element of the list and in the meantime that I am at the end, are added new messages in the listview the list scrolls. Minute [1.11-1.5] at the end of the video.

    – Paul
    Nov 16 '18 at 13:05













    I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

    – kumud kala
    Nov 19 '18 at 5:20





    I have seen the video. The above code can give exactly the solution of your problem. I have already implemented in my project. you need to just change according to your project code.

    – kumud kala
    Nov 19 '18 at 5:20













    It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

    – Paul
    Nov 19 '18 at 20:23





    It would be possible to see I do not know the video of what you have implemented in your project to understand how you did? Or some portion of code? These are days that I look for solutions, for how to solve these problems.

    – Paul
    Nov 19 '18 at 20:23











    0














    Use listview smoothScrollToPosition(int position) function.



    Please have a look on this post also
    https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition






    share|improve this answer




























      0














      Use listview smoothScrollToPosition(int position) function.



      Please have a look on this post also
      https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition






      share|improve this answer


























        0












        0








        0







        Use listview smoothScrollToPosition(int position) function.



        Please have a look on this post also
        https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition






        share|improve this answer













        Use listview smoothScrollToPosition(int position) function.



        Please have a look on this post also
        https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 8:06









        ShamsulShamsul

        224211




        224211






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332949%2fandroid-java-listview-scroll-automatic-a-slow-time%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine