Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Mar 6, 2017
1 parent 6f19a50 commit 7ae2fbc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 91 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
android:theme="@style/AppTheme">

<activity
android:name=".ui.MainActivity"
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand All @@ -30,7 +30,7 @@
</activity>

<activity
android:name=".ui.AboutActivity"
android:name=".AboutActivity"
android:screenOrientation="portrait"/>
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sunfusheng.StickyHeaderListView.ui;
package com.sunfusheng.StickyHeaderListView;

import android.content.Context;
import android.content.pm.PackageInfo;
Expand All @@ -17,8 +17,6 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sunfusheng.StickyHeaderListView.R;

import butterknife.BindView;
import butterknife.ButterKnife;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sunfusheng.StickyHeaderListView.ui;
package com.sunfusheng.StickyHeaderListView;

import android.app.Activity;
import android.content.Context;
Expand All @@ -12,7 +12,6 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunfusheng.StickyHeaderListView.R;
import com.sunfusheng.StickyHeaderListView.adapter.TravelingAdapter;
import com.sunfusheng.StickyHeaderListView.model.ChannelEntity;
import com.sunfusheng.StickyHeaderListView.model.FilterData;
Expand Down Expand Up @@ -165,7 +164,7 @@ public void onClick(View v) {
});

// (假的ListView头部展示的)筛选视图点击
headerFilterView.setOnFilterClickListener(new HeaderFilterView.OnFilterClickListener() {
headerFilterView.getFilterView().setOnFilterClickListener(new FilterView.OnFilterClickListener() {
@Override
public void onFilterClick(int position) {
filterPosition = position;
Expand Down Expand Up @@ -230,7 +229,7 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun

// 获取广告头部View、自身的高度、距离顶部的高度
if (itemHeaderBannerView == null) {
itemHeaderBannerView = smoothListView.getChildAt(1-firstVisibleItem);
itemHeaderBannerView = smoothListView.getChildAt(1);
}
if (itemHeaderBannerView != null) {
bannerViewTopMargin = DensityUtil.px2dip(mContext, itemHeaderBannerView.getTop());
Expand Down Expand Up @@ -268,11 +267,9 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun
// 填充数据
private void fillAdapter(List<TravelingEntity> list) {
if (list == null || list.size() == 0) {
smoothListView.setLoadMoreEnable(false);
int height = mScreenHeight - DensityUtil.dip2px(mContext, 95); // 95 = 标题栏高度 + FilterView的高度
mAdapter.setData(ModelUtil.getNoDataEntity(height));
} else {
smoothListView.setLoadMoreEnable(list.size() > TravelingAdapter.ONE_REQUEST_COUNT);
mAdapter.setData(list);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void onClick(View v) {
}

// 复位筛选的显示状态
public void resetFilterStatus() {
public void resetViewStatus() {
tvCategoryTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow);

Expand All @@ -162,7 +162,7 @@ public void resetFilterStatus() {

// 复位所有的状态
public void resetAllStatus() {
resetFilterStatus();
resetViewStatus();
hide();
}

Expand Down Expand Up @@ -253,8 +253,23 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)

// 动画显示
public void show(int position) {
if (isShowing && lastFilterPosition == position) return;
resetFilterStatus();
if (isShowing && lastFilterPosition == position) {
hide();
return;
} else if (!isShowing) {
viewMaskBg.setVisibility(VISIBLE);
llContentListView.setVisibility(VISIBLE);
llContentListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
llContentListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
panelHeight = llContentListView.getHeight();
ObjectAnimator.ofFloat(llContentListView, "translationY", -panelHeight, 0).setDuration(200).start();
}
});
}
isShowing = true;
resetViewStatus();
rotateArrowUp(position);
rotateArrowDown(lastFilterPosition);
lastFilterPosition = position;
Expand All @@ -276,25 +291,12 @@ public void show(int position) {
setFilterAdapter();
break;
}

if (isShowing) return ;
isShowing = true;
viewMaskBg.setVisibility(VISIBLE);
llContentListView.setVisibility(VISIBLE);
llContentListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
llContentListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
panelHeight = llContentListView.getHeight();
ObjectAnimator.ofFloat(llContentListView, "translationY", -panelHeight, 0).setDuration(200).start();
}
});
}

// 隐藏动画
public void hide() {
isShowing = false;
resetFilterStatus();
resetViewStatus();
rotateArrowDown(filterPosition);
rotateArrowDown(lastFilterPosition);
filterPosition = -1;
Expand All @@ -303,6 +305,21 @@ public void hide() {
ObjectAnimator.ofFloat(llContentListView, "translationY", 0, -panelHeight).setDuration(200).start();
}

// 设置筛选数据
public void setFilterData(Activity activity, FilterData filterData) {
this.mActivity = activity;
this.filterData = filterData;
}

// 是否显示
public boolean isShowing() {
return isShowing;
}

public int getFilterPosition() {
return filterPosition;
}

// 旋转箭头向上
private void rotateArrowUp(int position) {
switch (position) {
Expand Down Expand Up @@ -339,18 +356,6 @@ public static void rotateArrowUpAnimation(final ImageView iv) {
RotateAnimation animation = new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(200);
animation.setFillAfter(true);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {}

@Override
public void onAnimationEnd(Animation animation) {
}
});
iv.startAnimation(animation);
}

Expand All @@ -360,36 +365,9 @@ public static void rotateArrowDownAnimation(final ImageView iv) {
RotateAnimation animation = new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(200);
animation.setFillAfter(true);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {}

@Override
public void onAnimationEnd(Animation animation) {
}
});
iv.startAnimation(animation);
}

// 设置筛选数据
public void setFilterData(Activity activity, FilterData filterData) {
this.mActivity = activity;
this.filterData = filterData;
}

// 是否显示
public boolean isShowing() {
return isShowing;
}

public int getFilterPosition() {
return filterPosition;
}

// 筛选视图点击
private OnFilterClickListener onFilterClickListener;
public void setOnFilterClickListener(OnFilterClickListener onFilterClickListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Created by sunfusheng on 16/4/20.
*/
public class HeaderFilterView extends AbsHeaderView<Object> implements FilterView.OnFilterClickListener {
public class HeaderFilterView extends AbsHeaderView<Object> {

@BindView(R.id.fv_filter)
FilterView fvFilter;
Expand All @@ -25,33 +25,11 @@ public HeaderFilterView(Activity context) {
protected void getView(Object obj, ListView listView) {
View view = mInflate.inflate(R.layout.header_filter_layout, listView, false);
ButterKnife.bind(this, view);

dealWithTheView(obj);
listView.addHeaderView(view);
}

// 获得筛选View
public FilterView getFilterView() {
return fvFilter;
}

private void dealWithTheView(Object obj) {
fvFilter.setOnFilterClickListener(this);
}

@Override
public void onFilterClick(int position) {
if (onFilterClickListener != null) {
onFilterClickListener.onFilterClick(position);
}
}

private OnFilterClickListener onFilterClickListener;
public void setOnFilterClickListener(OnFilterClickListener onFilterClickListener) {
this.onFilterClickListener = onFilterClickListener;
}
public interface OnFilterClickListener {
void onFilterClick(int position);
}

}

0 comments on commit 7ae2fbc

Please sign in to comment.