Skip to content

Commit

Permalink
V1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Nov 30, 2016
1 parent 5b810b0 commit 20b86d6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,76 @@
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);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
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<? extends Window> 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<? extends Window> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Created by sunfusheng on 15/8/7.
*/
public class ToastTip {
public class ToastUtil {

private static Toast mToast;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class HeaderBannerView extends HeaderViewInterface<List<String>> {
private List<ImageView> ivList;
private ImageManager mImageManager;
private int bannerHeight;
private int bannerCount;

private Handler mHandler = new Handler() {
@Override
Expand Down Expand Up @@ -69,26 +70,29 @@ protected void getView(List<String> list, ListView listView) {

private void dealWithTheView(List<String> 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)));
}

AbsListView.LayoutParams layoutParams = (AbsListView.LayoutParams) rlBanner.getLayoutParams();
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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,25 +37,25 @@ protected void getView(List<ChannelEntity> list, ListView listView) {
}

private void dealWithTheView(final List<ChannelEntity> list) {
if (list == null || list.size() < 2) return;
int size = list.size();

if (size <= 4) {
gvChannel.setNumColumns(size);
} else if (size == 6) {
gvChannel.setNumColumns(3);
} 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());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,13 +37,16 @@ protected void getView(List<OperationEntity> list, ListView listView) {
}

private void dealWithTheView(List<OperationEntity> 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());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

public abstract class HeaderViewInterface<T> {

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) {
Expand Down

0 comments on commit 20b86d6

Please sign in to comment.