From 20b86d6e2eb7b81af2489af1d9233e69a0d63292 Mon Sep 17 00:00:00 2001 From: sfsheng0322 Date: Wed, 30 Nov 2016 11:34:12 +0800 Subject: [PATCH] V1.6 --- .../adapter/TravelingAdapter.java | 4 +- .../StickyHeaderListView/ui/MainActivity.java | 2 +- .../util/StatusBarUtil.java | 114 ++++++++---------- .../util/{ToastTip.java => ToastUtil.java} | 2 +- .../view/HeaderBannerView.java | 35 +++--- .../view/HeaderChannelView.java | 10 +- .../view/HeaderOperationView.java | 9 +- .../view/HeaderViewInterface.java | 8 +- 8 files changed, 90 insertions(+), 94 deletions(-) rename app/src/main/java/com/sunfusheng/StickyHeaderListView/util/{ToastTip.java => ToastUtil.java} (97%) diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/adapter/TravelingAdapter.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/adapter/TravelingAdapter.java index 5e9a15e..8dca6e4 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/adapter/TravelingAdapter.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/adapter/TravelingAdapter.java @@ -12,7 +12,7 @@ import com.sunfusheng.StickyHeaderListView.R; import com.sunfusheng.StickyHeaderListView.model.TravelingEntity; -import com.sunfusheng.StickyHeaderListView.util.ToastTip; +import com.sunfusheng.StickyHeaderListView.util.ToastUtil; import java.util.ArrayList; import java.util.List; @@ -104,7 +104,7 @@ public View getView(int position, View convertView, ViewGroup parent) { convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - ToastTip.show(mContext, title); + ToastUtil.show(mContext, title); } }); diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/ui/MainActivity.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/ui/MainActivity.java index 86785c9..7bce931 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/ui/MainActivity.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/ui/MainActivity.java @@ -96,7 +96,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); - StatusBarUtil.setStatusBarTrans(this, false); + StatusBarUtil.setStatusBarTranslucent(this, false); initData(); initView(); diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/StatusBarUtil.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/StatusBarUtil.java index 2c1652a..91923f7 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/StatusBarUtil.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/StatusBarUtil.java @@ -16,11 +16,11 @@ public class StatusBarUtil { // 设置状态栏透明与字体颜色 - public static void setStatusBarTrans(Activity acitivty, boolean lightStatusBar) { + public static void setStatusBarTranslucent(Activity acitivty, boolean isLightStatusBar) { + Window window = acitivty.getWindow(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - acitivty.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); - }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - Window window = acitivty.getWindow(); + window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); @@ -28,76 +28,64 @@ public static void setStatusBarTrans(Activity acitivty, boolean lightStatusBar) window.setStatusBarColor(Color.TRANSPARENT); } - ILightStatusBar IMPL; - if (MIUILightStatusBarImpl.isMe()) { - IMPL = new MIUILightStatusBarImpl(); - } else if (MeizuLightStatusBarImpl.isMe()) { - IMPL = new MeizuLightStatusBarImpl(); - } else { - IMPL = new ILightStatusBar() { - @Override - public void setLightStatusBar(Window window, boolean lightStatusBar) { - } - }; + if (isXiaomi()) { + setXiaomiStatusBar(window, isLightStatusBar); + } else if (isMeizu()) { + setMeizuStatusBar(window, isLightStatusBar); } - IMPL.setLightStatusBar(acitivty.getWindow(), lightStatusBar); } - // 小米状态栏设置类 - public static class MIUILightStatusBarImpl implements ILightStatusBar { - static boolean isMe() { - return "Xiaomi".equals(Build.MANUFACTURER); - } + // 是否是小米手机 + public static boolean isXiaomi() { + return "Xiaomi".equals(Build.MANUFACTURER); + } - public void setLightStatusBar(Window window, boolean lightStatusBar) { - Class clazz = window.getClass(); - try { - Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); - Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); - int darkModeFlag = field.getInt(layoutParams); - Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); - extraFlagField.invoke(window, lightStatusBar ? darkModeFlag : 0, darkModeFlag); - } catch (Exception e) { - } + // 设置小米状态栏 + public static void setXiaomiStatusBar(Window window, boolean isLightStatusBar) { + Class clazz = window.getClass(); + try { + Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); + Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); + int darkModeFlag = field.getInt(layoutParams); + Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); + extraFlagField.invoke(window, isLightStatusBar ? darkModeFlag : 0, darkModeFlag); + } catch (Exception e) { + e.printStackTrace(); } } - // 魅族状态栏设置类 - public static class MeizuLightStatusBarImpl implements ILightStatusBar { - static boolean isMe() { - final Method method; - try { - method = Build.class.getMethod("hasSmartBar"); - return method != null; - } catch (NoSuchMethodException e) { - } - return false; + // 是否是魅族手机 + public static boolean isMeizu() { + try { + Method method = Build.class.getMethod("hasSmartBar"); + return method != null; + } catch (NoSuchMethodException e) { } + return false; + } - public void setLightStatusBar(Window window, boolean lightStatusBar) { - WindowManager.LayoutParams params = window.getAttributes(); - try { - Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); - Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); - darkFlag.setAccessible(true); - meizuFlags.setAccessible(true); - int bit = darkFlag.getInt(null); - int value = meizuFlags.getInt(params); - if (lightStatusBar) { - value |= bit; - } else { - value &= ~bit; - } - meizuFlags.setInt(params, value); - window.setAttributes(params); - darkFlag.setAccessible(false); - meizuFlags.setAccessible(false); - } catch (Exception e) { + // 设置魅族状态栏 + public static void setMeizuStatusBar(Window window, boolean isLightStatusBar) { + WindowManager.LayoutParams params = window.getAttributes(); + try { + Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); + Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); + darkFlag.setAccessible(true); + meizuFlags.setAccessible(true); + int bit = darkFlag.getInt(null); + int value = meizuFlags.getInt(params); + if (isLightStatusBar) { + value |= bit; + } else { + value &= ~bit; } + meizuFlags.setInt(params, value); + window.setAttributes(params); + darkFlag.setAccessible(false); + meizuFlags.setAccessible(false); + } catch (Exception e) { + e.printStackTrace(); } } - interface ILightStatusBar { - void setLightStatusBar(Window window, boolean lightStatusBar); - } } diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/ToastTip.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/ToastUtil.java similarity index 97% rename from app/src/main/java/com/sunfusheng/StickyHeaderListView/util/ToastTip.java rename to app/src/main/java/com/sunfusheng/StickyHeaderListView/util/ToastUtil.java index 7d51069..70fc745 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/ToastTip.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/util/ToastUtil.java @@ -7,7 +7,7 @@ /** * Created by sunfusheng on 15/8/7. */ -public class ToastTip { +public class ToastUtil { private static Toast mToast; diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderBannerView.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderBannerView.java index 9933f80..dbd5bae 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderBannerView.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderBannerView.java @@ -39,6 +39,7 @@ public class HeaderBannerView extends HeaderViewInterface> { private List ivList; private ImageManager mImageManager; private int bannerHeight; + private int bannerCount; private Handler mHandler = new Handler() { @Override @@ -69,8 +70,11 @@ protected void getView(List list, ListView listView) { private void dealWithTheView(List list) { ivList.clear(); - int size = list.size(); - for (int i = 0; i < size; i++) { + bannerCount = list.size(); + if (bannerCount == 2) { + list.addAll(list); + } + for (int i = 0; i < list.size(); i++) { ivList.add(createImageView(list.get(i))); } @@ -78,17 +82,17 @@ private void dealWithTheView(List list) { layoutParams.height = bannerHeight; rlBanner.setLayoutParams(layoutParams); - HeaderBannerAdapter photoAdapter = new HeaderBannerAdapter(mContext, ivList); + HeaderBannerAdapter photoAdapter = new HeaderBannerAdapter(mActivity, ivList); vpBanner.setAdapter(photoAdapter); - addIndicatorImageViews(size); - setViewPagerChangeListener(size); + addIndicatorImageViews(); + setViewPagerChangeListener(); controlViewPagerSpeed(vpBanner, 500); } // 创建要显示的ImageView private ImageView createImageView(String url) { - ImageView imageView = new ImageView(mContext); + ImageView imageView = new ImageView(mActivity); AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); imageView.setLayoutParams(params); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); @@ -97,13 +101,14 @@ private ImageView createImageView(String url) { } // 添加指示图标 - private void addIndicatorImageViews(int size) { + private void addIndicatorImageViews() { llIndexContainer.removeAllViews(); - for (int i = 0; i < size; i++) { - ImageView iv = new ImageView(mContext); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 5)); + if (bannerCount < 2) return; + 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(mContext, 7); + lp.leftMargin = DensityUtil.dip2px(mActivity, 7); } iv.setLayoutParams(lp); iv.setBackgroundResource(R.drawable.xml_round_orange_grey_sel); @@ -116,13 +121,13 @@ private void addIndicatorImageViews(int size) { } // 为ViewPager设置监听器 - private void setViewPagerChangeListener(final int size) { + private void setViewPagerChangeListener() { vpBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { if (ivList != null && ivList.size() > 0) { - int newPosition = position % size; - for (int i = 0; i < size; i++) { + int newPosition = position % bannerCount; + for (int i = 0; i < bannerCount; i++) { llIndexContainer.getChildAt(i).setEnabled(false); if (i == newPosition) { llIndexContainer.getChildAt(i).setEnabled(true); @@ -159,7 +164,7 @@ private void controlViewPagerSpeed(ViewPager viewPager, int speedTimeMillis) { try { Field field = ViewPager.class.getDeclaredField("mScroller"); field.setAccessible(true); - FixedSpeedScroller scroller = new FixedSpeedScroller(mContext, new AccelerateDecelerateInterpolator()); + FixedSpeedScroller scroller = new FixedSpeedScroller(mActivity, new AccelerateDecelerateInterpolator()); scroller.setmDuration(speedTimeMillis); field.set(viewPager, scroller); } catch (Exception e) { diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderChannelView.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderChannelView.java index 2fa4f1f..77cda5c 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderChannelView.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderChannelView.java @@ -8,7 +8,7 @@ import com.sunfusheng.StickyHeaderListView.R; import com.sunfusheng.StickyHeaderListView.adapter.HeaderChannelAdapter; import com.sunfusheng.StickyHeaderListView.model.ChannelEntity; -import com.sunfusheng.StickyHeaderListView.util.ToastTip; +import com.sunfusheng.StickyHeaderListView.util.ToastUtil; import java.util.List; @@ -37,8 +37,8 @@ protected void getView(List list, ListView listView) { } private void dealWithTheView(final List list) { + if (list == null || list.size() < 2) return; int size = list.size(); - if (size <= 4) { gvChannel.setNumColumns(size); } else if (size == 6) { @@ -46,16 +46,16 @@ private void dealWithTheView(final List list) { } else if (size == 8) { gvChannel.setNumColumns(4); } else { - gvChannel.setNumColumns(4); + return; } - final HeaderChannelAdapter adapter = new HeaderChannelAdapter(mContext, list); + final HeaderChannelAdapter adapter = new HeaderChannelAdapter(mActivity, list); gvChannel.setAdapter(adapter); gvChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - ToastTip.show(mContext, adapter.getItem(position).getTitle()); + ToastUtil.show(mActivity, adapter.getItem(position).getTitle()); } }); } diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderOperationView.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderOperationView.java index 084a5c5..610b93f 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderOperationView.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderOperationView.java @@ -8,7 +8,7 @@ import com.sunfusheng.StickyHeaderListView.R; import com.sunfusheng.StickyHeaderListView.adapter.HeaderOperationAdapter; import com.sunfusheng.StickyHeaderListView.model.OperationEntity; -import com.sunfusheng.StickyHeaderListView.util.ToastTip; +import com.sunfusheng.StickyHeaderListView.util.ToastUtil; import java.util.List; @@ -37,13 +37,16 @@ protected void getView(List list, ListView listView) { } private void dealWithTheView(List list) { - final HeaderOperationAdapter adapter = new HeaderOperationAdapter(mContext, list); + if (list == null || list.size() < 2 || list.size() > 6) return; + if (list.size()%2 != 0) return; + + final HeaderOperationAdapter adapter = new HeaderOperationAdapter(mActivity, list); gvOperation.setAdapter(adapter); gvOperation.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - ToastTip.show(mContext, adapter.getItem(position).getTitle()); + ToastUtil.show(mActivity, adapter.getItem(position).getTitle()); } }); } diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderViewInterface.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderViewInterface.java index 3f846ab..49cc5f1 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderViewInterface.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderViewInterface.java @@ -8,13 +8,13 @@ public abstract class HeaderViewInterface { - protected Activity mContext; + protected Activity mActivity; protected LayoutInflater mInflate; protected T mEntity; - public HeaderViewInterface(Activity context) { - this.mContext = context; - mInflate = LayoutInflater.from(context); + public HeaderViewInterface(Activity activity) { + this.mActivity = activity; + mInflate = LayoutInflater.from(activity); } public boolean fillView(T t, ListView listView) {