Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Nov 21, 2016
1 parent b40573e commit 0492be8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

## StickyHeaderListView

StickyHeaderListView 是基于实际需求做出的灵活可定制的UI功能,具体实现功能如下:<br/>
一、支持无限循环的广告位。<br/>
二、高度可动态配置的Header2和Header3(使用GridView实现)。<br/>
三、主要功能:分类、排序和筛选布局滑动到顶部后吸附、悬停。<br/>
四、自定义FilterView筛选控件,支持动画显示与动画隐藏。<br/>
五、支持标题栏背景颜色渐变、字体颜色渐变。<br/>
六、数据不足一屏动态添加空数据占位。<br/>
七、数据为空时,ListView加载暂无数据视图。<br/>
八、思路清晰、界面优美,添加ripple点击效果。<br/>
九、支持下拉刷新和上拉加载更多功能。<br/>
StickyHeaderListView是基于实际需求做出的灵活可变的UI视图,具体实现了如下功能:
一、支持下拉刷新和上拉加载更多功能。
二、支持无限循环的广告位。
三、使用GridView实现可动态配置的频道位、运营位和分割线。
四、自定义FilterView实现筛选功能,同时支持动画显示与动画隐藏。
五、支持FilterView滑动到顶部后吸附悬浮。
六、支持标题栏背景颜色渐变和字体颜色渐变。
七、实现了数据不足一屏动态添加空数据占位。
八、数据为空时ListView多type加载暂无数据视图。

### 动态效果图

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class MainActivity extends AppCompatActivity implements SmoothListView.IS
private Activity mActivity;
private int mScreenHeight; // 屏幕高度

private List<String> adList = new ArrayList<>(); // 广告数据
private List<String> bannerList = new ArrayList<>(); // 广告数据
private List<ChannelEntity> channelList = new ArrayList<>(); // 频道数据
private List<OperationEntity> operationList = new ArrayList<>(); // 运营数据
private List<TravelingEntity> travelingList = new ArrayList<>(); // ListView数据
Expand All @@ -77,19 +77,19 @@ public class MainActivity extends AppCompatActivity implements SmoothListView.IS
private FilterData filterData; // 筛选数据
private TravelingAdapter mAdapter; // 主页数据

private View itemHeaderAdView; // 从ListView获取的广告子View
private View itemHeaderBannerView; // 从ListView获取的广告子View
private View itemHeaderFilterView; // 从ListView获取的筛选子View
private boolean isScrollIdle = true; // ListView是否在滑动
private boolean isStickyTop = false; // 是否吸附在顶部
private boolean isSmooth = false; // 没有吸附的前提下,是否在滑动
private int titleViewHeight = 65; // 标题栏的高度
private int filterPosition = -1; // 点击FilterView的位置:分类(0)、排序(1)、筛选(2)

private int adViewHeight = 180; // 广告视图的高度
private int adViewTopSpace; // 广告视图距离顶部的距离
private int bannerViewHeight = 180; // 广告视图的高度
private int bannerViewTopMargin; // 广告视图距离顶部的距离

private int filterViewPosition = 4; // 筛选视图的位置
private int filterViewTopSpace; // 筛选视图距离顶部的距离
private int filterViewTopMargin; // 筛选视图距离顶部的距离

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -115,14 +115,11 @@ private void initData() {
filterData.setFilters(ModelUtil.getFilterData());

// 广告数据
adList = ModelUtil.getAdData();

bannerList = ModelUtil.getAdData();
// 频道数据
channelList = ModelUtil.getChannelData();

// 运营数据
operationList = ModelUtil.getOperationData();

// ListView数据
travelingList = ModelUtil.getTravelingData();
}
Expand All @@ -135,7 +132,7 @@ private void initView() {

// 设置广告数据
headerBannerView = new HeaderBannerView(this);
headerBannerView.fillView(adList, smoothListView);
headerBannerView.fillView(bannerList, smoothListView);

// 设置频道数据
headerChannelView = new HeaderChannelView(this);
Expand Down Expand Up @@ -186,7 +183,7 @@ public void onFilterClick(int position) {
if (isStickyTop) {
filterPosition = position;
fvTopFilter.showFilterLayout(position);
if (titleViewHeight - 3 > filterViewTopSpace || filterViewTopSpace > titleViewHeight + 3) {
if (titleViewHeight - 3 > filterViewTopMargin || filterViewTopMargin > titleViewHeight + 3) {
smoothListView.smoothScrollToPositionFromTop(filterViewPosition, DensityUtil.dip2px(mContext, titleViewHeight));
}
}
Expand Down Expand Up @@ -231,37 +228,32 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (isScrollIdle && adViewTopSpace < 0) return;
if (isScrollIdle && bannerViewTopMargin < 0) return;

// 获取广告头部View、自身的高度、距离顶部的高度
if (itemHeaderAdView == null) {
itemHeaderAdView = smoothListView.getChildAt(1-firstVisibleItem);
if (itemHeaderBannerView == null) {
itemHeaderBannerView = smoothListView.getChildAt(1-firstVisibleItem);
}
if (itemHeaderAdView != null) {
adViewTopSpace = DensityUtil.px2dip(mContext, itemHeaderAdView.getTop());
adViewHeight = DensityUtil.px2dip(mContext, itemHeaderAdView.getHeight());
if (itemHeaderBannerView != null) {
bannerViewTopMargin = DensityUtil.px2dip(mContext, itemHeaderBannerView.getTop());
bannerViewHeight = DensityUtil.px2dip(mContext, itemHeaderBannerView.getHeight());
}

// 获取筛选View、距离顶部的高度
if (itemHeaderFilterView == null) {
itemHeaderFilterView = smoothListView.getChildAt(filterViewPosition - firstVisibleItem);
}
if (itemHeaderFilterView != null) {
filterViewTopSpace = DensityUtil.px2dip(mContext, itemHeaderFilterView.getTop());
filterViewTopMargin = DensityUtil.px2dip(mContext, itemHeaderFilterView.getTop());
}

// 处理筛选是否吸附在顶部
if (filterViewTopSpace > titleViewHeight) {
isStickyTop = false; // 没有吸附在顶部
fvTopFilter.setVisibility(View.GONE);
} else {
if (filterViewTopMargin <= titleViewHeight || firstVisibleItem > filterViewPosition) {
isStickyTop = true; // 吸附在顶部
fvTopFilter.setVisibility(View.VISIBLE);
}

if (firstVisibleItem > filterViewPosition) {
isStickyTop = true;
fvTopFilter.setVisibility(View.VISIBLE);
} else {
isStickyTop = false; // 没有吸附在顶部
fvTopFilter.setVisibility(View.GONE);
}

if (isSmooth && isStickyTop) {
Expand Down Expand Up @@ -290,15 +282,15 @@ private void fillAdapter(List<TravelingEntity> list) {
// 处理标题栏颜色渐变
private void handleTitleBarColorEvaluate() {
float fraction;
if (adViewTopSpace > 0) {
fraction = 1f - adViewTopSpace * 1f / 60;
if (bannerViewTopMargin > 0) {
fraction = 1f - bannerViewTopMargin * 1f / 60;
if (fraction < 0f) fraction = 0f;
rlBar.setAlpha(fraction);
return ;
}

float space = Math.abs(adViewTopSpace) * 1f;
fraction = space / (adViewHeight - titleViewHeight);
float space = Math.abs(bannerViewTopMargin) * 1f;
fraction = space / (bannerViewHeight - titleViewHeight);
if (fraction < 0f) fraction = 0f;
if (fraction > 1f) fraction = 1f;
rlBar.setAlpha(1f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,7 @@ private void controlViewPagerSpeed(ViewPager viewPager, int speedTimeMillis) {
}
}

public int getBannerHeight() {
return bannerHeight;
}
}

0 comments on commit 0492be8

Please sign in to comment.