android 滚动视图(ScrollView)学习与应用

huzhenv 2012-02-12

http://blog.csdn.net/applezhengxd/article/details/5574356

<?xml version="1.0" encoding="utf-8"?>
<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/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加按钮" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollbars="vertical">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>
package com.scrollview;

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;

public class ScrollViewActivity extends Activity {
    /** Called when the activity is first created. */
	// http://blog.csdn.net/applezhengxd/article/details/5574356
	private Button button1;
	private ScrollView scrollView1;
	private LinearLayout linearLayout1;
	private final Handler mHandler = new Handler();
	private static final String LOG_TAG = "ScrollViewActivity"; 
	
    @Override
    protected void onCreate(Bundle icicle) {     
        super.onCreate(icicle);     
        setContentView(R.layout.main);     
    
        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);     
        scrollView1 = (ScrollView) findViewById(R.id.scrollView1);     
    
        Button button = (Button) findViewById(R.id.button1);     
        button.setOnClickListener(new Button.OnClickListener() {     
            private int mIndex = 1;     
            @Override    
            public void onClick(View arg0) {     
                // TODO Auto-generated method stub          
                TextView textView = new TextView(ScrollViewActivity.this);     
                textView.setText("Text View " + mIndex);     
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(     
                        LinearLayout.LayoutParams.FILL_PARENT,     
                        LinearLayout.LayoutParams.WRAP_CONTENT     
                );     
                linearLayout1.addView(textView, p);     
        
                Button buttonView = new Button(ScrollViewActivity.this);     
                buttonView.setText("Button " + mIndex++);     
                linearLayout1.addView(buttonView, p);     
                //改变默认焦点切换     
                buttonView.setOnKeyListener(new View.OnKeyListener() {     
                    public boolean onKey(View v, int keyCode, KeyEvent event) {     
                        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN &&     
                                event.getAction() == KeyEvent.ACTION_DOWN &&     
                                v == linearLayout1.getChildAt(linearLayout1.getChildCount() - 1)) {     
                            findViewById(R.id.button1).requestFocus();     
                            return true;     
                        }     
                        return false;     
                    }     
                });     
                //投递一个消息进行滚动     
                mHandler.post(mScrollToBottom);     
            }            
        });     
        //改变默认焦点切换     
        button.setOnKeyListener(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 = linearLayout1.getChildCount();     
                        if ( iCount > 0) {     
                            viewToFoucus = linearLayout1.getChildAt(iCount - 1);     
                        }     
                        break;     
                    case KeyEvent.KEYCODE_DPAD_DOWN:     
                        if (linearLayout1.getChildCount() > 1) {     
                            viewToFoucus = linearLayout1.getChildAt(1);     
                        }     
                        break;     
                    default:     
                        break;     
                    }     
                }     
                     
                if (viewToFoucus != null) {     
                    viewToFoucus.requestFocus();     
                    return true;     
                } else {     
                    return false;     
                }     
            }     
        });     
    }     
         
         
    private Runnable mScrollToBottom = new Runnable() {     
        @Override    
        public void run() {     
            // TODO Auto-generated method stub     
            Log.d(LOG_TAG, "ScrollY: " + scrollView1.getScrollY());     
            int off = linearLayout1.getMeasuredHeight() - scrollView1.getHeight();     
            if (off > 0) {     
            	scrollView1.scrollTo(0, off);     
            }                            
        }     
    };     
         
}

相关推荐

ALDRIDGE / 0评论 2011-09-29