安卓开发--scrollview
2024-09-06 23:08:03
package com.cnn.scrollviewdemo01; import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView; @SuppressLint("ClickableViewAccessibility") public class MainActivity extends Activity {
private TextView textView;
private ScrollView scrollView;
private Button btnUp,btnDown;
int scrollY=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView= (TextView) findViewById(R.id.textView1);
textView.setText(getResources().getString(R.string.china_news)); scrollView=(ScrollView) findViewById(R.id.scrollView1);
scrollView.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
// TODO 自动生成的方法存根
switch (event.getAction()) {
case MotionEvent.ACTION_UP://手指抬起 break; case MotionEvent.ACTION_DOWN://手指落下 break;
case MotionEvent.ACTION_MOVE://手指滑动
if (scrollView.getScaleY()<=0) {//顶部
Log.i("tag", "没有滑动");
}
//textView总高度<=一屏幕的高度+滚动条的滚动距离
if (scrollView.getChildAt(0).getMeasuredHeight()<=
scrollView.getHeight()+scrollView.getScrollY()) {
Log.i("tag", "没有到底部");
textView.append("123111111111111111111111111111111111111111111111111111111");//(getResources().getString(R.string.china_news));
}
break;
}
return false;
}
}); btnUp=(Button) findViewById(R.id.button2);
btnDown=(Button) findViewById(R.id.button1); btnDown.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
//scrollY=scrollY+10;
//scrollView.setScrollY(scrollY);
scrollView.scrollBy(0, 20);
}
}); btnUp.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
//scrollY=scrollY-10;
//scrollView.setScrollY(scrollY);
scrollView.scrollBy(0, -20);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Donw" /> <ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>
</ScrollView> </LinearLayout>
最新文章
- Unable to create the selected property page. An error occurred while automatically activating bundle net.sourceforge.pmd
- Go语言配置与开发环境配置
- java 获取获取字符串编码格式
- Hibernate常见接口说明
- IOS 定位 单例
- struts1标签(html:text)
- CKEdit( htm编辑器)
- Linux新手的最佳包管理器
- How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app
- 怎么分别javascript写在<;head>;里还是<;body>;里面?
- 01——Solr学习之全文检索服务系统的基础认识
- Windows下USB磁盘开发系列一:枚举系统中U盘的盘符
- Final互评------《飞词》---- 拉格朗日2018
- POI往word模板中写入数据
- unicode utf8 学习记录
- 两种定时器 setInterval(一直执行) setTimeout(只执行一次)
- FastReport.Net使用:[26]数字格式
- Java - 无乱码读写文件
- Unity3D usage Experience
- 【转】哈希(Hash)与加密(Encrypt)的基本原理、区别及工程应用