2

My problem is that I have a ScrollView within a fragment, and when this fragment is swapped into the container, the ScrollView is initially in the middle (it seems to be scrolled to a ListView I have inside of the ScrollView). I have tried using:

myScrollView.scrollTo(0, 0);

and

mainScrollView.fullScroll(ScrollView.FOCUS_UP);

but this doesn't do anything. When I include these in my fragment's onCreateView method, this line executes, but when the layout becomes visible on the phone it is still scrolled to the middle.

I'll include the XML code here:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.johnnelson.tablayout.PingViewFragment">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pingScrollView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_above="@+id/wallPostInput"
    android:padding="0dp">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:longClickable="false"
        android:id="@+id/pingViewRelativeLayout"
        android:padding="0dp">

        <com.parse.ParseImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/pingProfileImage"
            android:scaleType="centerCrop"
            android:cropToPadding="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="15dp"
            android:layout_marginStart="21dp"
            android:translationZ="1dp"
            android:layout_marginTop="120dp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:text="attending"
            android:id="@+id/attendingButton"
            android:layout_alignBottom="@+id/pingProfileImage"
            android:layout_centerHorizontal="true"
            android:width="100dp"
            android:height="8dp"
            android:translationZ="1dp"
            android:layout_alignParentEnd="false"
            android:textSize="9dp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="attend"
            android:id="@+id/attendButton"
            android:layout_alignBottom="@+id/attendingButton"
            android:layout_toRightOf="@+id/attendingButton"
            android:layout_toEndOf="@+id/attendingButton"
            android:layout_marginLeft="15dp"
            android:layout_alignTop="@+id/attendingButton"
            android:width="100dp"
            android:height="8dp"
            android:translationZ="1dp"
            android:textSize="9dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:id="@+id/pingTitleTextView"
            android:layout_below="@+id/pingProfileImage"
            android:layout_alignLeft="@+id/pingProfileImage"
            android:layout_alignStart="@+id/pingProfileImage"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Time"
            android:id="@+id/pingStartTimeTextView"
            android:layout_below="@+id/pingTitleTextView"
            android:layout_alignRight="@+id/pingTitleTextView"
            android:layout_alignEnd="@+id/pingTitleTextView"
            android:layout_alignLeft="@+id/pingTitleTextView"
            android:layout_alignStart="@+id/pingTitleTextView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="End Time"
            android:id="@+id/pingEndTimeTextView"
            android:layout_below="@+id/pingStartTimeTextView"
            android:layout_alignLeft="@+id/pingStartTimeTextView"
            android:layout_alignParentRight="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/pingLocationNameTextView"
            android:textColor="@color/PING_HYPERLINK_BLUE"
            android:layout_below="@+id/pingEndTimeTextView"
            android:layout_alignLeft="@+id/pingEndTimeTextView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Description"
            android:id="@+id/pingDescriptionTextView"
            android:layout_below="@+id/pingLocationNameTextView"
            android:layout_alignLeft="@+id/pingLocationNameTextView"
            android:layout_alignParentRight="true"
            android:maxHeight="100dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wallPosts"
            android:id="@+id/wallPostsTextView"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/pingDescriptionTextView"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="10dp"
            android:textSize="10sp"
            android:background="@color/PING_WHITE" />

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="500dp"
            android:id="@+id/wallPostListView"
            android:nestedScrollingEnabled="true"
            android:layout_alignParentRight="false"
            android:layout_alignParentLeft="false"
            android:layout_alignParentBottom="true"
            android:minHeight="500dp"
            android:layout_below="@+id/wallPostsTextView" />

        <view
            android:layout_width="match_parent"
            android:layout_height="95dp"
            class="android.support.v4.view.ViewPager"
            android:id="@+id/pingViewPager"
            android:layout_alignParentLeft="false"
            android:layout_alignParentStart="false"
            android:translationZ="0dp"
            android:layout_above="@+id/attendingButton"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="false"
            android:padding="0dp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:paddingRight="0dp"
            android:paddingBottom="0dp"
            android:layout_alignWithParentIfMissing="false"/>

    </RelativeLayout>
</ScrollView>


<include
    layout="@layout/submit_wall_post"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:id="@+id/wallPostInput" />

</RelativeLayout>

There's a lot of stuff going on, so my worry is that some other view may be stealing focus or not allowing the ScrollView to scroll to top? The ScrollView I need scrolled to the top is @+id/pingScrollView. The included layout at the bottom is a chat bar.

Been stuck on this simple issue for days. I really appreciate any help!


Note: I have read a bunch of questions regarding my problem, such as this and this, but when I add any of that code (which seems like the correct, simplest solution) to my fragment, in either the onCreateView(), onStart(), or onResume() methods, nothing happens and the ScrollView remains scrolled to the middle.

Community
  • 1
  • 1
ThePartyTurtle
  • 2,276
  • 2
  • 18
  • 32
  • Does `smoothScrollTo(0,0)` work? I always have better luck using that. – kris larson Jul 28 '15 at 00:29
  • @Kris I'll check it out real quick! – ThePartyTurtle Jul 28 '15 at 15:40
  • @krislarson thanks for the suggestion, but this didn't work either. The code executes but the screen still appears scrolled all the way down. Just a reminder, this is in my fragments onCreateView method, I feel like that could be a problem. – ThePartyTurtle Jul 28 '15 at 16:40
  • posting Runnable with fullScroll() didn't work? In the SO links you referenced, some people indicated that that worked for them. – kris larson Jul 28 '15 at 16:48
  • 1
    @krislarson yea posting a runnable didn't work either :/. But again, the runnable executed. When the fragment appears however, the ScrollView remains scrolled to the ListView (wallPostListView in XML). I'm not sure if I'm calling smoothScrollTo in the wrong place or if there's something about my ListView that is causing it to be the default scroll position but I'm stumped. – ThePartyTurtle Jul 28 '15 at 17:15
  • Did you find a solution to this problem? – Allinone51 Sep 27 '16 at 09:46

0 Answers0