Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
grumoon committed Jan 20, 2015
1 parent 652bf85 commit 3242db8
Show file tree
Hide file tree
Showing 15 changed files with 476 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
* Created by Administrator on 2015/1/14.
* Created by grumoon on 2015/1/14.
*/
public class PTRApplication extends Application {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


/**
* Created by grumoon on 15/1/17.
*/
public class ListViewAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
private String[] imageUrls;
private List<String> imageUrls;

private DisplayImageOptions options;

public ListViewAdapter(Context context, String[] imageUrls) {
public ListViewAdapter(Context context, List<String> imageUrls) {

this.layoutInflater = LayoutInflater.from(context);
this.imageUrls = imageUrls;
Expand All @@ -42,15 +44,15 @@ public int getCount() {
if (imageUrls == null) {
return 0;
}
return imageUrls.length;
return imageUrls.size();
}

@Override
public Object getItem(int position) {
if (imageUrls == null || position > imageUrls.length - 1) {
if (imageUrls == null || position > imageUrls.size() - 1) {
return null;
}
return imageUrls[position];
return imageUrls.get(position);
}

@Override
Expand All @@ -72,7 +74,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
}


ImageLoader.getInstance().displayImage(imageUrls[position], viewHolder.ivCar, options);
ImageLoader.getInstance().displayImage(imageUrls.get(position), viewHolder.ivCar, options);

return convertView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
Expand All @@ -22,7 +21,7 @@


/**
* Created by Administrator on 2015/1/20.
* Created by grumoon on 2015/1/20.
*/
public class WindmillDrawable extends Drawable implements Animatable {

Expand All @@ -41,6 +40,8 @@ public class WindmillDrawable extends Drawable implements Animatable {

private boolean isFirstDraw = true;

private boolean isAnimating;

public WindmillDrawable(Context context, View parent) {
resources = context.getResources();
windmill = BitmapFactory.decodeResource(resources, R.drawable.windmill);
Expand Down Expand Up @@ -68,8 +69,6 @@ public void draw(Canvas canvas) {
}

Paint p = new Paint();

Log.v(TAG, matrix.toString());
canvas.drawBitmap(windmill, matrix, p);
}

Expand Down Expand Up @@ -97,15 +96,17 @@ public void postRotation(int degree) {
@Override
public void start() {
parent.startAnimation(animation);
isAnimating = true;
}

@Override
public void stop() {
parent.clearAnimation();
isAnimating = false;
}

@Override
public boolean isRunning() {
return false;
return isAnimating;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.grumoon.androidultrapulltorefreshdemo.R;
Expand All @@ -18,7 +14,7 @@
import in.srain.cube.views.ptr.PtrUIHandler;

/**
* Created by Administrator on 2015/1/20.
* Created by grumoon on 2015/1/20.
*/
public class WindmillHeader extends FrameLayout implements PtrUIHandler {

Expand All @@ -35,11 +31,6 @@ public class WindmillHeader extends FrameLayout implements PtrUIHandler {

private WindmillDrawable drawable;

// 旋转动画
private RotateAnimation animation;
// 反向旋转动画
private RotateAnimation reverseAnimation;

public WindmillHeader(Context context) {
this(context, null);
}
Expand All @@ -61,44 +52,19 @@ public WindmillHeader(Context context, AttributeSet attrs, int defStyleAttr) {
*/
private void init(Context context) {

initAnimation();



inflater = LayoutInflater.from(context);

/**
* 头部
*/
headView = (ViewGroup) inflater.inflate(R.layout.windmill_header, this, true);
ivWindmill = (ImageView) headView.findViewById(R.id.iv_windmill);
tvHeadTitle = (TextView) headView.findViewById(R.id.tv_head_title);

drawable = new WindmillDrawable(context,ivWindmill);

drawable = new WindmillDrawable(context, ivWindmill);
ivWindmill.setImageDrawable(drawable);


}

/**
* 初始化动画
*/
private void initAnimation() {
// 旋转
animation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(300);
animation.setFillAfter(true);

// 反向旋转
reverseAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
reverseAnimation.setInterpolator(new LinearInterpolator());
reverseAnimation.setDuration(300);
reverseAnimation.setFillAfter(true);
}


@Override
public void onUIReset(PtrFrameLayout ptrFrameLayout) {
tvHeadTitle.setText("下拉刷新");
Expand Down Expand Up @@ -128,5 +94,19 @@ public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte
drawable.postRotation(currentPos - lastPos);
invalidate();
}

final int mOffsetToRefresh = frame.getOffsetToRefresh();
if (currentPos < mOffsetToRefresh && lastPos >= mOffsetToRefresh) {
if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
tvHeadTitle.setText("下拉刷新");

}
} else if (currentPos > mOffsetToRefresh && lastPos <= mOffsetToRefresh) {
if (isUnderTouch && status == PtrFrameLayout.PTR_STATUS_PREPARE) {
tvHeadTitle.setText("松开刷新");
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import com.grumoon.androidultrapulltorefreshdemo.adapter.ListViewAdapter;
import com.grumoon.androidultrapulltorefreshdemo.util.Constants;

import java.util.ArrayList;
import java.util.Arrays;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
Expand Down Expand Up @@ -61,7 +64,7 @@ public void run() {


private void getData() {
adapter = new ListViewAdapter(getActivity(), Constants.SMALL_IMAGE_URLS);
adapter = new ListViewAdapter(getActivity(), Arrays.asList(Constants.SMALL_IMAGE_URLS));
lvMain.setAdapter(adapter);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,151 @@
package com.grumoon.androidultrapulltorefreshdemo.ui;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.grumoon.androidultrapulltorefreshdemo.R;
import com.grumoon.androidultrapulltorefreshdemo.adapter.ListViewAdapter;
import com.grumoon.androidultrapulltorefreshdemo.header.WindmillHeader;
import com.grumoon.androidultrapulltorefreshdemo.util.Constants;
import com.grumoon.androidultrapulltorefreshdemo.view.GetMoreListView;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
* A simple {@link Fragment} subclass.
*/
public class GetMoreListViewFragment extends Fragment {


public GetMoreListViewFragment() {
// Required empty public constructor
}
private static final int PAGE_NUM = 10;

private GetMoreListView gmlvMain;
private BaseAdapter adapter;

private PtrFrameLayout ptr;

private int currentPage = 0;

private Handler handler;

private List<String> imageUrlList;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
imageUrlList = new ArrayList<String>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_get_more_list_view, container, false);
View v = inflater.inflate(R.layout.fragment_get_more_list_view, container, false);
initView(v);
return v;
}


private void initView(View v) {

gmlvMain = (GetMoreListView) v.findViewById(R.id.gmlv_main);

gmlvMain.setOnGetMoreListener(new GetMoreListView.OnGetMoreListener() {
@Override
public void onGetMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {

getData(false);

}
}, 2000);

}
});

adapter = new ListViewAdapter(getActivity(), imageUrlList);
gmlvMain.setAdapter(adapter);

ptr = (PtrFrameLayout) v.findViewById(R.id.ptr_main);

final WindmillHeader header = new WindmillHeader(getActivity());


ptr.setHeaderView(header);
ptr.addPtrUIHandler(header);


ptr.setPtrHandler(new PtrDefaultHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
gmlvMain.setHasMore();
getData(true);

}
}, 2000);

}
});

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ptr.autoRefresh();
}
}, 100);

}


private void getData(boolean isRefresh) {

if (isRefresh) {
currentPage = 0;
imageUrlList.clear();
}

List<String> newImageUrlList = new ArrayList<String>();

int start = currentPage * PAGE_NUM;
int to = Math.min((currentPage + 1) * PAGE_NUM, Constants.SMALL_IMAGE_URLS.length);

for (int i = start; i < to; i++) {
newImageUrlList.add(Constants.SMALL_IMAGE_URLS[i]);
}

if (newImageUrlList.size() < 10) {
gmlvMain.setNoMore();
}

currentPage++;

imageUrlList.addAll(newImageUrlList);

adapter.notifyDataSetChanged();

gmlvMain.getMoreComplete();
ptr.refreshComplete();
}

}
Loading

0 comments on commit 3242db8

Please sign in to comment.