码迷,mamicode.com
首页 > 移动开发 > 详细

Android——ScrollView

时间:2015-04-08 23:17:37      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

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;
            }
        }
    };

}

Android——ScrollView

标签:

原文地址:http://www.cnblogs.com/Defry/p/4404003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!