1.使用时的注意点?
1.不用再ScrollView内部使用自带滚动条的控件或布局
2.ScrollView直接子View只能有一个
3.ScrollView只支持竖直滑动 如果想要横向滑动 HorizontalScrollView
2.具体案例实现
前台页面代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/and_di"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="滚动到底部" />
<Button
android:id="@+id/and_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="滚动到顶部" />
</LinearLayout>
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顶部"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文本9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="底部"/>
</LinearLayout>
</ScrollView>
后台Java代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ScrollView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button and_top=findViewById(R.id.and_top);
Button and_di=findViewById(R.id.and_di);
sv=findViewById(R.id.sv);
and_di.setOnClickListener(this);
and_top.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId()==R.id.and_di){
//滚动到底部
sv.fullScroll(ScrollView.FOCUS_DOWN);
}else if (view.getId()==R.id.and_top){
//滚动到顶部
sv.fullScroll(ScrollView.FOCUS_UP);
}
}
}