你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 仿IOS後果 帶彈簧動畫的ListView

仿IOS後果 帶彈簧動畫的ListView

編輯:IOS開發綜合

比來項目盤算做一個界面,相似於dayone首頁的界面後果,dayone 是一款付費運用,今朝只要IOS端。作為一個資深懶散的法式員,奉行的主旨是相對不反復造一個輪子。因而乎,去網上找一年夜堆開源項目,發明沒有找到適合的,然後,只能硬著頭皮本身來了。先看看後果:


後果圖

其實寫起來也比擬簡略,就是掌握ListView的頭部和底部的高度便可以了, 假如用RecycleView完成起來也是一樣,只是RecycleView添加頭和尾巴略微費事一點,處置點擊事宜也不是很便利,所以就基於ListView去完成了。完成的代碼, 我曾經上傳到github上了。

1、應用辦法

compile 'com.a520wcf.yllistview:YLListView:1.0.1

2、應用引見:
1)、結構:
結構留意一個小細節Android:layout_height 最好是match_parent, 不然ListView每次滑動的時刻都有能夠須要從新盤算條目高度,比擬消耗CPU;

 <com.a520wcf.yllistview.YLListView 
 Android:divider="@Android:color/transparent"
 android:id="@+id/listView" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" />

2)、代碼:

 private YLListView listView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (YLListView) findViewById(R.id.listView);
  // 不添加也有默許的頭和底
  View topView=View.inflate(this,R.layout.top,null);
  listView.addHeaderView(topView);
  View bottomView=new View(getApplicationContext());
  listView.addFooterView(bottomView);

  // 頂部和底部也能夠固定終究的高度 不固定就應用結構自己的高度
  listView.setFinalBottomHeight(100);
  listView.setFinalTopHeight(100);

  listView.setAdapter(new DemoAdapter());

  //YLListView默許有頭和底 處置點擊事宜地位留意減去
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    position=position-listView.getHeaderViewsCount();
   }
  });
 }


3、源碼引見
其實這個項目外面只要一個類,年夜家不須要依附,直接把這個類復制到項目中便可以了,來看看源碼:

package com.a520wcf.yllistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Scroller;

public class YLListView extends ListView implements AbsListView.OnScrollListener {
 private Scroller mScroller; // used for scroll back
 private float mLastY = -1;

 private int mScrollBack;
 private final static int SCROLLBACK_HEADER = 0;
 private final static int SCROLLBACK_FOOTER = 1;

 private final static int SCROLL_DURATION = 400; // scroll back duration
 private final static float OFFSET_RADIO = 1.8f;
 // total list items, used to detect is at the bottom of ListView.
 private int mTotalItemCount;
 private View mHeaderView; // 頂部圖片
 private View mFooterView; // 底部圖片
 private int finalTopHeight;
 private int finalBottomHeight;

 public YLListView(Context context) {
  super(context);
  initWithContext(context);
 }

 public YLListView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initWithContext(context);
 }

 public YLListView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  initWithContext(context);
 }

 private void initWithContext(Context context) {
  mScroller = new Scroller(context, new DecelerateInterpolator());
  super.setOnScrollListener(this);

  this.getViewTreeObserver().addOnGlobalLayoutListener(
    new OnGlobalLayoutListener() {
     @Override
     public void onGlobalLayout() {
      if(mHeaderView==null){
       View view=new View(getContext());
       addHeaderView(view);
      }
      if(mFooterView==null){
       View view=new View(getContext());
       addFooterView(view);
      }
      getViewTreeObserver()
        .removeGlobalOnLayoutListener(this);
     }
    });
 }

 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  if (mLastY == -1) {
   mLastY = ev.getRawY();
  }
  switch (ev.getAction()) {
   case MotionEvent.ACTION_DOWN:
    mLastY = ev.getRawY();
    break;
   case MotionEvent.ACTION_MOVE:
    final float deltaY = ev.getRawY() - mLastY;
    mLastY = ev.getRawY();
    if (getFirstVisiblePosition() == 0 && (mHeaderView.getHeight() > finalTopHeight || deltaY > 0)
      && mHeaderView.getTop() >= 0) {
     // the first item is shoWing, header has shown or pull down.
     updateHeaderHeight(deltaY / OFFSET_RADIO);
    } else if (getLastVisiblePosition() == mTotalItemCount - 1
      && (getFootHeight() >finalBottomHeight || deltaY < 0)) {
     updateFooterHeight(-deltaY / OFFSET_RADIO);
    }
    break;
   default:
    mLastY = -1; // reset
    if (getFirstVisiblePosition() == 0 && getHeaderHeight() > finalTopHeight) {
     resetHeaderHeight();
    }
    if (getLastVisiblePosition() == mTotalItemCount - 1 ){
      if(getFootHeight() > finalBottomHeight) {
       resetFooterHeight();
      }
    }
    break;
  }
  return super.onTouchEvent(ev);
 }

 /**
  * 重置底部高度
  */
 private void resetFooterHeight() {
  int bottomHeight = getFootHeight();
  if (bottomHeight > finalBottomHeight) {
   mScrollBack = SCROLLBACK_FOOTER;
   mScroller.startScroll(0, bottomHeight, 0, -bottomHeight+finalBottomHeight,
     SCROLL_DURATION);
   invalidate();
  }
 }
 // 盤算滑動 當invalidate()後 體系會主動挪用
 @Override
 public void computeScroll() {
  if (mScroller.computeScrollOffset()) {
   if (mScrollBack == SCROLLBACK_HEADER) {
    setHeaderHeight(mScroller.getCurrY());
   } else {
    setFooterViewHeight(mScroller.getCurrY());
   }
   postInvalidate();
  }
  super.computeScroll();
 }
 // 設置頂部高度
 private void setHeaderHeight(int height) {
  LayoutParams layoutParams = (LayoutParams) mHeaderView.getLayoutParams();
  layoutParams.height = height;
  mHeaderView.setLayoutParams(layoutParams);
 }
 // 設置底部高度
 private void setFooterViewHeight(int height) {
  LayoutParams layoutParams =
    (LayoutParams) mFooterView.getLayoutParams();
  layoutParams.height =height;
  mFooterView.setLayoutParams(layoutParams);
 }
 // 獲得頂部高度
 public int getHeaderHeight() {
  AbsListView.LayoutParams layoutParams =
    (AbsListView.LayoutParams) mHeaderView.getLayoutParams();
  return layoutParams.height;
 }
 // 獲得底部高度
 public int getFootHeight() {
  AbsListView.LayoutParams layoutParams =
    (AbsListView.LayoutParams) mFooterView.getLayoutParams();
  return layoutParams.height;
 }

 private void resetHeaderHeight() {
  int height = getHeaderHeight();
  if (height == 0) // not visible.
   return;
  mScrollBack = SCROLLBACK_HEADER;
  mScroller.startScroll(0, height, 0, finalTopHeight - height,
    SCROLL_DURATION);
  invalidate();
 }

 /**
  * 設置頂部高度 假如不設置高度,默許就是結構自己的高度
  * @param height 頂部高度
  */
 public void setFinalTopHeight(int height) {
  this.finalTopHeight = height;
 }
 /**
  * 設置底部高度 假如不設置高度,默許就是結構自己的高度
  * @param height 底部高度
  */
 public void setFinalBottomHeight(int height){
  this.finalBottomHeight=height;
 }
 @Override
 public void addHeaderView(View v) {
  mHeaderView = v;
  super.addHeaderView(mHeaderView);
  mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
    new OnGlobalLayoutListener() {
     @Override
     public void onGlobalLayout() {
      if(finalTopHeight==0) {
       finalTopHeight = mHeaderView.getMeasuredHeight();
      }
      setHeaderHeight(finalTopHeight);
      getViewTreeObserver()
        .removeGlobalOnLayoutListener(this);
     }
    });
 }

 @Override
 public void addFooterView(View v) {
  mFooterView = v;
  super.addFooterView(mFooterView);

  mFooterView.getViewTreeObserver().addOnGlobalLayoutListener(
    new OnGlobalLayoutListener() {
     @Override
     public void onGlobalLayout() {
      if(finalBottomHeight==0) {
       finalBottomHeight = mFooterView.getMeasuredHeight();
      }
      setFooterViewHeight(finalBottomHeight);
      getViewTreeObserver()
        .removeGlobalOnLayoutListener(this);
     }
    });
 }

 private OnScrollListener mScrollListener; // user's scroll listener

 @Override
 public void setOnScrollListener(OnScrollListener l) {
  mScrollListener = l;
 }

 @Override
 public void onScrollStateChanged(AbsListView view, int scrollState) {
  if (mScrollListener != null) {
   mScrollListener.onScrollStateChanged(view, scrollState);
  }
 }

 @Override
 public void onScroll(AbsListView view, int firstVisibleItem,
       int visibleItemCount, int totalItemCount) {
  // send to user's listener
  mTotalItemCount = totalItemCount;
  if (mScrollListener != null) {
   mScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
     totalItemCount);
  }
 }

 private void updateHeaderHeight(float delta) {
  setHeaderHeight((int) (getHeaderHeight()+delta));
  setSelection(0); // scroll to top each time
 }

 private void updateFooterHeight(float delta) {
  setFooterViewHeight((int) (getFootHeight()+delta));

 }
}

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

【仿IOS後果 帶彈簧動畫的ListView】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved