-1

I want a whatsapp like download image feature if i download an image and when download complete the list position does not change.Every thing is working fine but when download complete the listview scrolls to bottom.

Nisha
  • 45
  • 8
  • This question has been addressed here: http://stackoverflow.com/a/7032341/1394695 – anti_gone Jul 19 '16 at 13:36
  • Just put the scrollMyListViewToBottom() method from my link in the appropriate place (e.g. some callback method). – anti_gone Jul 19 '16 at 13:45
  • @Nisha Every time u will getting new data in array list ?? Or when user can scroll down that a time user can getting new data every time? so u can use PullToReferesh Lib. – Hardik Parmar Jul 20 '16 at 12:40
  • I have a list view which can contains text or image(some static image) , and on image i have a download button , when i click on download button image is download and its local path is saved in database. If i call adapter.notifyDataSetChanged(); , but when i scroll the listview the image just changed to default pic. or if i use list.setAdapter(adapter); then the listview scrolls to bottom. – Nisha Jul 20 '16 at 13:19
  • I want that the image should be change but list should not scrolls to bottom. like a whatsapp if i download an image when download complete the list position does not change. – Nisha Jul 20 '16 at 13:24
  • already used android:transcriptMode="alwaysScroll" android:stackFromBottom="true" , but it does not works. – Nisha Jul 20 '16 at 13:32
  • using chatList.setSelectionFromTop(index,top); i have resolved my issue. – Nisha Jul 21 '16 at 10:11
  • Possible duplicate of [Retaining position in ListView after calling notifyDataSetChanged](http://stackoverflow.com/questions/8276128/retaining-position-in-listview-after-calling-notifydatasetchanged) – Kushan Jul 21 '16 at 10:32
  • chatList.setSelectionFromTop(index,top); will works. http://stackoverflow.com/questions/8276128/retaining-position-in-listview-after-calling-notifydatasetchanged – Nisha Jul 26 '16 at 05:20

2 Answers2

0

To update the listview contents, Call adapter.notifydatasetchanged(). Where adapter is the one which is set for your litview.

Hari Krishnan
  • 4,758
  • 8
  • 31
  • 47
0

Try overriding this method to your own use. I have modified it to suit my custom layout manager for a recycler view but the same can be done for a list view. You can find loads of tutorials on google.

@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
                                   RecyclerView.State state, final int position) {

    LinearSmoothScroller smoothScroller =
            new LinearSmoothScroller(mContext) {

                //This controls the direction in which smoothScroll looks
                //for your view
                @Override
                public PointF computeScrollVectorForPosition
                (int targetPosition) {
                    return FoodFragmentCustomLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }

                //This returns the milliseconds it takes to
                //scroll one pixel.
                @Override
                protected float calculateSpeedPerPixel
                (DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH/displayMetrics.densityDpi;
                }
            };

    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
Kushan
  • 5,275
  • 2
  • 27
  • 41
  • put the layout inside a CoordinatorLayout for it to work properly ** – Kushan Jul 19 '16 at 13:41
  • i want a whatsapp like download image feature if i download an image and when download complete the list position does not change.Every thing is working fine but when download complete the listview scrolls to bottom. – Nisha Jul 20 '16 at 13:26
  • when the download completes, call smoothScrollToPosition(positionyouwant) – Kushan Jul 20 '16 at 13:29
  • i have used following on itemclicklistener of listview :- adapter = new ChatMessageAdapter(mcon, messages); chatList.setAdapter(adapter); chatList.setItemChecked(arg2, true); if (arg2 <= chatList.getFirstVisiblePosition() || arg2 >= chatList.getLastVisiblePosition()) { chatList.smoothScrollToPosition(arg2); } But again the scrollview moves to bottom. – Nisha Jul 21 '16 at 04:37
  • chatList.smoothScrollToPositionFromTop(arg2, 0); works but it looks very weird as fluctuation in listview when download complete . it should be at the same position where it is before downloading. – Nisha Jul 21 '16 at 04:59
  • chatList.setSelectionFromTop(index,top); will works. – Nisha Jul 21 '16 at 10:12
  • I am not aware of that method, sorry :( Anyway if that helps your cause better, use it :) If scrolling to zero looks weird, you can save the list size before updating and then update data set. Then immediately set smoothscroll to position to the saved list size and it should get you to where you were previously. – Kushan Jul 21 '16 at 10:30
  • Yea checked it out, mList.setSelectionFromTop(index, top); will work :) Use my method for some other cause. It's a cool way to take someone to what they are searching ;) i use it for that :D – Kushan Jul 21 '16 at 10:33
  • http://stackoverflow.com/questions/8276128/retaining-position-in-listview-after-calling-notifydatasetchanged the perfect answer you want :) – Kushan Jul 21 '16 at 10:34
  • Thanks for the help. – Nisha Jul 22 '16 at 11:24
  • today , while testing the app i was again getting error of image changed to default image when scroll in list. mList.setSelectionFromTop(index, top) stops working now – Nisha Jul 29 '16 at 10:15