Android之ScrollView详解

1.ScrollView使用介绍

在这里插入图片描述
首先来看google官方对他的介绍,翻译过来就是可以滚动的用户布局容器,如果手机显示不下子布局,那么可以使用scrollView,当然谷歌也说NestedscrollView已经提供了更好的用户体验,这个我们以后再详细总结下。

谷歌官方已经提示,不要在scrollView中添加RecyclerView或者是ListView布局,这样会引起不好的体验因为会有滑动冲突的问题出现。
另外,ScrollView的直接子View只能有一个。也就是说如果你的视图结构比较复杂,你需要一个标准的容器,如LinearLayoutRelativeLayout等。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阻尼回弹效果!!下回再总结完善

  • 31
    点赞
  • 127
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值