Skip to content

Commit

Permalink
添加CircleLoadingLayout动画&优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xiaojun committed May 8, 2017
1 parent aeedede commit 14c0676
Show file tree
Hide file tree
Showing 16 changed files with 429 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ A refresh layout, can refresh RecyclerView for all LayoutManager, NestedScrollVi



- custom refresh animation! There is two animation style now, I will support more animation in future...

![custom_anim](/Screenshot/custom_anim.gif)





# Depedency
Expand Down
Binary file added Screenshot/custom_anim.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<activity android:name=".RecyclerViewActivity"/>
<activity android:name=".NestedScrollViewActivity"/>
<activity android:name=".CustomRefreshAnimActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.xrefreshlayout.sample;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.lxj.xrefreshlayout.XRefreshLayout;
import com.lxj.xrefreshlayout.loadinglayout.DefaultLoadingLayout;
import com.lxj.xrefreshlayout.loadinglayout.CircleLoadingLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Created by dance on 2017/5/8.
*/

public class CustomRefreshAnimActivity extends AppCompatActivity {
@BindView(R.id.xrefreshLayout)
XRefreshLayout xrefreshLayout;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_refresh_anim);
ButterKnife.bind(this);



xrefreshLayout.setOnRefreshListener(new XRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
doNothing();
}

@Override
public void onLoadMore() {
doNothing();
}
});

}

private void doNothing() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(CustomRefreshAnimActivity.this, "Refresh Successfully!", Toast.LENGTH_SHORT).show();
xrefreshLayout.completeRefresh();
}
},2500);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.dafulat:
xrefreshLayout.setLoadingLayout(new DefaultLoadingLayout());
break;
case R.id.circle:
CircleLoadingLayout circleLoadingLayout = new CircleLoadingLayout();
// circleLoadingLayout.setCircleColor(Color.GREEN);
xrefreshLayout.setLoadingLayout(circleLoadingLayout);
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_refresh,menu);
return true;
}
}
10 changes: 4 additions & 6 deletions sample/src/main/java/com/xrefreshlayout/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@

public class MainActivity extends AppCompatActivity {

@BindView(R.id.btn_recyclerview)
Button btnRecyclerview;
@BindView(R.id.btn_scrollview)
Button btnScrollview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}

@OnClick({R.id.btn_recyclerview, R.id.btn_scrollview})
@OnClick({R.id.btn_recyclerview, R.id.btn_scrollview, R.id.btn_animation})
public void onViewClicked(View view) {
Intent intent = null;
switch (view.getId()) {
Expand All @@ -34,6 +29,9 @@ public void onViewClicked(View view) {
case R.id.btn_scrollview:
intent = new Intent(this,NestedScrollViewActivity.class);
break;
case R.id.btn_animation:
intent = new Intent(this,CustomRefreshAnimActivity.class);
break;
}
intent.putExtra("title",((Button)view).getText().toString());
startActivity(intent);
Expand Down
28 changes: 28 additions & 0 deletions sample/src/main/res/layout/activity_custom_refresh_anim.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<com.lxj.xrefreshlayout.XRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/xrefreshLayout"
android:layout_height="match_parent">

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:fontFamily="monospace"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:layout_gravity="center"
android:text="Refresh Animation:\n
1. DefaultLoadingLayout\n
2. CircleLoadingLayout\n
3. Custom your anim!"
android:textSize="22sp" />


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


</com.lxj.xrefreshlayout.XRefreshLayout>
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Custom Refresh Animation"
android:id="@+id/btn_animation"
android:textAllCaps="false"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Expand Down
14 changes: 14 additions & 0 deletions sample/src/main/res/menu/custom_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="never"
android:title="DefaultLoadingLayout"
android:id="@+id/dafulat"
/>
<item
app:showAsAction="never"
android:title="CircleLoadingLayout"
android:id="@+id/circle"
/>
</menu>
19 changes: 19 additions & 0 deletions sample/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="never"
android:title="LinearLayoutManager"
android:id="@+id/linear_manager"
/>
<item
app:showAsAction="never"
android:title="GridLayoutManager"
android:id="@+id/grid_manager"
/>
<item
app:showAsAction="never"
android:title="StaggeredLayoutManager"
android:id="@+id/staggered_manager"
/>
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public XRefreshLayout(Context context, AttributeSet attrs, int defStyle) {


protected void initLoadingLayout() {
if(header!=null)removeView(header);
if(footer!=null)removeView(footer);

header = loadingLayout.createLoadingHeader(getContext(), this);
footer = loadingLayout.createLoadingFooter(getContext(), this);

Expand Down Expand Up @@ -124,6 +127,8 @@ public void onNestedScrollAccepted(View child, View target, int axes) {

@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
loadingLayout.initAndResetHeader();
loadingLayout.initAndResetFooter();
isPullHeader = false;
isPullFooter = false;
return true;
Expand All @@ -136,8 +141,6 @@ public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes
*/
@Override
public void onStopNestedScroll(View child) {
L.d("onStopNestedScroll isRelease:"+isRelease + " isPullHeader:"+isPullHeader
+" isPullFooter: "+isPullFooter);
isRelease = true;
if (isPullHeader) {
if (getScrollY() <= -header.getMeasuredHeight()) {
Expand Down Expand Up @@ -309,7 +312,10 @@ public void computeScroll() {
* @param loadingLayout
*/
public void setLoadingLayout(ILoadingLayout loadingLayout) {
if(isRelease && isSmoothScrolling)return;
this.loadingLayout = loadingLayout;
initLoadingLayout();
requestLayout();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.lxj.xrefreshlayout.loadinglayout;

import android.animation.FloatEvaluator;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;

import com.lxj.xrefreshlayout.R;

/**
* Created by dance on 2017/5/8.
*/

public class CircleLoadingLayout implements ILoadingLayout {

private CircleLoadingView headerProgressView;
private CircleLoadingView footerProgressView;

private FloatEvaluator floatEval = new FloatEvaluator();
private View header;
private float startVal = 0f;

private final int MSG_HEADER = 1;
private final int MSG_FOOTER = 2;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case MSG_HEADER:
rotateView(headerProgressView);
break;
case MSG_FOOTER:
rotateView(footerProgressView);
break;
}

handler.sendEmptyMessageDelayed(msg.what, 1200);
}
};

private int circleColor = Color.BLUE;

/**
* set the circle color!
* @param color
*/
public void setCircleColor(int color){
this.circleColor = color;
}

@Override
public View createLoadingHeader(Context context, ViewGroup parent) {
header = (View) LayoutInflater.from(context).inflate(R.layout.xrl_progress_header, parent, false);
headerProgressView = (CircleLoadingView) header.findViewById(R.id.progressView);
headerProgressView.setProgressColor(circleColor);
return header;
}

@Override
public View createLoadingFooter(Context context, ViewGroup parent) {
View footer = (View) LayoutInflater.from(context).inflate(R.layout.xrl_progress_footer, parent, false);
footerProgressView = (CircleLoadingView) footer.findViewById(R.id.progressView);
footerProgressView.setProgressColor(circleColor);
return footer;
}

@Override
public void initAndResetHeader() {
handler.removeCallbacksAndMessages(null);
headerProgressView.setProgress(0);
headerProgressView.setRotation(0);
if(startVal==0f){
header.post(new Runnable() {
@Override
public void run() {
//计算当headerProgressView开始完全可见的百分比
startVal = headerProgressView.getHeight()*1f/header.getHeight();
}
});
}
}

@Override
public void initAndResetFooter() {
handler.removeCallbacksAndMessages(null);
footerProgressView.setStart(-270);
footerProgressView.setProgress(0);
footerProgressView.setRotation(0);

}

@Override
public void onPullHeader(float percent) {
if(percent>=startVal){
float p = (percent-startVal)/(1-startVal);
headerProgressView.setProgress(p);
}
}

@Override
public void onPullFooter(float percent) {
if(percent>=startVal){
float p = (percent-startVal)/(1-startVal);
footerProgressView.setProgress(p);
}
}

@Override
public void onHeaderRefreshing() {
handler.sendEmptyMessage(MSG_HEADER);
}

@Override
public void onFooterRefreshing() {
handler.sendEmptyMessage(MSG_FOOTER);
}

private void rotateView(View view){
ViewCompat.animate(view).rotationXBy(360).setDuration(1000)
.setInterpolator(new DecelerateInterpolator())
.start();
}

}
Loading

0 comments on commit 14c0676

Please sign in to comment.