ScrollView (scroll bar)
Introduction to this section:
This section brings the tenth of Android basic UI controls: ScrollView (scroll bar), or we should Call him Vertical scroll bar, corresponding to another horizontal scroll bar: HorizontalScrollView, let’s first send the official document Link: ScrollView, we can see that the class structure is as follows:
Hehe, it turns out to be a FrameLayout container, but on the basis of it Added scrolling, allowing more content to be displayed than is actually possible!
In addition, only one child element can be placed inside, which can be a single component, or a complex hierarchical structure wrapped in a layout!
Generally, for situations where the display may not be complete, we can directly put one on the outer layer of the layout: ScrollView or HorizontalScrollView! It’s that simple~!
Some requirements that may be encountered
Okay, let’s not deduct the documents one by one, let’s just talk about some of the requirements that may be encountered in actual development:
Another very typical problem is: the nesting problem of ScrollView and ListView. This is put in the chapter of ListView. Let’s explain~
1. Scroll to the bottom:
We can directly use the:fullScroll() method provided by ScrollView:
scrollView.fullScroll(ScrollView.FOCUS_DOWN);Scroll to the bottom
scrollView.fullScroll(ScrollView.FOCUS_UP);Scroll to the top
In addition, when using this thing, be careful about asynchronous things, that is, after addingView, it may not be displayed completely. If you call this method directly at this time, it may be invalid, so you need to write your own handler to update it~
Code example:
Rendering:
Implementation code:
The layout is relatively simple, so I won’t post it here , directly paste MainActivityMainActivity.java
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("呵呵 * " + 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 also use another more complicated way of writing:
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
public void run() {
if (scroll == null || inner == null) {
if (offset < 0 ) {
offset = 0;
#scrollTo
() parameters are x and y in order. Scroll to the corresponding x and y position!
2. Set the scrolling slider image
This is simpler:
scrollbarThumbVertical
HorizontalDirection slider: android:
scrollbarThumbHorizontal
3. Hide sliderOkay, this one It seems to be of no use:
There are two methods: 1.android:scrollbars="none"2.Java code setting: scrollview.setVerticalScrollBarEnabled(false);
4.Set scroll speed:
This does not provide us with a method that can be set directly. We need to inherit ScrollView ourselves and then rewrite it. Method of public void fling (int velocityY):
@Override public void fling(int velocityY) { super.fling(velocityY / 2); //速度变为原来的一半 }
Okay, there are only so many ScrollView things that I can think of, because this is usually used It's not much, usually just put it on the outside. In addition, the most common problem is usually the nesting problem of ScrollView and ListView. If you have anything to add, please feel free to ask, thank you~
Summary of this section: