package com.example.scrollview; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; public class MainActivity extends Activity { private ScrollView sView; private LinearLayout linearLayout; private Button button; private TextView textView; private Handler myHandler = new Handler(); //当前滚屏的高度 private int sHeight ; private Intent myIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sView = (ScrollView)findViewById(R.id.scrollView); linearLayout = (LinearLayout)findViewById(R.id.linearLayout); button = (Button)findViewById(R.id.button); textView = (TextView)findViewById(R.id.textView); sHeight = sView.getHeight(); button.setOnClickListener(new myOnclickListener()); } /** * button事件: * 1.点击button在linearlayout中动态添加textView和button * 2.触发ScrollView的滚屏操作,滚动到最新添加的button处 * */ class myOnclickListener implements OnClickListener{ int index = 0; @Override public void onClick(View v) { LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); TextView addText = new TextView(MainActivity.this); addText.setText("textView"+index); Button addButton = new Button(MainActivity.this); addButton.setText("button"+index); addButton.setId(index++); linearLayout.addView(addText, linearLayoutParams); linearLayout.addView(addButton, linearLayoutParams); myHandler.post(mScrollToButton); } } private Runnable mScrollToButton = new Runnable() { @Override public void run() { //linearLayout的总高度 System.out.println("linearLayout.getMeasuredHeight() =>"+linearLayout.getMeasuredHeight()); //定位:位置 = 总高度 - 当前屏幕的高度 int off = linearLayout.getMeasuredHeight() - sHeight; if(off > 0){ //向下滚屏 sView.scrollBy(0, off); } } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
相关推荐
本文实例讲述了Android控件之ScrollView用法。分享给大家供大家参考。具体如下: ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳来显示的视图。 ScrollView只支持垂直滚动。 以下为案例 ...
ios scrollview循环 支持自定义点击事件,简单易于添加到自己的项目中!ARC
Android平台下使用ScrollView滚动效果代码,详细可以参见博客:http://www.cnblogs.com/plokmju/p/android_ScrollView.html
主要为大家详细介绍了Android垂直滚动控件ScrollView的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
超级列表SuperScrollview使用模板.unitypackage
继承Scrollview 实现视图的横向播放 可以直接用到手边的程序中
有时候项目在ListView中需要显示EditText,CheckBox等控件,这样就会出现一个问题,滑动时或失去焦点时编辑信息混乱。...这里提供了令一种解决方法,ScrollView,+LinearLayout模拟ListView完美解决。
andriod ScrollView基本用法
上下2个大小的ScrollView显示图片。
Scrollview和gridview同时使用的时候,Scrollview滑动gridview不滑动,Gridview滑动Scrollview不滑动!
iOS 开发里在swift 中,xib 的使用方法
这个代码可以判断ScrollView是否滑动到了最下边或者最上边,同理,HorizontalScrollView也可以...使用方法就是直接用这个自定义控件并实现里面的OnScrollListener就可以了,会自动复写三个方法:上边、下边或者中间。
ScrollView,当内容超过了整个屏幕或者容器的时候需要使用ScrollView 并且ScrollView的直接子元素只能有一个. ScrollView的用法非常简单,这里主要说的是ScrollView中ScrollBar的用法
解决listview和scrollview冲突 使用linearlayout自定一个listview 用法与listview相同
一个能够向任意方向滚动的ScrollView,不再纠结Android只能横着或竖着滑动了,用法与ScrollView相同,并且附送回弹,越界效果哦~~ 更多功能及效果图可查看:http://blog.csdn.net/cboy017/article/details/8313472
用法:在XML文件中将需滚动的控件包含在ScrollView中,当控件超出屏幕范围时可通过滚动查看;ScrollView也提供了一些方法来控制自身的显示情况。 1.ScrollView中包含其他控件 代码如下: <ScrollView android:id=...
React Native ReboundScrollView 支持android 反弹ScrollView,兼容ios -- 使用react scrollview 用法##Android 在android/setting.gradle ... include ':app',':ReboundScrollView' project(':ReboundScrollView')....
具体介绍链接:https://blog.csdn.net/luckydogyxx/article/details/117551097