Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Feb 26, 2017
1 parent cc23c82 commit 6f19a50
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sunfusheng.StickyHeaderListView.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -14,23 +13,19 @@
public class HeaderBannerAdapter extends PagerAdapter {

private List<ImageView> ivList; // ImageView的集合
private int count = 1; // 广告的数量
private int count; // 广告的数量

public HeaderBannerAdapter(Context context, List<ImageView> ivList) {
public HeaderBannerAdapter(List<ImageView> ivList) {
super();
this.ivList = ivList;
if(ivList != null && ivList.size() > 0){
if(ivList != null){
count = ivList.size();
}
}

@Override
public int getCount() {
if (count == 1) {
return 1;
} else {
return Integer.MAX_VALUE;
}
return Integer.MAX_VALUE;
}

@Override
Expand All @@ -45,7 +40,7 @@ public void destroyItem(ViewGroup container, int position, Object object) {
@Override
public Object instantiateItem(ViewGroup container, int position) {
int newPosition = position % count;
// 先移除在添加,更新图片在container中的位置(把iv放至container末尾)
// 先移除再添加,更新图片在container中的位置(把iv放至container末尾)
ImageView iv = ivList.get(newPosition);
container.removeView(iv);
container.addView(iv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mDuration);
}

public void setmDuration(int time) {
public void setDuration(int time) {
mDuration = time;
}

public int getmDuration() {
return mDuration;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,36 @@ protected void getView(List<String> list, ListView listView) {

private void dealWithTheView(List<String> list) {
ivList.clear();

bannerCount = list.size();
if (bannerCount == 2) {
list.addAll(list);
}
for (int i = 0; i < list.size(); i++) {
ivList.add(createImageView(list.get(i)));
}

AbsListView.LayoutParams layoutParams = (AbsListView.LayoutParams) rlBanner.getLayoutParams();
layoutParams.height = bannerHeight;
rlBanner.setLayoutParams(layoutParams);

HeaderBannerAdapter photoAdapter = new HeaderBannerAdapter(mActivity, ivList);
vpBanner.setAdapter(photoAdapter);
createImageViews(list);

HeaderBannerAdapter adapter = new HeaderBannerAdapter(ivList);
vpBanner.setAdapter(adapter);

addIndicatorImageViews();
setViewPagerChangeListener();
controlViewPagerSpeed(vpBanner, 500);
}

// 创建要显示的ImageView
private ImageView createImageView(String url) {
GlideImageView imageView = new GlideImageView(mActivity);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
imageView.loadNetImage(url, R.color.font_black_6);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
private void createImageViews(List<String> list) {
for (int i = 0; i < list.size(); i++) {
GlideImageView imageView = new GlideImageView(mActivity);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
imageView.loadNetImage(list.get(i), R.color.font_black_6);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
ivList.add(imageView);
}
}

// 添加指示图标
Expand All @@ -105,15 +107,10 @@ private void addIndicatorImageViews() {
for (int i = 0; i < bannerCount; i++) {
ImageView iv = new ImageView(mActivity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DensityUtil.dip2px(mActivity, 5), DensityUtil.dip2px(mActivity, 5));
if (i != 0) {
lp.leftMargin = DensityUtil.dip2px(mActivity, 7);
}
lp.leftMargin = DensityUtil.dip2px(mActivity, (i == 0) ? 0 : 7);
iv.setLayoutParams(lp);
iv.setBackgroundResource(R.drawable.xml_round_orange_grey_sel);
iv.setEnabled(false);
if (i == 0) {
iv.setEnabled(true);
}
iv.setEnabled(i == 0);
llIndexContainer.addView(iv);
}
}
Expand All @@ -123,14 +120,10 @@ private void setViewPagerChangeListener() {
vpBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (ivList != null && ivList.size() > 0) {
int newPosition = position % bannerCount;
for (int i = 0; i < bannerCount; i++) {
llIndexContainer.getChildAt(i).setEnabled(false);
if (i == newPosition) {
llIndexContainer.getChildAt(i).setEnabled(true);
}
}
if (ivList == null || ivList.size() == 0) return;
int newPosition = position % bannerCount;
for (int i = 0; i < bannerCount; i++) {
llIndexContainer.getChildAt(i).setEnabled(i == newPosition);
}
}

Expand Down Expand Up @@ -163,13 +156,10 @@ private void controlViewPagerSpeed(ViewPager viewPager, int speedTimeMillis) {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
FixedSpeedScroller scroller = new FixedSpeedScroller(mActivity, new AccelerateDecelerateInterpolator());
scroller.setmDuration(speedTimeMillis);
scroller.setDuration(speedTimeMillis);
field.set(viewPager, scroller);
} catch (Exception e) {
}
}

public int getBannerHeight() {
return bannerHeight;
}
}

0 comments on commit 6f19a50

Please sign in to comment.