| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

android一个向上滑动显示菜单,向下滑动隐藏菜单实现

浏览器qq猎豹等,很多都是,向上滑动显示搜索菜单,向下浏览网页隐藏搜索菜单,而且非常平滑的效果,用Visistty.setVisisty(View....)一直达到我想要的效果。用改变view的位置,平移到屏幕显示外面。并且带有动画效果来实现。貌似会更好点。直接看代码。一部分代码仿另一个开源项目基础上修改而来的。

 
主要原理是检查判定是否在滑动到屏幕的顶部或者底部,根据其顶部的话就,让我们的内容view 偏移顶部高度为我们topview高度,否则view偏移量为0。并为其设置平移动画效果。
  public class ScrollViewActivity extends Activity {      private PoppyViewHelper mPoppyViewHelper;      private LayoutInflater layoutInflater;      private NotifyingScrollView scrollView;      private View poppyview;      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_scrollview);          layoutInflater=LayoutInflater.from(this);          scrollView=(NotifyingScrollView)findViewById(R.id.scrollView);          poppyview=layoutInflater.inflate(R.layout.poppyview, null);                              mPoppyViewHelper = new PoppyViewHelper(this, PoppyViewPosition.TOP);          ViewGroup poppyView = mPoppyViewHelper.createPoppyViewOnScrollView(scrollView, poppyview);                    poppyView.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  Toast.makeText(ScrollViewActivity.this, "Click me!", Toast.LENGTH_SHORT).show();              }          });      }  }        public class PoppyViewHelper {      public enum PoppyViewPosition {          TOP, BOTTOM      };      private static final int SCROLL_TO_TOP = - 1;      private static final int SCROLL_TO_BOTTOM = 1;      private static final int SCROLL_DIRECTION_CHANGE_THRESHOLD = 5;      private Activity mActivity;      private LayoutInflater mLayoutInflater;      private View mPoppyView;      private int mScrollDirection = 0;      private int mPoppyViewHeight = - 1;            private PoppyViewPosition mPoppyViewPosition;      public PoppyViewHelper(Activity activity, PoppyViewPosition position) {          mActivity = activity;          mLayoutInflater = LayoutInflater.from(activity);          mPoppyViewPosition = position;      }      public PoppyViewHelper(Activity activity) {          this(activity, PoppyViewPosition.BOTTOM);      }            private ViewGroup viewGroup;      // for scrollview      private  NotifyingScrollView scrollView;      public ViewGroup createPoppyViewOnScrollView(NotifyingScrollView scrollViewId, View poppyViewResId) {           mPoppyView =poppyViewResId;           scrollView = scrollViewId;          initPoppyViewOnScrollView();          return viewGroup;      }      // for ListView      public View createPoppyViewOnListView(int listViewId, int poppyViewResId, OnScrollListener onScrollListener) {          final ListView listView = (ListView)mActivity.findViewById(listViewId);          if(listView.getHeaderViewsCount() != 0) {              throw new IllegalArgumentException("use createPoppyViewOnListView with headerResId parameter");          }          if(listView.getFooterViewsCount() != 0) {              throw new IllegalArgumentException("poppyview library doesn't support listview with footer");          }          mPoppyView = mLayoutInflater.inflate(poppyViewResId, null);          initPoppyViewOnListView(listView, onScrollListener);          return mPoppyView;      }      public View createPoppyViewOnListView(int listViewId, int poppyViewResId) {          return createPoppyViewOnListView(listViewId, poppyViewResId, null);      }      // common      private void setPoppyViewOnView(View view) {          LayoutParams lp = view.getLayoutParams();          ViewParent parent = view.getParent();          ViewGroup group = (ViewGroup)parent;          int index = group.indexOfChild(view);          final FrameLayout newContainer = new FrameLayout(mActivity);          group.removeView(view);          group.addView(newContainer, index, lp);          newContainer.addView(view);          final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);          layoutParams.gravity = mPoppyViewPosition == PoppyViewPosition.BOTTOM ? Gravity.BOTTOM : Gravity.TOP;          newContainer.addView(mPoppyView, layoutParams);          group.invalidate();          viewGroup=group;      }      private boolean isjixu=false;      private void onScrollPositionChanged(int oldScrollPosition, int newScrollPosition) {          int newScrollDirection;          System.out.println(oldScrollPosition + " ->" + newScrollPosition);          if(newScrollPosition < oldScrollPosition) {              newScrollDirection = SCROLL_TO_TOP;              isjixu=true;              //gundong滚动dao到dingb顶部          } else {              newScrollDirection = SCROLL_TO_BOTTOM;              isjixu=false;          }          if(newScrollDirection != mScrollDirection) {              mScrollDirection = newScrollDirection;              translateYPoppyView();          }      }      private     int translationY = 0;      private boolean isdingbu=false;      private void translateYPoppyView() {                mPoppyView.post(new Runnable() {              @Override              public void run() {                  if(mPoppyViewHeight <= 0) {                      mPoppyViewHeight = mPoppyView.getHeight();                  }                                                      switch (mPoppyViewPosition) {                  case BOTTOM:                      translationY = mScrollDirection == SCROLL_TO_TOP ? 0 : mPoppyViewHeight;                                            break;                  case TOP:                      translationY = mScrollDirection == SCROLL_TO_TOP ?  0:  -mPoppyViewHeight;                      isdingbu=true;                      break;                  }                  ViewPropertyAnimator.animate(mPoppyView).setDuration(300).translationY(translationY);              }          });          if(isdingbu){              scrollView.post(new Runnable() {                  @Override                  public void run() {                      如果为顶部显示就让view平移Mpoppyviewhiegh高度                      ViewPropertyAnimator.animate(scrollView).setDuration(300).translationY(mPoppyViewHeight);                      isdingbu=false;                  }              });                        }else{              scrollView.post(new Runnable() {                  @Override                  public void run() {                      不然就让他显示在屏幕顶部                      ViewPropertyAnimator.animate(scrollView).setDuration(300).translationY(0);                  }              });                        }                }      // for ScrollView      private void initPoppyViewOnScrollView() {          setPoppyViewOnView(scrollView);          scrollView.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {              int mScrollPosition;              public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {                  if(Math.abs(t - mScrollPosition) >= SCROLL_DIRECTION_CHANGE_THRESHOLD) {                      onScrollPositionChanged(mScrollPosition, t);                  }                                    mScrollPosition = t;              }          });      }      // for ListView      private void initPoppyViewOnListView(ListView listView, final OnScrollListener onScrollListener) {          setPoppyViewOnView(listView);          listView.setOnScrollListener(new OnScrollListener() {              int mScrollPosition;              @Override              public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {                  if(onScrollListener != null) {                      onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);                  }                  View topChild = view.getChildAt(0);                  int newScrollPosition = 0;                  if(topChild == null) {                      newScrollPosition = 0;                  } else {                      newScrollPosition = - topChild.getTop() + view.getFirstVisiblePosition() * topChild.getHeight();                  }                  if(Math.abs(newScrollPosition - mScrollPosition) >= SCROLL_DIRECTION_CHANGE_THRESHOLD) {                      onScrollPositionChanged(mScrollPosition, newScrollPosition);                  }                  mScrollPosition = newScrollPosition;              }              @Override              public void onScrollStateChanged(AbsListView view, int scrollState) {                  if(onScrollListener != null) {                      onScrollListener.onScrollStateChanged(view, scrollState);                  }              }          });      }  }