自定义ViewGroup来理解View的scrollTo()方法
来源:互联网 发布:公知污名化 编辑:程序博客网 时间:2024/05/20 22:37
本文主要用于帮助理解View的scrollTo()方法,并穿叉了View的一些知识点。
一、自定义ViewGroup
自定义的ViewGroup有3个LinearLayout组成,在滑动时屏幕上一次只会显示一个LinearLayout,在构造函数中完成了布局的初始化,如下:
public class MultiViewGroup extends ViewGroup {private static final String TAG = "yxf";private Context mContext;public MultiViewGroup(Context context) {super(context);mContext = context;init();}public MultiViewGroup(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;init();}private void init() {// 初始化3个 LinearLayout控件LinearLayout oneLL = new LinearLayout(mContext);oneLL.setBackgroundColor(Color.RED); addView(oneLL);LinearLayout twoLL = new LinearLayout(mContext);twoLL.setBackgroundColor(Color.YELLOW);addView(twoLL);LinearLayout threeLL = new LinearLayout(mContext);threeLL.setBackgroundColor(Color.BLUE);addView(threeLL);}
另外,重载了onMeasure()和onLayout()方法。
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {Log.i(TAG, "--- start onMeasure() --");// 设置该ViewGroup的大小int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);setMeasuredDimension(width, height);int childCount = getChildCount();Log.i(TAG, "--- onMeasure childCount is -->" + childCount);for (int i = 0; i < childCount; i++) {View child = getChildAt(i);// 设置每个子视图的大小 , 即全屏child.measure(MultiScreenActivity.screenWidth, MultiScreenActivity.scrrenHeight);}}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stubLog.i(TAG, "--- start onLayout() --");int startLeft = 0; // 每个子视图的起始布局坐标int startTop = 50; // 间距设置为10px 相当于 android:layout_marginTop= "10px"int childCount = getChildCount();Log.i(TAG, "--- onLayout childCount is -->" + childCount);for (int i = 0; i < childCount; i++) {View child = getChildAt(i);child.layout(startLeft, startTop, startLeft + MultiScreenActivity.screenWidth, startTop + MultiScreenActivity.scrrenHeight);startLeft = startLeft + MultiScreenActivity.screenWidth;}}这两个地方重点注意:
child.measure(MultiScreenActivity.screenWidth, MultiScreenActivity.scrrenHeight);
child.layout(startLeft, startTop, startLeft + MultiScreenActivity.screenWidth,startTop + MultiScreenActivity.scrrenHeight);
二、在Activity中使用自定义的ViewGroup
整个Activity的布局如下图:
package com.example.scrollerviewdemos;import android.app.Activity;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MultiScreenActivity extends Activity implements OnClickListener {private static final String TAG = "yxf";private Button mScrollLeft;private Button mScrollRight;private MultiViewGroup mulTiViewGroup ;public static int screenWidth; // 屏幕宽度public static int scrrenHeight;//屏幕高度private int curscreen = 0;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); //获得屏幕分辨率大小DisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);screenWidth = metric.widthPixels ;scrrenHeight = metric.heightPixels;Log.d(TAG,"screenWidth * scrrenHeight --->" + screenWidth + " * " +scrrenHeight);setContentView(R.layout.activity_multiview); //获取自定义视图的空间引用mulTiViewGroup = (MultiViewGroup)findViewById(R.id.mymultiViewGroup);mScrollLeft = (Button) findViewById(R.id.bt_scrollLeft);mScrollRight = (Button) findViewById(R.id.bt_scrollRight);mScrollLeft.setOnClickListener(this);mScrollRight.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.bt_scrollLeft:if(curscreen > 0) { curscreen -- ; Toast.makeText(MultiScreenActivity.this, "第" +(curscreen+1) + "屏", Toast.LENGTH_SHORT).show();} else { Toast.makeText(MultiScreenActivity.this, "当前已是第一屏",Toast.LENGTH_SHORT).show();}mulTiViewGroup.scrollTo(curscreen * screenWidth , 0);break;case R.id.bt_scrollRight:if (curscreen < 2 ){curscreen ++ ;Toast.makeText(MultiScreenActivity.this, "第" + (curscreen+1) + "屏", Toast.LENGTH_SHORT).show();} else {Toast.makeText(MultiScreenActivity.this, "当前已是最后一屏",Toast.LENGTH_SHORT).show();}mulTiViewGroup.scrollTo(curscreen * screenWidth, 0);break;}}}布局文件res/layout/activity_multiview.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/tv_hello" android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" /><com.example.scrollerviewdemos.MultiViewGroupandroid:id="@+id/mymultiViewGroup"android:layout_below="@id/tv_hello"android:layout_height="wrap_content" android:layout_width="wrap_content"></com.example.scrollerviewdemos.MultiViewGroup><Button android:id="@+id/bt_scrollLeft"android:layout_alignParentBottom="true"android:layout_width="200dip"android:layout_height="wrap_content" android:text="Prev" /><Button android:id="@+id/bt_scrollRight" android:layout_width="200dip"android:layout_toRightOf="@id/bt_scrollLeft"android:layout_alignParentBottom="true" android:layout_height="wrap_content"android:text="Next" /></RelativeLayout>
0 0
- 自定义ViewGroup来理解View的scrollTo()方法
- ViewGroup自定义View方法
- 自定义view,viewgroup的onMeasure 方法
- View和ViewGroup的理解
- Android View 的scrollTo 和 scrollBy方法
- android View的scrollTo,scrollBy方法详解
- scrollTo 以及 scrollBy方法的理解
- Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解
- android view的scrollTo,scrollBy,computeScroll的理解
- View的scrollTo/scrollBy
- 自定义View的onMeasure方法理解
- View与ViewGroup的区别与联系及自定义ViewGroup
- 自定义ViewGroup[跟随手指移动的view]
- 自定义ViewGroup以及View的一些心得。
- 自定义view继承viewgroup的解释
- Android自定义View或ViewGroup的流程
- 自定义ViewGroup实现view的自由布局
- android自定义View(viewGroup)
- 北京产品之行---对产品的重新认识
- 1-25,通讯录管理系统(一)
- Java Web黑马程序员 听课笔记
- [背包问题][第三阶段-初见dp][HDU-1003]Max Sum
- Android相关属性的介绍:android:exported
- 自定义ViewGroup来理解View的scrollTo()方法
- Emacs教程 30minutes
- 千万不能松懈
- poj 1707 伯努利数
- Linux shell 之 提取文件名和目录名的一些方法
- 【Rosseta中文分词】python下利用HMM思想进行中文分词
- C语言二进制文件读取解析
- SQLServer 维护脚本分享(03)数据库中记录CPU最近N分钟内的使用情况
- hdu4614 Vases and Flowers