人在旅途

朝着胜利的方向~

Android ApiDemos/ScrollView2 添加自动滚动和智能焦点切换

Android ApiDemos/ScrollView2 添加自动滚动和智能焦点切换

首先总结一下andorid view的键盘事件处理过程:
键盘事件首先交给当前Activity中获得焦点的View,该View的
setKeyListener(OnKeyListener l)中设定的l的onKey方法被调用,若返回true,framwork不再处理,否则有framework继续处理.我就是用该方法实现的智能焦点切换。

然后是自动滚动的实现,当ViewGroup中添加了新的View后,不能马上进行滚动,因为此时相应的高度信息还没有更新,需要用
Handler来向消息队列投递一个Runnale来进行滚动。

相关实现如下:
1.ScrollView2.java
package com.google.android.samples.view;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.android.samples.R;


/**
* Demonstrates wrapping a layout in a ScrollView.
*
*/
public class ScrollView2 extends Activity {
private static final String LOG_TAG = "ScrollView2";
private LinearLayout mLayout;
private ScrollView mScrollView;
private final Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.scroll_view_2);

mLayout = (LinearLayout) findViewById(R.id.layout);
mScrollView = (ScrollView) findViewById(R.id.scrollview);

Button button = (Button) findViewById(R.id.add_widget);
button.setOnClickListener(mClickListener);
//改变默认焦点切换
button.setKeyListener(mAddButtonKeyListener);
}

private Button.OnClickListener mClickListener = new Button.OnClickListener() {
private int mIndex = 1;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView textView = new TextView(ScrollView2.this);
textView.setText("Text View " + mIndex);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
mLayout.addView(textView, p);

Button buttonView = new Button(ScrollView2.this);
buttonView.setText("Button " + mIndex++);
mLayout.addView(buttonView, p);
//改变默认焦点切换
buttonView.setKeyListener(mNewButtonKeyListener);
//投递一个消息进行滚动
mHandler.post(mScrollToBottom);
}
};

private Runnable mScrollToBottom = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "ScrollY: " + mScrollView.getScrollY());
int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();
if (off > 0) {
mScrollView.scrollTo(0, off);
}
}
};

private View.OnKeyListener mNewButtonKeyListener = new View.OnKeyListener() {
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.add_widget).requestFocus();
return true;
}
return false;
}
};

private View.OnKeyListener mAddButtonKeyListener = new Button.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, event.toString());

View viewToFoucus = null;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
int iCount = mLayout.getChildCount();
if ( iCount > 0) {
viewToFoucus = mLayout.getChildAt(iCount - 1);
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (mLayout.getChildCount() > 1) {
viewToFoucus = mLayout.getChildAt(1);
}
break;
default:
break;
}
}

if (viewToFoucus != null) {
viewToFoucus.requestFocus();
return true;
} else {
return false;
}
}
};
}

2.scroll_view_2.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Demonstrates scrolling with a ScrollView. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button id="@+id/add_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Widget"/>
<ScrollView id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
阅读更多
个人分类: Android(转)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭