时间:2020-11-10 16:29:08 | 栏目:Android代码 | 点击:次
这是效果
主要是onTouchListener监听事件,监视什么时候滑到底部
同时要理解getMeasuredHeight和getHeight的区别
getMeasuredHeight:全部的长度 包括隐藏的
getHeight:在布局中展示出来的长度
布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ScrollView android:id="@+id/scrollview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="none" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView> <Button android:id="@+id/next" android:layout_gravity="bottom|center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible" android:text="点击加载更多" /> </FrameLayout>
MainActivity
package com.example.scrollview; import android.opengl.Visibility; import android.os.Bundle; import android.app.Activity; import android.support.v4.app.NotificationCompat.Action; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.Window; import android.widget.Button; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ScrollView scroll; private TextView text; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); scroll=(ScrollView) findViewById(R.id.scrollview); text=(TextView) findViewById(R.id.text); button=(Button) findViewById(R.id.next); text.setText(getResources().getString(R.string.lyric)); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub text.append(getResources().getString(R.string.lyric)); button.setVisibility(View.INVISIBLE); } }); scroll.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch(event.getAction()){ case MotionEvent.ACTION_MOVE:{ break; } case MotionEvent.ACTION_DOWN:{ break; } case MotionEvent.ACTION_UP:{ //当文本的measureheight 等于scroll滚动的长度+scroll的height if(scroll.getChildAt(0).getMeasuredHeight()<=scroll.getScrollY()+scroll.getHeight()){ button.setVisibility(View.VISIBLE); }else{ } break; } } return false; } }); } }