6

I've got an App with a LinearLayout to which rectangular fragments are added below each other over time from top to bottom.

At some point the screen is full and the new fragments are "hidden" outside the view of the screen. What I'd like to do then is to make the App scroll to bottom every time a new fragment is added.

A fragment is added everytime a button in the activity is pushed or a checkbox in the above fragment is checked. So I've tried the following in the onClickListeners and in the onCreateView of the fragments:

        fm.beginTransaction().add(R.id.activity_uhf_layout, fragment).commit(); //Adding fragment



        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0, scrollView.getBottom());
            }});

This works sometimes, the fragment is added and the screen scrolls down, but its like 1/8 times.

I've tried other things such as:

        scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.scrollTo(0, scrollView.getBottom());
            }
        });

Which basically gives the same result and I've tried without the Runnable post, which doesn't work at all.

The result I'm getting is that the layout is only scrolled down to the bottom of the fragment which is already present and not all the way down to the end of the new fragment which has just been added by the fragmentmanager.

To me it seems that even though the fragment is added, the LayoutManager hasn't really "detected it" or something similar, but how to get around it, I'm not really sure about.

  • Have you tried [this](http://stackoverflow.com/a/4294319/4428462) ? You might need to do something like `listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL)` and `listView.setStackFromBottom(true);`. – Jonas Czech May 13 '15 at 10:48
  • I haven't but since I'm not using Listview for my fragments, I don't really think it would help. However, it might just be the only solution to go from the LinearLayout to using ListView. – HANSEMINATOR May 13 '15 at 12:06
  • "_got an App with a listview_" Ah, You're not using an actual ListView, it seems. I'd put my fragments inside a ListView, instead of using a ScrollView. Since you're using ScrollView, try [this](http://stackoverflow.com/a/13860258/4428462), using `scrollview.fullScroll(ScrollView.FOCUS_DOWN);` in a runnable. – Jonas Czech May 13 '15 at 12:18
  • Ha. Total slip up. Am using a LinearLayout in vertical orientation. – HANSEMINATOR May 13 '15 at 13:55

1 Answers1

0

You could also try doing this.

fm.executePendingTransactions();
runOnUiThread(new Runnable() {
            @Override
            public void run() {
//Linearlayout is the layout the fragments are being added to.
View view = linearLayout.getChildAt(linearLayout.getChildCount()-1);
scrollView.scrollTo(0, view.getY() + view.getHeight());
}});

I don't know that the executePendingTransactions() is necessary, but if the view has been added, then that should work.

charliebeckwith
  • 1,449
  • 11
  • 29