Skip to content

Commit

Permalink
代码优化,防止数组越界异常
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohaibin committed Aug 23, 2019
1 parent be28702 commit 725c157
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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')
}
17 changes: 10 additions & 7 deletions app/src/main/java/com/stx/xmarqueeviewdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class MainActivity extends AppCompatActivity {

private List<String> mDatas;
private MarqueeViewAdapter mMarqueeViewAdapter;
private XMarqueeView mXMarqueeView;
private MarqueeViewAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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));
}
//刷新数据
Expand Down
35 changes: 25 additions & 10 deletions xmarqueeview/src/main/java/com/stx/xmarqueeview/XMarqueeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down

0 comments on commit 725c157

Please sign in to comment.