程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成毫秒轉換成時分秒的辦法

C#完成毫秒轉換成時分秒的辦法

編輯:C#入門知識

C#完成毫秒轉換成時分秒的辦法。本站提示廣大學習愛好者:(C#完成毫秒轉換成時分秒的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成毫秒轉換成時分秒的辦法正文


RecyclerView曾經出來良久了,許很多多的項目都開端從ListView轉戰RecyclerView,那末,上拉加載和下拉刷新是一件很有需要的工作。

在ListView上,我們可以經由過程本身添加addHeadView和addFootView去添加頭結構和底部局完成自界說的上拉和下拉,或許應用一些第三方庫來簡略的集成,例如Android-pulltorefresh或許android-Ultra-Pull-to-Refresh,後者的自界說更強,但須要本身完成上拉加載。

而鄙人面我們將用兩種方法來完成上拉加載和下拉刷新

第一種方法:SwipeRefreshLayout+滑動底部主動加載

第二種方法:應用第三方庫SwipeToLoadLayout完成上拉加載和下拉刷新。

第一種方法:SwipeRefreshLayout+滑動底部主動加載

SwipeRefreshLayout完成很簡略,重點是滑動究竟部主動加載應當若何完成,其實其完成的方法相似於ListView的完成方法。

看一下activity_recycle_swiperefresh.xml文件:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/swipe_refresh"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView
  android:id="@+id/swipe_target"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:scrollbars="none" />

</android.support.v4.widget.SwipeRefreshLayout>

結構文件就兩個控件,SwipeRefreshLayout中嵌套RecyclerView。

在代碼中初始化RecyclerView和完成adapter等,這不是重點,不再貼代碼。

在RecyclerView中無方法addOnScrollListener,該辦法相似於ListView的setOnScrollListener辦法,OnScrollListener中有兩個辦法的回調

*onScrolled(RecyclerView recyclerView, int dx, int dy) :轉動的回調,dx和dy表現手指滑動程度和垂直的偏移量。

*onScrollStateChanged(RecyclerView recyclerView, int newState):滑動狀況的回調。

那末,我們的側重點就在這個兩個辦法上了。

關於向上加載更多,我們須要有以下斷定

--能否是向上滑動

--能否滑動究竟部

--以後能否正在加載數據

--以後狀況能否是滑動停滯的狀況

完成比擬龐雜,界說一個類LoadDataScrollController,繼續類RecyclerView.OnScrollListener,

由於onScrollStateChanged其實狀況轉變時的回調,沒法不時的獲得顯示的條目和地位,所以我們在onScrolled中獲得響應地位,

 @Override
 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

  /**
   * 獲得結構參數
   */
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

  //假如為null,第一次運轉,肯定結構類型
  if (mLayoutManagerType == null) {
   if (layoutManager instanceof LinearLayoutManager) {
    mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT;
   } else if (layoutManager instanceof GridLayoutManager) {
    mLayoutManagerType = LayoutManagerType.GRID_LAYOUT;
   } else if (layoutManager instanceof StaggeredGridLayoutManager) {
    mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT;
   } else {
    throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager");
   }
  }

  //關於不太可以或許的結構參數,分歧的辦法獲得到以後顯示的最初一個條目數
  switch (mLayoutManagerType) {
   case LINEAR_LAYOUT:
    mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
    break;
   case GRID_LAYOUT:
    mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
    break;
   case STAGGERED_GRID_LAYOUT:
    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
    if (mLastPostions == null) {
     mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()];
    }
    staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions);
    mLastVisibleItemPosition = findMax(mLastPostions);
    break;
   default:
    break;
  }

 }

起首獲得結構治理器,並斷定是那品種型的,由於有三品種型,界說列舉來保留結構類型的參數

/**
 *
 * RecycleView的結構治理器的類型
 * Created by Alex_MaHao on 2016/5/10.
 */
public enum LayoutManagerType {
 LINEAR_LAYOUT,
 GRID_LAYOUT,
 STAGGERED_GRID_LAYOUT
}

然後依據結構通例其的類型獲得其以後顯示的最年夜條目,關於瀑布流來講,他假如是垂直的兩列瀑布的話,我們須要獲得兩列平分別最年夜條目數,停止比擬,選出最年夜條目數。

 /**
  * 當是瀑布流時,獲得到的是每個瀑布最下方顯示的條目,經由過程條目停止比較
  */
 private int findMax(int[] lastPositions) {
  int max = lastPositions[0];
  for (int value : lastPositions) {
   if (value > max) {
    max = value;
   }
  }
  return max;
 }

拿到以後最年夜的條目數以後,在onScrollStateChange中停止斷定狀況等,

 @Override
 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

  //RecycleView 顯示的條目數
  int visibleCount = layoutManager.getChildCount();

  //顯示數據總數
  int totalCount = layoutManager.getItemCount();


  // 四個前提,分離是能否稀有據,狀況能否是滑動停滯狀況,顯示的最年夜條目能否年夜於全部數據(留意偏移量),能否正在加載數據
  if(visibleCount>0
    &&newState==RecyclerView.SCROLL_STATE_IDLE
    &&mLastVisibleItemPosition>=totalCount-1
    &&!isLoadData){
   //可以加載數據
   isLoadData = true;
  }

 }

正文很清晰,在加載數據的處所,我們將isLoadData設為true,同時應用接口回調加載數據,等數據加載完成,經由過程setLoadDataStatus辦法設置為false

 public void setLoadDataStatus(boolean isLoadData){
  this.isLoadData = isLoadData;
 }

假如如許就停止了,感到很費事,關於刷新和加載更多,我們須要在挪用的處所分離設置監聽,那末我們可讓LoadDataScrollController完成SwipeRefreshLayout的刷新監聽辦法,在應用我們界說的同一的上拉刷新和加載數據接口停止處置

/**
 * 完成上拉加載的監聽:加載前提:滑動到最初,且是停滯狀況,則開端加載數據
 * Created by Alex_MaHao on 2016/5/10.
 */
public class LoadDataScrollController extends RecyclerView.OnScrollListener implements SwipeRefreshLayout.OnRefreshListener {

 /**
  * 以後結構治理器的類型
  */
 private LayoutManagerType mLayoutManagerType;


 /**
  * 以後RecycleView顯示的最年夜條目
  */
 private int mLastVisibleItemPosition;


 /**
  * 每列的最初一個條目
  */
 private int[] mLastPostions;


 /**
  * 能否正在加載數據 包含刷新和向上加載更多
  */
 private boolean isLoadData = false;


 /**
  * 回調接口
  */
 private OnRecycleRefreshListener mListener;


 public LoadDataScrollController(OnRecycleRefreshListener onRecycleRefreshListener) {
  this.mListener = onRecycleRefreshListener;
 }

 @Override
 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

  /**
   * 獲得結構參數
   */
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

  //假如為null,第一次運轉,肯定結構類型
  if (mLayoutManagerType == null) {
   if (layoutManager instanceof LinearLayoutManager) {
    mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT;
   } else if (layoutManager instanceof GridLayoutManager) {
    mLayoutManagerType = LayoutManagerType.GRID_LAYOUT;
   } else if (layoutManager instanceof StaggeredGridLayoutManager) {
    mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT;
   } else {
    throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager");
   }
  }

  //關於不太可以或許的結構參數,分歧的辦法獲得到以後顯示的最初一個條目數
  switch (mLayoutManagerType) {
   case LINEAR_LAYOUT:
    mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
    break;
   case GRID_LAYOUT:
    mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
    break;
   case STAGGERED_GRID_LAYOUT:
    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
    if (mLastPostions == null) {
     mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()];
    }
    staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions);
    mLastVisibleItemPosition = findMax(mLastPostions);
    break;
   default:
    break;
  }

 }

 @Override
 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

  //RecycleView 顯示的條目數
  int visibleCount = layoutManager.getChildCount();

  //顯示數據總數
  int totalCount = layoutManager.getItemCount();


  // 四個前提,分離是能否稀有據,狀況能否是滑動停滯狀況,顯示的最年夜條目能否年夜於全部數據(留意偏移量),能否正在加載數據
  if(visibleCount>0
    &&newState==RecyclerView.SCROLL_STATE_IDLE
    &&mLastVisibleItemPosition>=totalCount-1
    &&!isLoadData){
   //可以加載數據
   if(mListener!=null){
    isLoadData = true;
    mListener.loadMore();
   }
  }

 }


 /**
  * 當是瀑布流時,獲得到的是每個瀑布最下方顯示的條目,經由過程條目停止比較
  */
 private int findMax(int[] lastPositions) {
  int max = lastPositions[0];
  for (int value : lastPositions) {
   if (value > max) {
    max = value;
   }
  }
  return max;
 }


 public void setLoadDataStatus(boolean isLoadData){
  this.isLoadData = isLoadData;
 }

 @Override
 public void onRefresh() {
  //刷新數據的辦法
  if(mListener!=null){
   isLoadData = true;
   mListener.refresh();
  }

 }

 /**
  * 數據加載接口回調
  */
 interface OnRecycleRefreshListener{
  void refresh();
  void loadMore();
 }
}

最初看一下main的代碼

/**
 * 應用原生的SwipeRefreshLayout和代碼斷定
 *  完成RecyclewView 的刷新和加載更多
 *
 * Created by Alex_MaHao on 2016/5/10.
 */
public class SwipeRefreshActivity extends AppCompatActivity implements LoadDataScrollController.OnRecycleRefreshListener {


 private SwipeRefreshLayout mSwipeRefresh;

 private RecyclerView mRecycle;

 private HomeAdapter mAdapter;

 private LoadDataScrollController mController;


 private ProgressDialog pd;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_recycle_swiperefresh);

  mRecycle = ((RecyclerView) findViewById(R.id.swipe_target));

  mSwipeRefresh = ((SwipeRefreshLayout) findViewById(R.id.swipe_refresh));

  mSwipeRefresh.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);

  /**
   * 創立掌握器,同時使以後activity完成數據監聽回調接口
   */
  mController = new LoadDataScrollController(this);



  mAdapter = new HomeAdapter();

  //設置垂直的線性結構治理器,Orientation --> VERTICAL:垂直 HORIZONTAL:程度
  LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

  //StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

  //添加朋分線
  mRecycle.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));


  mRecycle.setLayoutManager(layoutManager);

  mRecycle.setItemAnimator(new DefaultItemAnimator());

  mRecycle.setAdapter(mAdapter);

  mAdapter.refresh();

  /**
   * 設置監聽
   */
  mRecycle.addOnScrollListener(mController);

  mSwipeRefresh.setOnRefreshListener(mController);

 }

 @Override
 public void refresh() {
  //刷新的接口調
  mSwipeRefresh.postDelayed(new Runnable() {
   @Override
   public void run() {
    mAdapter.refresh();
    mSwipeRefresh.setRefreshing(false);
    mController.setLoadDataStatus(false);
   }
  },2000);
 }

 @Override
 public void loadMore() {
  //加載更多的接口回調
  pd = new ProgressDialog(this);
  pd.show();
  mSwipeRefresh.postDelayed(new Runnable() {
   @Override
   public void run() {
    mAdapter.add();
    //設置數據加載停止的監聽狀況
    mController.setLoadDataStatus(false);
    pd.dismiss();
   }
  },2000);
 }
}

貼個後果圖

第二種方法:SwipeToLoadLayout完成上拉加載和下拉刷新

該刷新控件的方法相似於Ultra-pull-to-refresh的應用方法。

以下方法添加該庫:

 repositories {
  maven { url "https://jitpack.io" }
   }

 compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.3'

起首我們須要自界說一個頭視圖和底部視圖,頭部試圖和底部試圖的用法雷同,所以我們先界說一個頭部視圖類:

/**
 * 基本的refreshHeadView
 */
public class RefreshHeaderView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {

 public RefreshHeaderView(Context context) {
  super(context);
 }

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

 public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, defStyleAttr, defStyleRes);
 }

 @Override
 public void onRefresh() {
  //下拉到必定地位松開以後,挪用此辦法
  setText("refresh");

  Log.i("info","onRefresh");
 }

 @Override
 public void onPrepare() {

  //下拉之前挪用此辦法
  Log.i("info","onPrepare");
 }

 @Override
 public void onMove(int yScrolled, boolean isComplete, boolean automatic) {
  if (!isComplete) {
   //以後Y軸偏移量年夜於控件高度時,標識下拉到界線,顯示“松開已刷新”
   if (yScrolled >= getHeight()) {

   } else {
    //未到達偏移量

   }
  } 
  Log.i("info","onMove");
 }

 @Override
 public void onRelease() {
  //到達必定滑動間隔,松開刷新時挪用
  setText("onRelease");
  Log.i("info","onRelease");
 }

 @Override
 public void onComplete() {
  //加載完成以後挪用此辦法
  setText("complete");
  Log.i("info","onComplete");
 }

 @Override
 public void onReset() {
  //重置
  setText("onReset");
  Log.i("info","onReset");
 }
}

其須要完成接口SwipeRefreshTrigger和SwipeTrigger。

而底部須要完成SwipeTrigger和SwipeLoadMoreTrigger。

結構文件中以下應用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ECEDF0"
 >

 <com.aspsine.swipetoloadlayout.SwipeToLoadLayout
  android:id="@+id/swipeToLoadLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.RefreshHeaderView
   android:id="@+id/swipe_refresh_header"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />

  <android.support.v7.widget.RecyclerView
   android:id="@+id/swipe_target"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:scrollbars="vertical" />

  <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.LoaderMoreView
   android:id="@+id/swipe_load_more_footer"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:padding="20dp" />
 </com.aspsine.swipetoloadlayout.SwipeToLoadLayout>


</RelativeLayout>

查找控件,設置監聽

        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);

在我們之前的代碼中,參加了log信息,我們可以看一下log信息。…代表屢次onMove()辦法屢次挪用。

05-10 10:30:34.396 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onPrepare
05-10 10:30:34.536 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
..........................................................................
05-10 10:30:34.886 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
05-10 10:30:34.896 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onRelease
05-10 10:30:34.906 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
..........................................................................
05-10 10:30:35.086 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
05-10 10:30:35.106 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onRefresh
05-10 10:30:37.116 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onComplete
05-10 10:30:37.416 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
..........................................................................
05-10 10:30:37.516 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onMove
05-10 10:30:37.916 23814-23814/com.mahao.alex.systemwidgetdemo I/info: onReset

起首會挪用onPrepare()辦法,onMove()辦法會一向挪用,只需視圖有偏移,就會挪用。下拉到必定間隔以後,松開挪用onRelaease(),回歸到刷新地位時回調onRefresh(),加載完成挪用onComplete(),視圖開端減少,最初隱蔽以後挪用onReset()

依據需求自界說視圖,

界說我們的橢圓,應用自界說控件

/**
 * CircleView 圓盤控件,可以扭轉
 * Created by Alex_MaHao on 2016/5/10.
 */
public class CircleView extends View {

 /**
  * 控件的半徑
  */
 private int mRadius;

 /**
  * 繪制弧形的畫筆
  */
 private Paint mArcPaint;

 /**
  * 繪制弧形的區域
  */
 private RectF mRange;


 private int[] colors = {Color.RED, Color.BLUE, Color.YELLOW, Color.GREEN};

 public CircleView(Context context) {
  this(context, null, 0);
 }

 public CircleView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);

  init();
 }

 private void init() {
  mArcPaint = new Paint();
  mArcPaint.setAntiAlias(true);
  mArcPaint.setDither(true);
  mArcPaint.setStyle(Paint.Style.FILL);
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  int width = 0;
  int height = 0;

  int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);

  int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  int heightMode = MeasureSpec.getMode(heightMeasureSpec);


  if (widthMode == MeasureSpec.EXACTLY) {
   width = widthSize;
  } else {
   width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
  }

  if (heightMode == MeasureSpec.EXACTLY) {
   height = heightSize;
  } else {
   height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
  }

  //獲得半徑
  mRadius = Math.min(width, height) / 2;
  /**
   * 設置寬高為固定值
   */
  setMeasuredDimension(mRadius * 2, mRadius * 2);

   mRange = new RectF(0, 0, mRadius * 2, mRadius * 2);
 }


 @Override
 protected void onDraw(Canvas canvas) {

  float degree = 360/colors.length/2f;

  for (int i = 0; i < 8; i++) {
   mArcPaint.setColor(colors[i%4]);
   canvas.drawArc(mRange,-90f+degree*i,degree,true,mArcPaint);
  }

 }
}

繪制頭部刷新試圖

**
 * 自界說的下拉刷新控件 頭部
 * Created by Alex_MaHao on 2016/5/10.
 */
public class CircleRefreshHeaderView extends RelativeLayout implements SwipeTrigger, SwipeRefreshTrigger {

 CircleView mCircleView;

 TextView mDescText;

 private ObjectAnimator anim;

 private boolean isRelease;

 public CircleRefreshHeaderView(Context context) {
  this(context, null, 0);
 }

 public CircleRefreshHeaderView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public CircleRefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);

  initView();
 }

 /**
  * 初始化結構
  */
 private void initView() {

  int circlewidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());

  mCircleView = new CircleView(getContext());

  LinearLayout.LayoutParams circleParams = new LinearLayout.LayoutParams(circlewidth,circlewidth);

  mCircleView.setLayoutParams(circleParams);

  mDescText = new TextView(getContext());

  LinearLayout.LayoutParams descParams = new LinearLayout.LayoutParams(circlewidth*3, ViewGroup.LayoutParams.WRAP_CONTENT);

  descParams.gravity = Gravity.CENTER;
  descParams.setMargins(circlewidth/2,0,0,0);
  mDescText.setLayoutParams(descParams);
  mDescText.setTextSize(12);
  mDescText.setTextColor(Color.GRAY);
  mDescText.setText("下拉刷新");

  //添加線性的父結構
  LinearLayout ll = new LinearLayout(getContext());
  RelativeLayout.LayoutParams llParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  llParams.addRule(CENTER_IN_PARENT);
  ll.setLayoutParams(llParams);
  ll.setPadding(10,10,10,10);

  ll.addView(mCircleView);
  ll.addView(mDescText);

  addView(ll);
 }

 @Override
 public void onRefresh() {

  //開端刷新,啟動動畫
  anim = ObjectAnimator.ofFloat(mCircleView, "rotation", mCircleView.getRotation(), mCircleView.getRotation()+360f)
    .setDuration(500);
  anim.setRepeatCount(ValueAnimator.INFINITE);
  anim.setRepeatMode(ValueAnimator.RESTART);
  anim.start();

  mDescText.setText("正在加載數據");
 }

 @Override
 public void onPrepare() {
  isRelease = false;
 }

 @Override
 public void onMove(int yScroll, boolean isComplete, boolean b1) {
  if (!isComplete) {
   if (yScroll < getHeight()) {
    mDescText.setText("下拉刷新");
   } else {
    mDescText.setText("松開刷新更多");
   }

   //假如是仍鄙人拉狀況,則圓環追隨滑動停止轉動
   if (!isRelease)
    mCircleView.setRotation(((float) yScroll) / getHeight() * 360f);
  }


 }

 @Override
 public void onRelease() {
  isRelease = true;
 }

 @Override
 public void onComplete() {
  anim.cancel();
  mDescText.setText("加載完成");
 }

 @Override
 public void onReset() {
  //重置時,將動畫置為初始狀況
  mCircleView.setRotation(0f);
 }
}

結構文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ECEDF0"
 >

 <com.aspsine.swipetoloadlayout.SwipeToLoadLayout
  android:id="@+id/swipeToLoadLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.CircleRefreshHeaderView
   android:id="@+id/swipe_refresh_header"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />

  <android.support.v7.widget.RecyclerView
   android:id="@+id/swipe_target"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:scrollbars="vertical" />

  <com.mahao.alex.systemwidgetdemo.recycleView.swipetoloadlayout.LoaderMoreView
   android:id="@+id/swipe_load_more_footer"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:padding="20dp" />
 </com.aspsine.swipetoloadlayout.SwipeToLoadLayout>


</RelativeLayout>

public class SwipeToLayoutActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {



 private RecyclerView mRecycleView;

 SwipeToLoadLayout swipeToLoadLayout;

 private HomeAdapter adapter;


 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_recycle_swipetolayout);


  swipeToLoadLayout = ((SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout));


  mRecycleView = ((RecyclerView) findViewById(R.id.swipe_target));


  adapter = new HomeAdapter();

  //設置垂直的線性結構治理器,Orientation --> VERTICAL:垂直 HORIZONTAL:程度
  LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

//  StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

  //添加朋分線
  mRecycleView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL_LIST));

  mRecycleView.setLayoutManager(layoutManager);

  mRecycleView.setItemAnimator(new DefaultItemAnimator());

  mRecycleView.setAdapter(adapter);

  adapter.refresh();

  /**
   * 設置下拉刷新和上拉加載監聽
   */
  swipeToLoadLayout.setOnRefreshListener(this);
  swipeToLoadLayout.setOnLoadMoreListener(this);

 }




 @Override
 public void onRefresh() {
  swipeToLoadLayout.postDelayed(new Runnable() {
   @Override
   public void run() {
    adapter.refresh();
    swipeToLoadLayout.setRefreshing(false);
   }
  },2000);
 }

 @Override
 public void onLoadMore() {
  swipeToLoadLayout.postDelayed(new Runnable() {
   @Override
   public void run() {

    adapter.add();
    swipeToLoadLayout.setLoadingMore(false);
   }
  },2000);
 }
}

OK。確定有小同伴應用該框架時一向報錯,為何,看框架的源碼,有以下一段

this.mHeaderView = this.findViewById(id.swipe_refresh_header);
 this.mTargetView = this.findViewById(id.swipe_target);
 this.mFooterView = this.findViewById(id.swipe_load_more_footer);

可以看出,作者是依據固定的id值獲得的,所以在我們的結構文件中,必需應用固定的三個id。

若有需求,可移步我的github獲得源碼,源碼在systemwidgetdemo中。

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved