From 725c1575960585ef3cba4545a8388909a386344d Mon Sep 17 00:00:00 2001 From: "Mr.xiao" Date: Fri, 23 Aug 2019 23:05:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E6=95=B0=E7=BB=84=E8=B6=8A=E7=95=8C=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 6 ++-- .../stx/xmarqueeviewdemo/MainActivity.java | 17 +++++---- .../com/stx/xmarqueeview/XMarqueeView.java | 35 +++++++++++++------ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 78d435f..78b6eee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 + compileSdkVersion 28 buildToolsVersion '27.0.3' defaultConfig { applicationId "com.stx.xmarqueeviewdemo" minSdkVersion 15 - targetSdkVersion 25 + targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -21,6 +21,6 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:25.4.0' + implementation 'com.android.support:appcompat-v7:28.0.0' implementation project(':xmarqueeview') } diff --git a/app/src/main/java/com/stx/xmarqueeviewdemo/MainActivity.java b/app/src/main/java/com/stx/xmarqueeviewdemo/MainActivity.java index f2c3f99..05812bf 100644 --- a/app/src/main/java/com/stx/xmarqueeviewdemo/MainActivity.java +++ b/app/src/main/java/com/stx/xmarqueeviewdemo/MainActivity.java @@ -14,6 +14,8 @@ public class MainActivity extends AppCompatActivity { private List mDatas; private MarqueeViewAdapter mMarqueeViewAdapter; + private XMarqueeView mXMarqueeView; + private MarqueeViewAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -28,25 +30,26 @@ private void initView() { mDatas.add("这是跑马灯内容" + (i + 1)); } - XMarqueeView marqueeviewone = (XMarqueeView) findViewById(R.id.marquee1); - marqueeviewone.setAdapter(new MarqueeViewAdapter(mDatas, this)); + XMarqueeView marqueeviewone = findViewById(R.id.marquee1); + mAdapter = new MarqueeViewAdapter(mDatas, this); + marqueeviewone.setAdapter(mAdapter); marqueeviewone.stopFlipping(); - XMarqueeView marqueeviewtwo = (XMarqueeView) findViewById(R.id.marquee2); + XMarqueeView marqueeviewtwo = findViewById(R.id.marquee2); marqueeviewtwo.setAdapter(new MarqueeViewAdapter(mDatas, this)); - XMarqueeView xMarqueeView = (XMarqueeView) findViewById(R.id.marquee3); + mXMarqueeView = findViewById(R.id.marquee3); mMarqueeViewAdapter = new MarqueeViewAdapter(mDatas, this); - xMarqueeView.setAdapter(mMarqueeViewAdapter); - Button btn = (Button) findViewById(R.id.btn); + mXMarqueeView.setAdapter(mMarqueeViewAdapter); + Button btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mDatas.clear(); - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 2; i++) { mDatas.add("这是刷新后的跑马灯内容" + (i + 1)); } //刷新数据 diff --git a/xmarqueeview/src/main/java/com/stx/xmarqueeview/XMarqueeView.java b/xmarqueeview/src/main/java/com/stx/xmarqueeview/XMarqueeView.java index 5d069ba..0e30ee9 100644 --- a/xmarqueeview/src/main/java/com/stx/xmarqueeview/XMarqueeView.java +++ b/xmarqueeview/src/main/java/com/stx/xmarqueeview/XMarqueeView.java @@ -21,24 +21,36 @@ */ public class XMarqueeView extends ViewFlipper implements XMarqueeViewAdapter.OnDataChangedListener { - /** 是否设置动画时间间隔 */ + /** + * 是否设置动画时间间隔 + */ private boolean isSetAnimDuration = false; - /** 是否单行显示 */ + /** + * 是否单行显示 + */ private boolean isSingleLine = true; - /** 轮播间隔 */ + /** + * 轮播间隔 + */ private int interval = 3000; - /** 动画时间 */ + /** + * 动画时间 + */ private int animDuration = 1000; private int textSize = 14; private int textColor = Color.parseColor("#888888"); - /** 一次性显示多少个 */ + /** + * 一次性显示多少个 + */ private int itemCount = 1; private XMarqueeViewAdapter mMarqueeViewAdapter; - /** 当数据源少于一次性显示数目是否自动轮播标记 */ + /** + * 当数据源少于一次性显示数目是否自动轮播标记 + */ private boolean isFlippingLessCount = true; public XMarqueeView(Context context, AttributeSet attrs) { @@ -95,7 +107,9 @@ private void setData() { for (int i = 0; i < loopconunt; i++) { if (isSingleLine) { View view = mMarqueeViewAdapter.onCreateView(this); - mMarqueeViewAdapter.onBindView(view, view, currentIndex); + if (currentIndex < mMarqueeViewAdapter.getItemCount()) { + mMarqueeViewAdapter.onBindView(view, view, currentIndex); + } currentIndex = currentIndex + 1; addView(view); } else { @@ -107,7 +121,9 @@ private void setData() { View view = mMarqueeViewAdapter.onCreateView(this); parentView.addView(view); currentIndex = getRealPosition(j, currentIndex); - mMarqueeViewAdapter.onBindView(parentView, view, currentIndex); + if (currentIndex < mMarqueeViewAdapter.getItemCount()) { + mMarqueeViewAdapter.onBindView(parentView, view, currentIndex); + } } addView(parentView); } @@ -130,8 +146,7 @@ public void setFlippingLessCount(boolean flippingLessCount) { } private int getRealPosition(int index, int currentIndex) { - if ((index == 0 && currentIndex == 0) || - (currentIndex == mMarqueeViewAdapter.getItemCount() - 1)) { + if ((index == 0 && currentIndex == 0) || (currentIndex == mMarqueeViewAdapter.getItemCount() - 1)) { return 0; } else { return currentIndex + 1;