关闭

ScorllView相关问题总结

标签: ScrollView滑动底部
135人阅读 评论(0) 收藏 举报
分类:

1、如何判断ScrollView是否已经滑动到了最底部?

public class MainActivity extends Activity implements OnTouchListener{

    private ScrollView scrollView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        scrollView = (ScrollView) findViewById(R.id.id_scroll);
        textView = (TextView) findViewById(R.id.id_texvview);

        scrollView.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_UP:

            Log.i("ScrollView", "getMeasuredHeight:"+scrollView.getChildAt(0).getMeasuredHeight());
            Log.i("ScrollView", "getScrollY:"+scrollView.getScrollY());
            Log.i("ScrollView", "getHeight:"+scrollView.getHeight());

            if (scrollView.getScrollY() == 0) {
                Toast.makeText(MainActivity.this, "已到顶部", Toast.LENGTH_SHORT).show();
            }
            if (scrollView.getChildAt(0).getMeasuredHeight() <= scrollView.getScrollY() + scrollView.getHeight()){
                //Log.i("ScrollView", scrollView.getChildAt(0).getMeasuredHeight());
                Log.i("ScrollView", "getMeasuredHeight:"+scrollView.getChildAt(0).getMeasuredHeight());
                Log.i("ScrollView", "getScrollY:"+scrollView.getScrollY());
                Log.i("ScrollView", "getHeight:"+scrollView.getHeight());

                Toast.makeText(MainActivity.this, "下拉加载更多", Toast.LENGTH_SHORT).show();
                textView.append(getResources().getString(R.string.scroll_content));
            }
            break;

        default:
            break;
        }       
        return false;
    }
}

上述代码中,scrollView.getScrollY() == 0 用于判断ScorllView是否滑动到了顶部,scrollView.getChildAt(0).getMeasuredHeight() <= scrollView.getScrollY() + scrollView.getHeight() 用于判断 ScorllView 是否滑动到了底部,其中 scrollView.getChildAt(0).getMeasuredHeight() 为 ScorllView 的内容高度,scrollView.getScrollY() 为滚动条滑动距离,scrollView.getHeight() 为 ScorllView 本身高度(同一机型该值为一固定值)。

参考
1、http://blog.sina.com.cn/s/blog_4ad7c2540102vrd8.html

2、http://www.cnblogs.com/popfisher/p/5709202.html

3、http://www.aichengxu.com/view/1424006

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26772次
    • 积分:733
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:10篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论