diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/MainActivity.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/MainActivity.java index ca4bfde..48e94a0 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/MainActivity.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/MainActivity.java @@ -39,15 +39,15 @@ /** * 作者:孙福生 - * + *

* 个人博客:sunfusheng.com */ public class MainActivity extends AppCompatActivity implements SmoothListView.ISmoothListViewListener { @BindView(R.id.listView) SmoothListView smoothListView; - @BindView(R.id.filterView) - FilterView filterView; + @BindView(R.id.real_filterView) + FilterView realFilterView; @BindView(R.id.rl_bar) RelativeLayout rlBar; @BindView(R.id.tv_title) @@ -76,19 +76,19 @@ public class MainActivity extends AppCompatActivity implements SmoothListView.IS private FilterData filterData; // 筛选数据 private TravelingAdapter mAdapter; - 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 View itemHeaderBannerView; // 从ListView获取的广告子View private int bannerViewHeight = 180; // 广告视图的高度 private int bannerViewTopMargin; // 广告视图距离顶部的距离 + private View itemHeaderFilterView; // 从ListView获取的筛选子View private int filterViewPosition = 4; // 筛选视图的位置 private int filterViewTopMargin; // 筛选视图距离顶部的距离 + private boolean isScrollIdle = true; // ListView是否在滑动 + private boolean isStickyTop = false; // 是否吸附在顶部 + private boolean isSmooth = false; // 没有吸附的前提下,是否在滑动 + private int filterPosition = -1; // 点击FilterView的位置:分类(0)、排序(1)、筛选(2) @Override protected void onCreate(Bundle savedInstanceState) { @@ -144,8 +144,8 @@ private void initView() { headerFilterView.fillView(new Object(), smoothListView); // 设置真FilterView数据 - filterView.setFilterData(mActivity, filterData); - filterView.setVisibility(View.GONE); + realFilterView.setFilterData(mActivity, filterData); + realFilterView.setVisibility(View.GONE); // 设置ListView数据 mAdapter = new TravelingAdapter(this, travelingList); @@ -174,21 +174,17 @@ public void onFilterClick(int position) { }); // (真正的)筛选视图点击 - filterView.setOnFilterClickListener(new FilterView.OnFilterClickListener() { + realFilterView.setOnFilterClickListener(new FilterView.OnFilterClickListener() { @Override public void onFilterClick(int position) { - if (isStickyTop) { - filterPosition = position; - filterView.show(position); - if (titleViewHeight - 3 > filterViewTopMargin || filterViewTopMargin > titleViewHeight + 3) { - smoothListView.smoothScrollToPositionFromTop(filterViewPosition, DensityUtil.dip2px(mContext, titleViewHeight)); - } - } + filterPosition = position; + realFilterView.show(position); + smoothListView.smoothScrollToPositionFromTop(filterViewPosition, DensityUtil.dip2px(mContext, titleViewHeight)); } }); // 分类Item点击 - filterView.setOnItemCategoryClickListener(new FilterView.OnItemCategoryClickListener() { + realFilterView.setOnItemCategoryClickListener(new FilterView.OnItemCategoryClickListener() { @Override public void onItemCategoryClick(FilterTwoEntity leftEntity, FilterEntity rightEntity) { fillAdapter(ModelUtil.getCategoryTravelingData(leftEntity, rightEntity)); @@ -196,7 +192,7 @@ public void onItemCategoryClick(FilterTwoEntity leftEntity, FilterEntity rightEn }); // 排序Item点击 - filterView.setOnItemSortClickListener(new FilterView.OnItemSortClickListener() { + realFilterView.setOnItemSortClickListener(new FilterView.OnItemSortClickListener() { @Override public void onItemSortClick(FilterEntity entity) { fillAdapter(ModelUtil.getSortTravelingData(entity)); @@ -204,7 +200,7 @@ public void onItemSortClick(FilterEntity entity) { }); // 筛选Item点击 - filterView.setOnItemFilterClickListener(new FilterView.OnItemFilterClickListener() { + realFilterView.setOnItemFilterClickListener(new FilterView.OnItemFilterClickListener() { @Override public void onItemFilterClick(FilterEntity entity) { fillAdapter(ModelUtil.getFilterTravelingData(entity)); @@ -216,7 +212,8 @@ public void onItemFilterClick(FilterEntity entity) { smoothListView.setSmoothListViewListener(this); smoothListView.setOnScrollListener(new SmoothListView.OnSmoothScrollListener() { @Override - public void onSmoothScrolling(View view) {} + public void onSmoothScrolling(View view) { + } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { @@ -247,15 +244,15 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun // 处理筛选是否吸附在顶部 if (filterViewTopMargin <= titleViewHeight || firstVisibleItem > filterViewPosition) { isStickyTop = true; // 吸附在顶部 - filterView.setVisibility(View.VISIBLE); + realFilterView.setVisibility(View.VISIBLE); } else { isStickyTop = false; // 没有吸附在顶部 - filterView.setVisibility(View.GONE); + realFilterView.setVisibility(View.GONE); } if (isSmooth && isStickyTop) { isSmooth = false; - filterView.show(filterPosition); + realFilterView.show(filterPosition); } // 处理标题栏颜色渐变 @@ -281,7 +278,7 @@ private void handleTitleBarColorEvaluate() { fraction = 1f - bannerViewTopMargin * 1f / 60; if (fraction < 0f) fraction = 0f; rlBar.setAlpha(fraction); - return ; + return; } float space = Math.abs(bannerViewTopMargin) * 1f; @@ -316,10 +313,10 @@ protected void onStop() { @Override public void onBackPressed() { - if (!filterView.isShowing()) { - super.onBackPressed(); + if (realFilterView.isShowing()) { + realFilterView.resetAllStatus(); } else { - filterView.resetAllStatus(); + super.onBackPressed(); } } diff --git a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderFilterView.java b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderFilterView.java index d0a7f3a..be181bd 100644 --- a/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderFilterView.java +++ b/app/src/main/java/com/sunfusheng/StickyHeaderListView/view/HeaderFilterView.java @@ -14,8 +14,9 @@ */ public class HeaderFilterView extends AbsHeaderView { - @BindView(R.id.fv_filter) - FilterView fvFilter; + + @BindView(R.id.fake_filterView) + FilterView fakeFilterView; public HeaderFilterView(Activity context) { super(context); @@ -29,7 +30,7 @@ protected void getView(Object obj, ListView listView) { } public FilterView getFilterView() { - return fvFilter; + return fakeFilterView; } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2f640a8..8a7dac8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -75,7 +75,7 @@ diff --git a/app/src/main/res/layout/header_filter_layout.xml b/app/src/main/res/layout/header_filter_layout.xml index 5def4e3..1833dd9 100644 --- a/app/src/main/res/layout/header_filter_layout.xml +++ b/app/src/main/res/layout/header_filter_layout.xml @@ -1,6 +1,6 @@