1.ScrollView使用介绍
首先来看google官方对他的介绍,翻译过来就是可以滚动的用户布局容器,如果手机显示不下子布局,那么可以使用scrollView,当然谷歌也说NestedscrollView
已经提供了更好的用户体验,这个我们以后再详细总结下。
谷歌官方已经提示,不要在scrollView中添加RecyclerView
或者是ListView
布局,这样会引起不好的体验因为会有滑动冲突的问题出现。
另外,ScrollView的直接子View只能有一个。也就是说如果你的视图结构比较复杂,你需要一个标准的容器,如LinearLayout
、RelativeLayout
等。ScrollView只支持竖直滑动,水平滑动使HorizontalScrollView
。
先看布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/vertical_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
<include layout="@layout/scollview_item" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
scrollviewItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
>
<ImageView
android:layout_height="64dp"
android:layout_width="64dp"
android:src="@drawable/vector_drawable_right"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="ScollViewItem"
android:textSize="21dp"
android:gravity="left|center_vertical"
android:layout_gravity="center"
/>
</LinearLayout>
效果图:
我是在scrollView中添加了一个LinearLayout,默认是垂直。
scrollView的大小是撑满了父布局。
在这里说一下,ScrollView是一种特殊的布局。当ScrollView的内容大于他本身的size的时候,ScrollView会自动添加滚动条,并可以竖直滑动。如果想要在子View高度小于ScrollView高度时占满,必需在xml里为ScrollView加上Android:fillViewport="true"
,这样子View小于ScrollView高度时就会占满父View.
在说下面之前,再介绍下scollview的其他特点:
scollview在滑动的时候默认是有滑动条的
android:scrollbars="vertical"//垂直滚动条
android:fadeScrollbars="false"//不隐藏
- 如果只添加
android:scrollbars="vertical"
则只有在上下滑动页面的时候才会有垂直滚动条,停止滑动则滚动条消失。
如果不想显示滚动条:
android:scrollbars="none"
不想显示ScrollView拉到尽头(顶部、底部)出现的阴影效果,适用于2.3及以上的 否则不用设置.
android:overScrollMode="never"
2.Android ScrollView监听滑动到顶部和底部:
有需求才会有市场!
需求:典型的就是滚动到底部触发自动加载操作
整体思路:
1.自定义View
2.bottom_position - (getHeight() + getScrollY()) -getPaddingBottom
3.添加滑动到底部条件的回调监听
在这里简单说一下这几个方法:
getHeight():
获得当前view的高度
getScrollX()
:Return the scrolled left position of this view
意思是返回当前滑动View左边界的位置,其实获取的值就是这块幕布在窗口左边界时的x坐标,而幕布上面哪个点是原点(0,0)呢?就是初始化时内容显示的位置。具体的可以看下面的这张图。
getScrollY()
与这个类似,是获取窗口上边界时的y坐标,当然这两个值会随着滑动而变化。
这里如果在实践的时候log一下就会更清楚!
1.CustomScrollView.java
package com.example.myscollview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
OnBottomReachedListener mListener;
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//重点:
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY())) - view.getPaddingBottom();
Log.e("Bottom", String.valueOf(view.getBottom()));
Log.e("Height", String.valueOf(getHeight()));
Log.e("ScrollY", String.valueOf(getScrollY()));
Log.e("PaddingBottom", String.valueOf(view.getPaddingBottom()));
if (diff <= 0 && mListener != null) {
mListener.onBottomReached();
}
super.onScrollChanged(l, t, oldl, oldt);
}
// Getters & Setters
public OnBottomReachedListener getOnBottomReachedListener() {
return mListener;
}
public void setOnBottomReachedListener(
OnBottomReachedListener onBottomReachedListener) {
mListener = onBottomReachedListener;
}
//Event listener.
public interface OnBottomReachedListener {
public void onBottomReached();
}
}
2.MainActivity.java
package com.example.myscollview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CustomScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = findViewById(R.id.vertical_scroll_view);
scrollView.setOnBottomReachedListener(new CustomScrollView.OnBottomReachedListener() {
@Override
public void onBottomReached() {
Toast.makeText(MainActivity.this, "Has reached the bottom!", Toast.LENGTH_SHORT).show();
}
});
}
}
3.activity_main.xml
<com.example.myscollview.CustomScrollView
android:id="@+id/vertical_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/scollview_item" />
此处省略。。。。。。。。。。。
</LinearLayout>
</com.example.myscollview.CustomScrollView>
肯定要结果图呀!!
上面说了怎么监听,下面再来说说怎么在某条件下滑动到顶部和底部
3.ScrollView滑动到顶部和底部
需求比如说点击某个btn自动滑动到顶部/底部
Android ScrollView滑动到顶部和底部:
- Scrollview禁止滑动:
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}});
- 可滑动
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}});
滑动到底部
scrollView.post(new Runnable() {
@Override
public void run() {
//滑动底部
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}});
滑动到顶部
scrollView.post(new Runnable() {
@Override
public void run() {
//滑动顶部
scrollView.fullScroll(ScrollView.FOCUS_UP);
}});
滑动到某个位置
scrollView.post(new Runnable() {
@Override
public void run() {
int offset = 100;//偏移值
scrollView.smoothScrollTo(0, offset);
}});
scrollview 遇到的坑:
android scrollview 滑不到最下面 ,在scrollview子view里面设置layout_margin之后就滑不到最下面,目前有效的解决办法不要在子view里面设置,在子view的子view里面设置。
参考:
ScrollView监听滑动到底部
Scrollview控制滑动到底部
最后还没完,来加个需求吧:ScrollView阻尼回弹效果!!下回再总结完善