Scroll at the bottom of the activity on onclick li

2019-08-31 23:25发布

问题:

In my Android app, I have a few views (Buttons, EditTexts etc.) not visible initially. These are visible only on a particular button click event. I wish to move the scrollview to the end of these views so that these are visible to the user.

The relevant portion of the layout file:

    <ScrollView...>

    <RelativeLayout android:id="@+id/RelDevDet"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            .
            .
            .
            <ImageButton android:id="@+id/ibRateApp"
                android:layout_toRightOf="@id/tvRateApp"
                android:layout_alignTop="@id/tvRateApp"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:background="@drawable/arrow_up" android:clickable="true"
                android:layout_marginRight="5dp"
                android:paddingBottom="10dp"
                 />
            <RatingBar android:id="@+id/rtApp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvRateApp"
                android:layout_marginLeft="5dp" android:visibility="gone"/>
            <EditText android:layout_width="fill_parent"
                android:id="@+id/etRateApp"
                android:layout_height="wrap_content" 
                android:lines="3"
                android:layout_below="@+id/rtApp"
                android:layout_marginRight="5dp"
                android:layout_alignLeft="@+id/tvDevWeb"
                android:visibility="gone" />
            <Button android:id="@+id/btSubRat" android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_below="@+id/etRateApp" android:layout_margin="5dp" android:text="Submit" android:visibility="gone"/>
        </RelativeLayout>
        </ScrollView>

The onclicklistener for the button is:

public void onClick(View v) {
if(v.getId()== R.id.ibRateApp){
            Log.d("amit","scroll end");
            rtApp.setVisibility(View.VISIBLE);
            etRateApp.setVisibility(View.VISIBLE);
            //How to get the scrollview move to the end of the page here??
        }
}

回答1:

scroll.fullScroll(View.FOCUS_DOWN);

This fixed it for me.



回答2:

use

 mScrollViewLayout.scrollTo(0, mScrollViewLayout.getMaxScrollAmount ());


回答3:

see the docs:

  • scrollTo(int, int)
  • smoothScrollTo(int, int)
  • smoothScrollBy(int, int)


回答4:

If the layout has a hidden view it's safe to use post delay so that the method will only be called after making the view visible so that it work scroll down to the end of the page by considering the hidden view

int SPLASH_TIME_OUT = 10;

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invisibleview.setVisibility(View.VISIBLE);
                scrollView.fullScroll(View.FOCUS_DOWN);
                if (invisibleview.getVisibility() == View.VISIBLE){

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // This method will be executed once the timer is over
                            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                        }
                    }, SPLASH_TIME_OUT);
                    scrollView.fullScroll(View.FOCUS_DOWN);
                }
            }
        });