Android Learning Notes-ScrollView (Scroll Bar)

Introduction to this section:

This section brings up the tenth of Android's basic UI controls: ScrollView, or we should call it a vertical scrollbar, corresponding to another horizontal scrollbar: HorizontalScrollView, which starts with a link to an official document: ScrollView We can see that the structure of the class is as follows:

Hey hey, it was originally a container for FrameLayout, but on top of that it added scrolling to allow more content to be displayed than it really is!

In addition, only one child element can be placed inside, either a single component or a complex hierarchy wrapped in a layout!

Generally, for cases where the display may be incomplete, we can cover the layout directly with a ScrollView or HorizontalScrollView!It's that simple ~!

Some requirements you may encounter

Okay, instead of deducting documents one by one, let's just say that there are some requirements you may encounter in the actual development:

Another typical problem is the nesting of ScrollView and ListView, which is explained in the ListView chapter ~

1. Scroll to the bottom:

We can use ScrollView directly to provide us with the fullScroll() method:

scrollView.fullScroll(ScrollView.FOCUS_DOWN); scroll to the bottom

scrollView.fullScroll(ScrollView.FOCUS_UP); scroll to the top

Also, be careful with asynchronous items when using this item, that is, after addView, it may not be displayed completely. If this method is called directly at this time, it may not be valid, so you need to write your own handler to update it~

Code example:

Design sketch:

Implementation code:

Simple layout, no paste, paste MainActivity.java directly

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_down;
    private Button btn_up;
    private ScrollView scrollView;
    private TextView txt_show;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
    }


    private void bindViews() {
        btn_down = (Button) findViewById(R.id.btn_down);
        btn_up = (Button) findViewById(R.id.btn_up);
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        txt_show = (TextView) findViewById(R.id.txt_show);
        btn_down.setOnClickListener(this);
        btn_up.setOnClickListener(this);

        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 100; i++) {
            sb.append("Ha-ha * " + i + "\n");
        }
        txt_show.setText(sb.toString());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_down:
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                break;
            case R.id.btn_up:
                scrollView.fullScroll(ScrollView.FOCUS_UP);
                break;
        }
    }
}

Of course, in addition to this method, you can use a more complex way of writing:

public static void scrollToBottom(final View scroll, final View inner) {
    Handler mHandler = new Handler();
    mHandler.post(new Runnable() {
        public void run() {
            if (scroll == null || inner == null) {
                return;
            }
            int offset = inner.getMeasuredHeight() - scroll.getHeight();
            if (offset < 0) {
                offset = 0;
            }
            scroll.scrollTo(0, offset);
        }
    });
}  

The scrollTo() parameter is x, y rolls to the corresponding x, y position in turn!

2. Set slider picture for scrolling

This is simpler: vertical slider: android:scrollbarThumbVertical
Horizontal slider: android:scrollbarThumbHorizontal

3. Hide the slider

Well, this one doesn't seem to be useful:

There are two methods: 1.android:scrollbars="none"
2.Java code settings: scrollview.setVerticalScrollBarEnabled(false);

4. Set the scrolling speed:

This does not give us a way to set it directly. We need to inherit ScrollView and rewrite a public void fling (int velocityY) method:

@Override
public void fling(int velocityY) {
    super.fling(velocityY / 2);    //Change speed to half of original
}

Summary of this section:

Okay, that's all you can think of with ScrollView, because it's not used much in general, it's usually just wrapped out directly. In addition, the most common questions are usually nested questions of ScrollView and ListView. ~If there's anything to add, thank you ~

 

Reference from article: http://www.runoob.com/w3cnote/android-tutorial-scrollview.html

Keywords: Android Java

Added by Jason28 on Wed, 26 Jun 2019 19:43:42 +0300