2.4.1 ScrollView (Scroll View)
Category Android Basic Tutorial
Introduction to This Section:
>
This section introduces the tenth basic UI control in Android: ScrollView (scroll bar), or perhaps we should call it a vertical scroll bar, with the corresponding horizontal scroll bar: HorizontalScrollView. Let's start with the official documentation link: ScrollView. We can see the class structure as follows:
Hehe, it turns out to be a FrameLayout container, but with scrolling added on top of it, allowing more content to be displayed than actually exists!
Additionally, only one child element can be placed inside, which can be a single component or a complex hierarchy wrapped in a layout!
Generally, for situations where the content might not be fully displayed, we can directly wrap the layout with an outer layer of: ScrollView or HorizontalScrollView! It's that simple~!
Some Possible Requirements
Well, let's not go through the documents one by one, but let's talk about some requirements that might be encountered in actual development:
There is also a very typical issue, which is the nesting problem of ScrollView and ListView. This will be discussed in the ListView section~
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
When using this, be careful with asynchronous things, that is, after addView, it may not have been displayed yet. If you call this method directly at this time, it may be ineffective. This requires writing a handler to update it yourself~
Code Example:
Effect Picture:
Implementation Code:
The layout is relatively simple, so I won't post it, and I'll directly post MainActivity MainActivity.java
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("Hehe * " + 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 a more complex writing method:
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);
}
});
}
scrollTo () parameters are x, y to scroll to the corresponding x, y position!
2. Set the Scroll Bar Image
>
This is even simpler: Vertical direction thumb: android:scrollbarThumbVertical Horizontal direction thumb: android:scrollbarThumbHorizontal
3. Hide the Scroll Bar
Well, this seems to be of no use:
>
There are two methods:
- android:scrollbars="none"
4. Set the Scroll Speed:
>
This does not provide a method that can be set directly. We need to inherit ScrollView ourselves and then override a public void fling (int velocityY) method:
@Override
public void fling(int velocityY) {
super.fling(velocityY / 2); //The speed is halved
}
Summary of This Section:
Well, that's all I can think of for ScrollView. It's not used very much in daily
2.4.1 ScrollView
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment - Bottom Navigation Bar Implementation (Method 1)
[5.2.2 Detailed Explanation of Fragment - Bottom Navigation Bar Implementation (Method 2)
8.3.4 Paint API - Detailed Explanation of Xfermode and PorterDuff (Part One)
8.3.5 Paint API - Detailed Explanation of Xfermode and PorterDuff (Part Two)
8.3.6 Paint API - Detailed Explanation of Xfermode and PorterDuff (Part Three)
8.3.7 Paint API - Detailed Explanation of Xfermode and PorterDuff (Part Four)
8.3.8 Paint API - Detailed Explanation of Xfermode and PorterDuff (Part Five)
8.3.14 Paint Enum/Constant Values and ShadowLayer Shadow Effect
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Encounter
8.4.4 Android Animation Collection - Property Animation - Revisited
[10.12 Sensor Special (3) - Accelerometer/Gyroscope Sensor