Android——ScrollView
1.activity_scrollview.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/linearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView0" />
<Button
android:id="@+id/button001"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button0" />
</LinearLayout>
</ScrollView>
2、ScrollViewActivity
public class ScrollViewActivity extends Activity {
private LinearLayout mLayout;
private ScrollView mScrollView;
private final Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview);
mLayout = (LinearLayout) findViewById(R.id.linearLayout01);
mScrollView = (ScrollView) findViewById(R.id.scrollView01);
Button button = (Button) findViewById(R.id.button001);
// 设置Button点击事件
button.setOnClickListener(mClickListener);
// 改变默认焦点切换
button.setOnKeyListener(mAddButtonKeyListener);
}
// button事件监听,点击按钮时,增加一个TextView和Button
private Button.OnClickListener mClickListener = new Button.OnClickListener() {
private int mIndex = 1;
@Override
public void onClick(View v) {
TextView textView = new TextView(ScrollViewActivity.this);
textView.setText("Text View:" + mIndex);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// 增加一个TextView到线性布局中
mLayout.addView(textView, params);
Button buttonView = new Button(ScrollViewActivity.this);
buttonView.setText("button :" + mIndex++);
// 增加一个Button到线性布局中
mLayout.addView(buttonView, params);
// 改变默认焦点切换**************************
buttonView.setOnKeyListener(mNewButtonKeyListener);
// 投递一个消息进行滚动
mHandler.post(mScrollToButtom);
}
};
private Runnable mScrollToButtom = new Runnable() {
@Override
public void run() {
int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();
if (off > 0) {
mScrollView.scrollTo(0, off);
}
}
};
// 事件监听
private View.OnKeyListener mNewButtonKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN
&& event.getAction() == KeyEvent.ACTION_DOWN
&& v == mLayout.getChildAt(mLayout.getChildCount() - 1)) {
findViewById(R.id.button001).requestFocus();
return true;
}
return false;
}
};
// 事件监听
private View.OnKeyListener mAddButtonKeyListener = new Button.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
View viewToFocus = null;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
int iCount = mLayout.getChildCount();
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
if (iCount > 0) {
viewToFocus = mLayout.getChildAt(iCount - 1);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (iCount < mLayout.getWeightSum()) {
viewToFocus = mLayout.getChildAt(iCount + 1);
}
break;
default:
break;
}
}
if (viewToFocus != null) {
viewToFocus.requestFocus();
return true;
} else {
return false;
}
}
};
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。