├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── Image.png ├── LimitScrollerView.gif ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── openxu │ │ └── lc │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── openxu │ │ │ └── lc │ │ │ ├── DataBean.java │ │ │ ├── LimitScrollerView.java │ │ │ ├── MainActivity.java │ │ │ └── ToActivity.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── limit_scroller.xml │ │ └── limit_scroller_item.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── openxu │ └── lc │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── tm.gif └── 讲解.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | LimitScrollerView -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/Image.png -------------------------------------------------------------------------------- /LimitScrollerView.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/LimitScrollerView.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ###高仿天猫轮转广告条,可指定每次展示的广告条数,滚动速度、滚动时间间隔 3 | 4 | ###讲解博客:http://blog.csdn.net/xmxkf/article/details/53303872 5 | 6 | ##效果图: 7 | 8 | ![](/tm.gif "效果图") 9 | ![](/LimitScrollerView.gif "效果图") 10 | 11 | 12 | ##使用方法: 13 | 14 | 1、文件拷贝: 15 | ①、将`limit_scroller.xml`、`limit_scroller_item.xml`拷贝到`layout`文件夹中 16 | ②、将`attrs.xml`拷贝到`values`目录下 17 | ③、将`LimitScrollerView`自定义控件拷贝到项目源码目录下 18 | 19 | 2、在activity布局中使用自定义控件 20 | ``` 21 | 28 | ``` 29 | 30 | 3、Activity中: 31 | ①、初始化控件 32 | ``` 33 | limitScroll = (LimitScrollerView)findViewById(R.id.limitScroll); 34 | ``` 35 | ②、设置数据适配器,需要实现`LimitScrollerView.LimitScrllAdapter`,详情请见`MainActivity.MyLimitScrllAdapter` 36 | ``` 37 | //API:1、设置数据适配器 38 | adapter = new MyLimitScrllAdapter(); 39 | limitScroll.setDataAdapter(adapter); 40 | ``` 41 | ③、请求到服务器数据后填充数据 42 | ``` 43 | adapter.setDatas(datas); 44 | ``` 45 | ④、同步生命周期方法 46 | onStart()方法中调用 47 | ``` 48 | //API:2、开始滚动 49 | limitScroll.startScroll(); 50 | ``` 51 | onStop()方法中调用 52 | ``` 53 | //API:3、停止滚动 54 | limitScroll.cancel(); 55 | ``` 56 | ⑤、条目点击事件 57 | ``` 58 | //API:4、设置条目点击事件 59 | limitScroll.setOnItemClickListener(new LimitScrollerView.OnItemClickListener() { 60 | @Override 61 | public void onItemClick(Object obj) { 62 | if(obj instanceof DataBean){ 63 | //强制转换 64 | DataBean bean = (DataBean)obj; 65 | Toast.makeText(MainActivity.this, "点击了:"+bean.getText(), Toast.LENGTH_SHORT).show(); 66 | Log.v("oepnxu", "点击了:"+bean.getText()); 67 | } 68 | 69 | } 70 | }); 71 | ``` 72 | 73 | 74 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.openxu.lc" 9 | minSdkVersion 11 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.4.0' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\IDE\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/openxu/lc/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.openxu.lc; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/openxu/lc/DataBean.java: -------------------------------------------------------------------------------- 1 | package com.openxu.lc; 2 | 3 | /** 4 | * author : openXu 5 | * create at : 2016/11/22 12:12 6 | * blog : http://blog.csdn.net/xmxkf 7 | * gitHub : https://github.com/openXu 8 | * project : LimitScrollerView 9 | * class name : DataBean 10 | * version : 1.0 11 | * class describe: 数据 12 | */ 13 | public class DataBean { 14 | public DataBean(int icon, String text) { 15 | this.icon = icon; 16 | this.text = text; 17 | } 18 | 19 | private int icon; 20 | private String text; 21 | 22 | public int getIcon() { 23 | return icon; 24 | } 25 | 26 | public void setIcon(int icon) { 27 | this.icon = icon; 28 | } 29 | 30 | public String getText() { 31 | return text; 32 | } 33 | 34 | public void setText(String text) { 35 | this.text = text; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/openxu/lc/LimitScrollerView.java: -------------------------------------------------------------------------------- 1 | package com.openxu.lc; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorSet; 5 | import android.animation.ObjectAnimator; 6 | import android.content.Context; 7 | import android.content.res.TypedArray; 8 | import android.os.Handler; 9 | import android.os.Message; 10 | import android.util.AttributeSet; 11 | import android.util.Log; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.widget.LinearLayout; 15 | 16 | /** 17 | * author : openXu 18 | * create at : 2016/11/22 10:14 19 | * blog : http://blog.csdn.net/xmxkf 20 | * gitHub : https://github.com/openXu 21 | * project : LimitScrollerView 22 | * class name : LimitScrollerView 23 | * version : 1.0 24 | * class describe: 25 | */ 26 | public class LimitScrollerView extends LinearLayout implements View.OnClickListener{ 27 | 28 | private String TAG = "LimitScrollerView"; 29 | 30 | private LinearLayout ll_content1, ll_content2; 31 | private LinearLayout ll_now, ll_down; //当前可见的,下面不可见的(切换) 32 | private int limit; //可见条目数量 33 | private int durationTime; //动画执行时间 34 | private int periodTime; //间隔时间 35 | private int scrollHeight; //滚动高度(控件高度) 36 | 37 | private int dataIndex; 38 | 39 | private boolean isCancel; //是否停止滚动动画 40 | private boolean boundData; //是否已经第一次绑定过数据 41 | 42 | private final int MSG_SETDATA = 1; 43 | private final int MSG_SCROL = 2; 44 | 45 | private Handler handler = new Handler(){ 46 | @Override 47 | public void handleMessage(Message msg) { 48 | if(msg.what == MSG_SETDATA){ 49 | boundData(true); 50 | }else if(msg.what == MSG_SCROL){ 51 | if(isCancel) 52 | return; 53 | startAnimation(); 54 | } 55 | } 56 | }; 57 | 58 | public LimitScrollerView(Context context) { 59 | this(context, null); 60 | } 61 | 62 | public LimitScrollerView(Context context, AttributeSet attrs) { 63 | this(context, attrs, 0); 64 | } 65 | 66 | public LimitScrollerView(Context context, AttributeSet attrs, int defStyleAttr) { 67 | super(context, attrs, defStyleAttr); 68 | init(context, attrs); 69 | } 70 | 71 | private void init(Context context, AttributeSet attrs){ 72 | LayoutInflater.from(context).inflate(R.layout.limit_scroller, this, true); 73 | ll_content1 = (LinearLayout) findViewById(R.id.ll_content1); 74 | ll_content2 = (LinearLayout) findViewById(R.id.ll_content2); 75 | ll_now = ll_content1; 76 | ll_down = ll_content2; 77 | if(attrs!=null){ 78 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LimitScroller); 79 | limit = ta.getInt(R.styleable.LimitScroller_limit, 1); 80 | durationTime = ta.getInt(R.styleable.LimitScroller_durationTime, 1000); 81 | periodTime = ta.getInt(R.styleable.LimitScroller_periodTime, 1000); 82 | ta.recycle(); //注意回收 83 | Log.v(TAG, "limit="+limit); 84 | Log.v(TAG, "durationTime="+durationTime); 85 | Log.v(TAG, "periodTime="+periodTime); 86 | } 87 | } 88 | 89 | @Override 90 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 91 | //EXACTLY=1073741824 92 | //AT_MOST=-2147483648 93 | /* int specMode = MeasureSpec.getMode(heightMeasureSpec); 94 | int specSize = MeasureSpec.getSize(heightMeasureSpec); 95 | Log.w(TAG, "specMode="+specMode); 96 | Log.w(TAG, "specSize="+specSize); 97 | int newHeightSpec = MeasureSpec.makeMeasureSpec(specSize, MeasureSpec.EXACTLY); 98 | int childCount = ll_content1.getChildCount(); 99 | if(childCount>0){ 100 | View item = ll_content1.getChildAt(0); 101 | item.measure(widthMeasureSpec, newHeightSpec); 102 | Log.w(TAG, "条目高度="+ item.getMeasuredHeight()); 103 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 104 | setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight()/2); 105 | scrollHeight = getMeasuredHeight(); 106 | return; 107 | } 108 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);*/ 109 | 110 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 111 | //设置高度为整体高度的一般,以达到遮盖预备容器的效果 112 | setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight()/2); 113 | //此处记下控件的高度,此高度就是动画执行时向上滚动的高度 114 | scrollHeight = getMeasuredHeight(); 115 | // Log.w(TAG, "getMeasuredWidth="+getMeasuredWidth()); 116 | // Log.w(TAG, "getMeasuredHeight="+getMeasuredHeight()); 117 | // Log.w(TAG, "scrollHeight="+scrollHeight); 118 | } 119 | 120 | private void startAnimation(){ 121 | if(isCancel) 122 | return; 123 | Log.i(TAG, "滚动"); 124 | //当前展示的容器,从当前位置(0),向上滚动scrollHeight 125 | ObjectAnimator anim1 = ObjectAnimator.ofFloat(ll_now, "Y",ll_now.getY(), ll_now.getY()-scrollHeight); 126 | //预备容器,从当前位置,向上滚动scrollHeight 127 | ObjectAnimator anim2 = ObjectAnimator.ofFloat(ll_down, "Y",ll_down.getY(), ll_down.getY()-scrollHeight); 128 | AnimatorSet animSet = new AnimatorSet(); 129 | animSet.setDuration(durationTime); 130 | animSet.playTogether(anim1, anim2); 131 | animSet.addListener(new Animator.AnimatorListener() { 132 | @Override 133 | public void onAnimationStart(Animator animation) { 134 | // Log.v(TAG, "ll_now动画开始前位置:"+ll_now.getX()+"*"+ll_now.getY()); 135 | // Log.v(TAG, "ll_down动画开始前位置:"+ll_down.getX()+"*"+ll_down.getY()); 136 | } 137 | @Override 138 | public void onAnimationEnd(Animator animation) { 139 | //滚动结束后,now的位置变成了-scrollHeight,这时将他移动到最底下 140 | ll_now.setY(scrollHeight); 141 | //down的位置变为0,也就是当前看见的 142 | ll_down.setY(0); 143 | // Log.v(TAG, "1调整之后ll_now位置:"+ll_now.getX()+"*"+ll_now.getY()); 144 | // Log.v(TAG, "1调整之后ll_down位置:"+ll_down.getX()+"*"+ll_down.getY()); 145 | LinearLayout temp = ll_now; 146 | ll_now = ll_down; 147 | ll_down = temp; 148 | // Log.v(TAG, "2调整之后ll_now位置:"+ll_now.getX()+"*"+ll_now.getY()); 149 | // Log.v(TAG, "2调整之后ll_down位置:"+ll_down.getX()+"*"+ll_down.getY()); 150 | //给不可见的控件绑定新数据 151 | boundData(false); 152 | 153 | handler.removeMessages(MSG_SCROL); 154 | if(isCancel) { 155 | return; 156 | } 157 | handler.sendEmptyMessageDelayed(MSG_SCROL, periodTime); 158 | 159 | } 160 | @Override 161 | public void onAnimationCancel(Animator animation) { 162 | } 163 | @Override 164 | public void onAnimationRepeat(Animator animation) { 165 | } 166 | }); 167 | animSet.start(); 168 | } 169 | 170 | /** 171 | * 向容器中添加子条目 172 | * @param first 173 | */ 174 | private void boundData(boolean first){ 175 | if(adapter==null || adapter.getCount()<=0) 176 | return; 177 | if(first){ 178 | //第一次绑定数据,需要为两个容器添加子条目 179 | boundData = true; 180 | ll_now.removeAllViews(); 181 | for(int i = 0; i=adapter.getCount()) 183 | dataIndex = 0; 184 | View view = adapter.getView(dataIndex); 185 | 186 | //设置点击监听 187 | view.setClickable(true); 188 | view.setOnClickListener(this); 189 | 190 | ll_now.addView(view); 191 | dataIndex ++; 192 | } 193 | } 194 | 195 | //每次动画结束之后,为预备容器添加新条目 196 | ll_down.removeAllViews(); 197 | for(int i = 0; i=adapter.getCount()) 199 | dataIndex = 0; 200 | View view = adapter.getView(dataIndex); 201 | //设置点击监听 202 | view.setClickable(true); 203 | view.setOnClickListener(this); 204 | ll_down.addView(view); 205 | dataIndex ++; 206 | } 207 | } 208 | 209 | @Override 210 | public void onClick(View v) { 211 | if(clickListener!=null){ 212 | Object obj = v.getTag(); 213 | clickListener.onItemClick(obj); 214 | } 215 | } 216 | 217 | interface LimitScrollAdapter{ 218 | public int getCount(); 219 | public View getView(int index); 220 | } 221 | private LimitScrollAdapter adapter; 222 | 223 | interface OnItemClickListener{ 224 | public void onItemClick(Object obj); 225 | } 226 | private OnItemClickListener clickListener; 227 | /**********************public API 以下为暴露的接口***********************/ 228 | 229 | /** 230 | * 1、设置数据适配器 231 | * @param adapter 232 | */ 233 | public void setDataAdapter(LimitScrollAdapter adapter){ 234 | this.adapter = adapter; 235 | handler.sendEmptyMessage(MSG_SETDATA); 236 | } 237 | 238 | /** 239 | * 2、开始滚动 240 | * 应该在两处调用此方法: 241 | * ①、Activity.onStart() 242 | * ②、MyLimitScrllAdapter.setDatas() 243 | */ 244 | public void startScroll(){ 245 | if(adapter==null||adapter.getCount()<=0) 246 | return; 247 | if(!boundData){ 248 | handler.sendEmptyMessage(MSG_SETDATA); 249 | } 250 | isCancel = false; 251 | Log.e(TAG, "开始滚动"); 252 | handler.removeMessages(MSG_SCROL); //先清空所有滚动消息,避免滚动错乱 253 | handler.sendEmptyMessageDelayed(MSG_SCROL, periodTime); 254 | } 255 | /** 256 | * 3、停止滚动 257 | * 当在Activity不可见时,在Activity.onStop()中调用 258 | */ 259 | public void cancel(){ 260 | isCancel = true; 261 | Log.e(TAG, "停止滚动"); 262 | } 263 | 264 | /** 265 | * 4、设置条目点击事件 266 | * @param listener 267 | */ 268 | public void setOnItemClickListener(OnItemClickListener listener){ 269 | this.clickListener = listener; 270 | } 271 | 272 | } 273 | -------------------------------------------------------------------------------- /app/src/main/java/com/openxu/lc/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.openxu.lc; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | public class MainActivity extends Activity { 17 | 18 | private LimitScrollerView limitScroll; 19 | private MyLimitScrollAdapter adapter; 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_main); 25 | 26 | limitScroll = (LimitScrollerView)findViewById(R.id.limitScroll); 27 | 28 | //API:4、设置条目点击事件 29 | limitScroll.setOnItemClickListener(new LimitScrollerView.OnItemClickListener() { 30 | @Override 31 | public void onItemClick(Object obj) { 32 | if(obj instanceof DataBean){ 33 | //强制转换 34 | DataBean bean = (DataBean)obj; 35 | Toast.makeText(MainActivity.this, "点击了:"+bean.getText(), Toast.LENGTH_SHORT).show(); 36 | Log.v("oepnxu", "点击了:"+bean.getText()); 37 | } 38 | 39 | } 40 | }); 41 | 42 | //API:1、设置数据适配器 43 | adapter = new MyLimitScrollAdapter(); 44 | limitScroll.setDataAdapter(adapter); 45 | 46 | initData(); 47 | } 48 | 49 | 50 | private void initData(){ 51 | 52 | //TODO 模拟获取服务器数据操作,此处需要修改 53 | new Thread(new Runnable() { 54 | @Override 55 | public void run() { 56 | try { 57 | Thread.sleep(3000); 58 | } catch (InterruptedException e) { 59 | e.printStackTrace(); 60 | } 61 | 62 | List datas = new ArrayList<>(); 63 | datas.add(new DataBean(R.mipmap.ic_launcher, "1.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 64 | datas.add(new DataBean(R.mipmap.ic_launcher, "2.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 65 | datas.add(new DataBean(R.mipmap.ic_launcher, "3.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 66 | datas.add(new DataBean(R.mipmap.ic_launcher, "4.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 67 | datas.add(new DataBean(R.mipmap.ic_launcher, "5.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 68 | datas.add(new DataBean(R.mipmap.ic_launcher, "6.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 69 | datas.add(new DataBean(R.mipmap.ic_launcher, "7.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 70 | datas.add(new DataBean(R.mipmap.ic_launcher, "8.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 71 | datas.add(new DataBean(R.mipmap.ic_launcher, "9.劲爆促销中,凡在此商场消费满888的顾客,请拿着小票到前台咨询处免费领取美女一枚")); 72 | 73 | adapter.setDatas(datas); 74 | 75 | } 76 | }).start(); 77 | 78 | 79 | 80 | } 81 | 82 | @Override 83 | protected void onStart() { 84 | super.onStart(); 85 | limitScroll.startScroll(); //API:2、开始滚动 86 | } 87 | 88 | 89 | //TODO 修改适配器绑定数据 90 | class MyLimitScrollAdapter implements LimitScrollerView.LimitScrollAdapter{ 91 | 92 | private List datas; 93 | public void setDatas(List datas){ 94 | this.datas = datas; 95 | //API:2、开始滚动 96 | limitScroll.startScroll(); 97 | } 98 | @Override 99 | public int getCount() { 100 | return datas==null?0:datas.size(); 101 | } 102 | 103 | @Override 104 | public View getView(int index) { 105 | View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.limit_scroller_item, null, false); 106 | ImageView iv_icon = (ImageView)itemView.findViewById(R.id.iv_icon); 107 | TextView tv_text = (TextView)itemView.findViewById(R.id.tv_text); 108 | 109 | //绑定数据 110 | DataBean data = datas.get(index); 111 | itemView.setTag(data); 112 | iv_icon.setImageResource(data.getIcon()); 113 | tv_text.setText(data.getText()); 114 | return itemView; 115 | } 116 | } 117 | 118 | 119 | @Override 120 | protected void onStop() { 121 | super.onStop(); 122 | //API:3、停止滚动 123 | limitScroll.cancel(); 124 | } 125 | 126 | public void to(View v){ 127 | startActivity(new Intent(this,ToActivity.class)); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/openxu/lc/ToActivity.java: -------------------------------------------------------------------------------- 1 | package com.openxu.lc; 2 | 3 | import android.app.Activity; 4 | 5 | /** 6 | * author : openXu 7 | * create at : 2016/11/23 16:43 8 | * blog : http://blog.csdn.net/xmxkf 9 | * gitHub : https://github.com/openXu 10 | * project : LimitScrollerView 11 | * class name : ToActivity 12 | * version : 1.0 13 | * class describe: 14 | */ 15 | public class ToActivity extends Activity{ 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 22 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/layout/limit_scroller.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 11 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/limit_scroller_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 12 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | LimitScrollerView 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/openxu/lc/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.openxu.lc; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.1.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /tm.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/openXu/LimitScrollerView/58da86114d9b83f5a84edf2e19408d722b506875/tm.gif -------------------------------------------------------------------------------- /讲解.md: -------------------------------------------------------------------------------- 1 |   最近项目中需要在首页做一个跑马灯类型的广告栏,最后上面决定仿照天猫的广告栏效果做(中间部位),效果图如下(右边是我们的效果): 2 | 3 |     ![这里写图片描述](http://img.blog.csdn.net/20161123122218742) ![这里写图片描述](http://img.blog.csdn.net/20161123122236180) 4 | 5 |   天猫上抢购那一栏的广告条可以向上滚动,每次展示一条广告,展示一定时间后,第二条广告从下往上顶起。但项目经理说我们需要一次展示两条广告,广告每次停留5秒,然后向上滚动,滚动的过程持续1.5秒。要求还真多,想着这么多要求说不定什么时候又得改了,每次展示三条广告,需要停留8秒,滚动持续3秒,那就死球了。所以干脆自己封装一个通用的,你爱咋改咋改... 6 | 7 | ##1、分析 8 |   遇到这种展示效果,我们第一反应就会想到两个控件:`ListView`、`ScrollerView`。`ListView`可以展示条目,只需要重写下`onMeasure`就能达到一次只显示**n**条的效果,但是要自动滚动、滚动时间限制貌似有点困难;`ScrollerView`可以动态的往里面添加指定数量的条目,可以实现自动滚动,但是滚动持续时间不可控制。想到这里,顿时绝望、一头雾水,既然系统自带的控件实现起来有困难,那就自己造。 9 | 10 |   经过一小阵思索,突然灵光一现,如下: 11 |     ![](http://img.blog.csdn.net/20161123122412681) 12 | 13 |   既然要实现滚动的效果,肯定有一个容器容纳当前展示的条目,还有一个容器在下面作为预备展示的容器,需要展示几条就动态的向容器中添加指定数量的子条目;最外层是一个大的容器,如果将他的高度设置为小容器的高度,即可实现遮挡预备容器的目的;滚动可使用动画集合,让两个容器同时向上滚动;滚动结束后,马上让被顶上去的容器复位到预备位置;这里需要两个引用指向当前展示的容器和预备容器,当动画结束之后,这两个引用需要互换。经过一段时间停留后重复上述步骤即可。 14 | 15 |   思路是有了,要实现起来得考虑细节了。最外层用什么包裹?继承`ViewGroup`?太麻烦(得重写`onLayout`计算麻烦),我要实现的效果就是里面的两个容器在开始的时候能够垂直向下排列好即可,所以最简单的就是`LinearLayout`,里面的容器就不用说了,子条目都是垂直向下排列,肯定也是`LinearLayout`。是直接继承`LinearLayout`后动态向里面添加两个`LinearLayout`?还是使用组合控件?考虑到之前博客中自定义控件系列没有讲到组合控件,就这个机会写个小demo填充空白。那下面就开始了(不要嫌我啰嗦,大神们如果觉得太**easy**请口下留人,这些实现思路我想对很多人还是有帮助的) 16 | 17 | ##2、定义组合控件布局 18 | 19 |   组合控件,顾名思义就是由很多个控件组合而成,这里第一步就是定义好这些控件组合: 20 | ```xml 21 | 22 | 26 | 31 | 36 | 37 | ``` 38 | 39 | ##3、继承最外层控件 40 |   上面的控件组合定义好之后,下面就需要用一个类去形容他,那这个类就是组合控件了。用什么形容他合适呢?那就看控件组合最外层用的是什么,这里最外层是`LinearLayout`,那就定义一个类继承`LinearLayout`,然后覆盖其构造方法,使用`LayoutInflater`将控件组合挂在自己身上,并完成容器内控件的初始化: 41 | ```Java 42 | public class LimitScrollerView extends LinearLayout{ 43 | private LinearLayout ll_content1, ll_content2; //展示容器 和 预备容器 44 | public LimitScrollerView(Context context) { 45 | this(context, null); 46 | } 47 | 48 | public LimitScrollerView(Context context, AttributeSet attrs) { 49 | this(context, attrs, 0); 50 | } 51 | 52 | public LimitScrollerView(Context context, AttributeSet attrs, int defStyleAttr) { 53 | super(context, attrs, defStyleAttr); 54 | init(context, attrs); 55 | } 56 | 57 | private void init(Context context, AttributeSet attrs) { 58 | //将控件组合挂载到自己身上 59 | LayoutInflater.from(context).inflate(R.layout.limit_scroller, this, true); 60 | ll_content1 = (LinearLayout) findViewById(R.id.ll_content1); 61 | ll_content2 = (LinearLayout) findViewById(R.id.ll_content2); 62 | } 63 | } 64 | ``` 65 | 66 | ##4、自定义属性 67 | 68 |   为了达到通用的效果,自定义属性是必不可少的(自定义属性详解请参见: [Android自定义View(二、深入解析自定义属性)](http://blog.csdn.net/xmxkf/article/details/51468648))。这里需要定义的是:一次显示的条目数量、滚动动画持续时间、停留时间,如下: 69 | ```zml 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | ``` 82 |   然后就是使用这个自定义的控件了,在使用的时候可以指定属性值: 83 | ```xml 84 | 91 | ``` 92 |   最后需要在控件初始化的时候,获取到属性值: 93 | ```Java 94 | private void init(Context context, AttributeSet attrs){ 95 | LayoutInflater.from(context).inflate(R.layout.limit_scroller, this, true); 96 | ll_content1 = (LinearLayout) findViewById(R.id.ll_content1); 97 | ll_content2 = (LinearLayout) findViewById(R.id.ll_content2); 98 | if(attrs!=null){ 99 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LimitScroller); 100 | limit = ta.getInt(R.styleable.LimitScroller_limit, 1); 101 | durationTime = ta.getInt(R.styleable.LimitScroller_durationTime, 1000); 102 | periodTime = ta.getInt(R.styleable.LimitScroller_periodTime, 1000); 103 | ta.recycle(); //注意回收 104 | Log.v(TAG, "limit="+limit); 105 | Log.v(TAG, "durationTime="+durationTime); 106 | Log.v(TAG, "periodTime="+periodTime); 107 | } 108 | } 109 | ``` 110 | 111 | 112 | ##5、重写onMeasure 113 |   由于每次只能显示需要展示的容器,遮盖预备容器,所以只能设置整个高度的一半,这里使用一个小技巧,由于最外层是`LinearLayout`,并且是竖直向下的,自带的`LinearLayout`的`onMeasure()`方法完成之后组合控件的高度就是两个子容器的高度了,所以直接调用`super.onMeasuer()`之后,再设置高度为`getMeasureHeight()/2`即可(`onMeasure()`详解请移步: [Android自定义View(三、深入解析控件测量onMeasure)](http://blog.csdn.net/xmxkf/article/details/51490283)) 114 | ```Java 115 | @Override 116 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 117 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 118 | //设置高度为整体高度的一般,以达到遮盖预备容器的效果 119 | setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight()/2); 120 | //此处记下控件的高度,此高度就是动画执行时向上滚动的高度 121 | scrollHeight = getMeasuredHeight(); 122 | } 123 | ``` 124 | 125 | ##6、数据适配器 126 |   上面的步骤完成之后,展示的框架已经搭好了,但是运行之后是看不到控件的,因为容器中还没有子条目,整个控件的高度是0,下面就开始绑定数据、动态添加子条目。由于大家对`ListView`的数据填充模式已经很熟练,所以这里模仿`Adapter`的方式: 127 | ```java 128 | /**数据适配器*/ 129 | interface LimitScrllAdapter{ 130 | public int getCount(); 131 | public View getView(int index); 132 | } 133 | private LimitScrllAdapter adapter; 134 | 135 | public void setDataAdapter(LimitScrllAdapter adapter){ 136 | this.adapter = adapter; 137 | handler.sendEmptyMessage(MSG_SETDATA); 138 | } 139 | ``` 140 |   在`Activity`请求数据完毕后,为适配器添加数据,这里需要实现`LimitScrollAdapter`的两个抽象方法,使用方式和`ListView`一样,这里就不赘述: 141 | ```Java 142 | class MyLimitScrllAdapter implements LimitScrollerView.LimitScrllAdapter{ 143 | 144 | private List datas; 145 | public void setDatas(List datas){ 146 | this.datas = datas; 147 | //API:2、开始滚动 148 | limitScroll.startScroll(); 149 | } 150 | @Override 151 | public int getCount() { 152 | return datas==null?0:datas.size(); 153 | } 154 | 155 | @Override 156 | public View getView(int index) { 157 | View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.limit_scroller_item, null, false); 158 | ImageView iv_icon = (ImageView)itemView.findViewById(R.id.iv_icon); 159 | TextView tv_text = (TextView)itemView.findViewById(R.id.tv_text); 160 | 161 | //绑定数据 162 | DataBean data = datas.get(index); 163 | itemView.setTag(data); 164 | iv_icon.setImageResource(data.getIcon()); 165 | tv_text.setText(data.getText()); 166 | return itemView; 167 | } 168 | } 169 | ``` 170 | 171 | ##7、动态添加子条目 172 |   数据有了,子条目通过`adapter.getView()`获取,那什么时候向容器中添加条目呢?第一次肯定是两个容器中都得添加,向上滚动之后,有一个容器被定到上面,然后复位到预备位置了,但是他的数据还是之前的数据,所以每次动画结束之后得为预备容器更新新的子条目: 173 | ```Java 174 | private void boundData(boolean first){ 175 | if(adapter==null || adapter.getCount()<=0) 176 | return; 177 | if(first){ 178 | //第一次绑定数据,需要为两个容器添加子条目 179 | boundData = true; 180 | ll_now.removeAllViews(); 181 | for(int i = 0; i=adapter.getCount()) 183 | dataIndex = 0; 184 | View view = adapter.getView(dataIndex); 185 | ll_now.addView(view); 186 | dataIndex ++; 187 | } 188 | } 189 | 190 | //每次动画结束之后,为预备容器添加新条目 191 | ll_down.removeAllViews(); 192 | for(int i = 0; i=adapter.getCount()) 194 | dataIndex = 0; 195 | View view = adapter.getView(dataIndex); 196 | ll_down.addView(view); 197 | dataIndex ++; 198 | } 199 | } 200 | ``` 201 | ##8、滚动动画 202 |   什么时候开始动画?这是个需要考虑的问题,没有数据的时候肯定不需要吧?有数据之后,`activity`不可见了也不需要动画,所以这里需要提供接口让`activity`中控制,`Activity`中请求完数据之后调用此接口开始动画,在`onStart()`中也需要调用开启动画,在`onStop()`中调用停止动画的接口。动画开启之后会无限循环的执行,每次动画执行完毕后通过`Handler`发送一个延迟指定时间的消息,停留指定时间后,`handler`收到消息后又调用`startAnimation()`方法: 203 | ```Java 204 | private final int MSG_SETDATA = 1; 205 | private final int MSG_SCROL = 2; 206 | private Handler handler = new Handler(){ 207 | @Override 208 | public void handleMessage(Message msg) { 209 | if(msg.what == MSG_SETDATA){ 210 | boundData(true); 211 | }else if(msg.what == MSG_SCROL){ 212 | //继续动画 213 | startAnimation(); 214 | } 215 | } 216 | }; 217 | ``` 218 | ```Java 219 | private void startAnimation(){ 220 | if(isCancel) 221 | return; 222 | //当前展示的容器,从当前位置(0),向上滚动scrollHeight 223 | ObjectAnimator anim1 = ObjectAnimator.ofFloat(ll_now, "Y",ll_now.getY(), ll_now.getY()-scrollHeight); 224 | //预备容器,从当前位置,向上滚动scrollHeight 225 | ObjectAnimator anim2 = ObjectAnimator.ofFloat(ll_down, "Y",ll_down.getY(), ll_down.getY()-scrollHeight); 226 | AnimatorSet animSet = new AnimatorSet(); 227 | animSet.setDuration(durationTime); 228 | animSet.playTogether(anim1, anim2); 229 | animSet.addListener(new Animator.AnimatorListener() { 230 | @Override 231 | public void onAnimationStart(Animator animation) { 232 | } 233 | @Override 234 | public void onAnimationEnd(Animator animation) { 235 | //滚动结束后,now的位置变成了-scrollHeight,这时将他移动到最底下 236 | ll_now.setY(scrollHeight); 237 | //down的位置变为0,也就是当前看见的 238 | ll_down.setY(0); 239 | //引用交换 240 | LinearLayout temp = ll_now; 241 | ll_now = ll_down; 242 | ll_down = temp; 243 | //给不可见的控件绑定新数据 244 | boundData(false); 245 | //停留指定时间后,重复动画 246 | handler.sendEmptyMessageDelayed(MSG_SCROL, periodTime); 247 | } 248 | @Override 249 | public void onAnimationCancel(Animator animation) { 250 | } 251 | @Override 252 | public void onAnimationRepeat(Animator animation) { 253 | } 254 | }); 255 | animSet.start(); 256 | } 257 | ``` 258 | ```Java 259 | /** 260 | * 2、开始滚动 261 | * 应该在两处调用此方法: 262 | * ①、Activity.onStart() 263 | * ②、MyLimitScrllAdapter.setDatas() 264 | */ 265 | public void startScroll(){ 266 | if(adapter==null||adapter.getCount()<=0) 267 | return; 268 | if(!boundData){ 269 | handler.sendEmptyMessage(MSG_SETDATA); 270 | } 271 | isCancel = false; 272 | handler.sendEmptyMessageDelayed(MSG_SCROL, periodTime); 273 | } 274 | /** 275 | * 3、停止滚动 276 | * 当在Activity不可见时,在Activity.onStop()中调用 277 | */ 278 | public void cancel(){ 279 | isCancel = true; 280 | } 281 | ``` 282 | 283 | ##9、条目点击事件 284 |   在组合控件中写一个条目点击事件的接口,在动态添加子条目时,为子条目添加点击事件,通过`view.getTag()`(数据适配器绑定数据时,将数据对象设置给子条目view)将当前点击的子条目对应的数据对象返回即可: 285 | ```Java 286 | interface OnItemClickListener{ 287 | public void onItemClick(Object obj); 288 | } 289 | private OnItemClickListener clickListener; 290 | ``` 291 | ```Java 292 | /** 293 | * 向容器中添加子条目 294 | * @param first 295 | */ 296 | private void boundData(boolean first){ 297 | if(adapter==null || adapter.getCount()<=0) 298 | return; 299 | if(first){ 300 | //第一次绑定数据,需要为两个容器添加子条目 301 | boundData = true; 302 | ll_now.removeAllViews(); 303 | for(int i = 0; i=adapter.getCount()) 305 | dataIndex = 0; 306 | View view = adapter.getView(dataIndex); 307 | 308 | //设置点击监听 309 | view.setClickable(true); 310 | view.setOnClickListener(this); 311 | 312 | ll_now.addView(view); 313 | dataIndex ++; 314 | } 315 | } 316 | 317 | //每次动画结束之后,为预备容器添加新条目 318 | ll_down.removeAllViews(); 319 | for(int i = 0; i=adapter.getCount()) 321 | dataIndex = 0; 322 | View view = adapter.getView(dataIndex); 323 | //设置点击监听 324 | view.setClickable(true); 325 | view.setOnClickListener(this); 326 | ll_down.addView(view); 327 | dataIndex ++; 328 | } 329 | } 330 | 331 | @Override 332 | public void onClick(View v) { 333 | if(clickListener!=null){ 334 | Object obj = v.getTag(); 335 | clickListener.onItemClick(obj); 336 | } 337 | } 338 | ``` 339 | 340 |   好了,该考虑的基本上都有了,看看最终的效果: 341 | 342 |         ![这里写图片描述](http://img.blog.csdn.net/20161123122236180) --------------------------------------------------------------------------------