当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中使用ScrollView实现滑动到底部显示加载更多

Android中使用ScrollView实现滑动到底部显示加载更多

2019年07月24日  | 移动技术网移动技术  | 我要评论

这是效果

主要是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; 
 } 
 }); 
 } 
} 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网