Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Mar 12, 2017
1 parent 7ae2fbc commit 70ec321
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@

/**
* 作者:孙福生
*
* <p>
* 个人博客: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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -174,37 +174,33 @@ 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));
}
});

// 排序Item点击
filterView.setOnItemSortClickListener(new FilterView.OnItemSortClickListener() {
realFilterView.setOnItemSortClickListener(new FilterView.OnItemSortClickListener() {
@Override
public void onItemSortClick(FilterEntity entity) {
fillAdapter(ModelUtil.getSortTravelingData(entity));
}
});

// 筛选Item点击
filterView.setOnItemFilterClickListener(new FilterView.OnItemFilterClickListener() {
realFilterView.setOnItemFilterClickListener(new FilterView.OnItemFilterClickListener() {
@Override
public void onItemFilterClick(FilterEntity entity) {
fillAdapter(ModelUtil.getFilterTravelingData(entity));
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

// 处理标题栏颜色渐变
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*/
public class HeaderFilterView extends AbsHeaderView<Object> {

@BindView(R.id.fv_filter)
FilterView fvFilter;

@BindView(R.id.fake_filterView)
FilterView fakeFilterView;

public HeaderFilterView(Activity context) {
super(context);
Expand All @@ -29,7 +30,7 @@ protected void getView(Object obj, ListView listView) {
}

public FilterView getFilterView() {
return fvFilter;
return fakeFilterView;
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</RelativeLayout>

<com.sunfusheng.StickyHeaderListView.view.FilterView
android:id="@+id/filterView"
android:id="@+id/real_filterView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/header_filter_layout.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<com.sunfusheng.StickyHeaderListView.view.FilterView
android:id="@+id/fv_filter"
android:id="@+id/fake_filterView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 70ec321

Please sign in to comment.