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

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

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

相关实现如下:
1.ScrollView2.java
Java代码 复制代码
  1. package com.google.android.samples.view;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.os.Handler;   
  6. import android.util.Log;   
  7. import android.view.KeyEvent;   
  8. import android.view.View;   
  9. import android.widget.Button;   
  10. import android.widget.LinearLayout;   
  11. import android.widget.ScrollView;   
  12. import android.widget.TextView;   
  13.   
  14. import com.google.android.samples.R;   
  15.   
  16.   
  17. /**  
  18.  * Demonstrates wrapping a layout in a ScrollView.  
  19.  *  
  20.  */  
  21. public class ScrollView2 extends Activity {   
  22.     private static final String LOG_TAG = "ScrollView2";   
  23.     private LinearLayout mLayout;   
  24.     private ScrollView mScrollView;   
  25.     private final Handler mHandler = new Handler();   
  26.        
  27.     @Override  
  28.     protected void onCreate(Bundle icicle) {   
  29.         super.onCreate(icicle);   
  30.         setContentView(R.layout.scroll_view_2);   
  31.   
  32.         mLayout = (LinearLayout) findViewById(R.id.layout);   
  33.         mScrollView = (ScrollView) findViewById(R.id.scrollview);   
  34.   
  35.         Button button = (Button) findViewById(R.id.add_widget);   
  36.         button.setOnClickListener(mClickListener);   
  37.         //改变默认焦点切换   
  38.         button.setKeyListener(mAddButtonKeyListener);   
  39.     }   
  40.        
  41.     private Button.OnClickListener mClickListener = new Button.OnClickListener() {   
  42.         private int mIndex = 1;   
  43.         @Override  
  44.         public void onClick(View arg0) {   
  45.             // TODO Auto-generated method stub        
  46.             TextView textView = new TextView(ScrollView2.this);   
  47.             textView.setText("Text View " + mIndex);   
  48.             LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(   
  49.                     LinearLayout.LayoutParams.FILL_PARENT,   
  50.                     LinearLayout.LayoutParams.WRAP_CONTENT   
  51.             );   
  52.             mLayout.addView(textView, p);   
  53.   
  54.             Button buttonView = new Button(ScrollView2.this);   
  55.             buttonView.setText("Button " + mIndex++);   
  56.             mLayout.addView(buttonView, p);   
  57.             //改变默认焦点切换   
  58.             buttonView.setKeyListener(mNewButtonKeyListener);   
  59.             //投递一个消息进行滚动   
  60.             mHandler.post(mScrollToBottom);   
  61.         }          
  62.     };   
  63.        
  64.     private Runnable mScrollToBottom = new Runnable() {   
  65.         @Override  
  66.         public void run() {   
  67.             // TODO Auto-generated method stub   
  68.             Log.d(LOG_TAG, "ScrollY: " + mScrollView.getScrollY());   
  69.             int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();   
  70.             if (off > 0) {   
  71.                 mScrollView.scrollTo(0, off);   
  72.             }                          
  73.         }   
  74.     };   
  75.       
  76.     private View.OnKeyListener mNewButtonKeyListener = new View.OnKeyListener() {   
  77.         public boolean onKey(View v, int keyCode, KeyEvent event) {   
  78.             if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN &&   
  79.                     event.getAction() == KeyEvent.ACTION_DOWN &&   
  80.                     v == mLayout.getChildAt(mLayout.getChildCount() - 1)) {   
  81.                 findViewById(R.id.add_widget).requestFocus();   
  82.                 return true;   
  83.             }   
  84.             return false;   
  85.         }   
  86.     };   
  87.        
  88.     private View.OnKeyListener mAddButtonKeyListener = new Button.OnKeyListener() {   
  89.         @Override  
  90.         public boolean onKey(View v, int keyCode, KeyEvent event) {   
  91.             // TODO Auto-generated method stub   
  92.             Log.d(LOG_TAG, event.toString());   
  93.                
  94.             View viewToFoucus = null;   
  95.             if (event.getAction() == KeyEvent.ACTION_DOWN) {   
  96.                 switch (keyCode) {   
  97.                 case KeyEvent.KEYCODE_DPAD_UP:   
  98.                     int iCount = mLayout.getChildCount();   
  99.                     if ( iCount > 0) {   
  100.                         viewToFoucus = mLayout.getChildAt(iCount - 1);   
  101.                     }   
  102.                     break;   
  103.                 case KeyEvent.KEYCODE_DPAD_DOWN:   
  104.                     if (mLayout.getChildCount() > 1) {   
  105.                         viewToFoucus = mLayout.getChildAt(1);   
  106.                     }   
  107.                     break;   
  108.                 default:   
  109.                     break;   
  110.                 }   
  111.             }   
  112.                
  113.             if (viewToFoucus != null) {   
  114.                 viewToFoucus.requestFocus();   
  115.                 return true;   
  116.             } else {   
  117.                 return false;   
  118.             }   
  119.         }   
  120.     };   
  121. }  
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
Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!-- Demonstrates scrolling with a ScrollView. -->   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:orientation="vertical">   
  7.     <Button id="@+id/add_widget"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Add Widget"/>   
  11. <ScrollView id="@+id/scrollview"  
  12.     android:layout_width="fill_parent"  
  13.     android:layout_height="wrap_content"  
  14.     android:scrollbars="vertical">   
  15.     <LinearLayout   
  16.         id="@+id/layout"  
  17.         android:orientation="vertical"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content">   
  20.     </LinearLayout>   
  21. </ScrollView>   
  22. </LinearLayout>   
发布了1 篇原创文章 · 获赞 3 · 访问量 1万+
展开阅读全文

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

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览