Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed a ViewPager on the top of PtrFrameLayout touch conflict issue #157

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
28 changes: 21 additions & 7 deletions README-cn.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
###### [关注我的GitHub吧,江湖救急,需要你的支持和帮助](http://www.liaohuqiu.net/cn/posts/follow-me-on-github/)
# 修改

Github: https://github.com/liaohuqiu
本分支对原有的Ultra Pull To Refresh进行了修改,使得其支持了上拉加载更多。和下拉刷新一样,上拉加载同样支持所有view。全部的逻辑都是仿造原有的下拉刷新而来,并且配置信息(阻抗,持续时间等)为两者共享。目前,实例项目demo中只有`主页面`和`自动刷新`页面演示了如何使用上拉加载更多,不过原理上与下拉刷新一致,应该比较好理解。

微博: http://weibo.com/liaohuqiu
如果想要使用本分支,请直接下载源码并导入IDE中。底部的gradle和maven目前并不支持。

注:本分支没有修改任何原有API。如果你之前已经在使用Ultra Pull To Refresh,那么你可以无缝的转换到本分支上来。

感谢`廖祜秋`开源了这个非常棒的下拉刷新库。他所写的代码可拓展性极高,使得我的修改工作也轻松不少。

本分支刚刚开发完成,可能会存在bug。如果有问题,欢迎指出!

Github: https://github.com/captainbupt

博客: http://blog.csdn.net/hwz2311245

# 添加的方法和类

- `setFooterView`:对应于`setHeaderView()`。在set完footer后,你需要调用 `addPtrUIHandler()`,这和设置header的机制是一样的。
- `PtrHandler2`:原有`PtrHandler`类的一个补充。当需要使用上拉加载更多的时候,你应该调用`setPtrHandler(new PtrHandler2())`,而不是`setPtrHandler(new PtrHandler())`。
- `PtrDefaultHandler2`:实现了默认的 `checkCanDoLoadMore()` 逻辑,可以适用于大部分的View。机制和`PtrDefaultHandler`一致。
- `PtrClassicDefaultFooter`:默认的footer,将默认header反转了过来。
- `setMode(Mode)`: Mode是本分支的一个新特性。通过调用`setMode`, 你可以任意的开启/关闭header或者footer。参数类型是一个枚举变量,可以通过以下方式调用:`setMode(Mode.BOTH)`.

---

Expand All @@ -16,10 +34,6 @@ Github: https://github.com/liaohuqiu

[APK下载](https://raw.githubusercontent.com/liaohuqiu/android-Ultra-Pull-To-Refresh/master/ptr-demo.apk)

#### 下拉刷新 + 加载更多?

本类库是单纯的下拉刷新。如果你需要用到`加载更多`,看这个项目: https://github.com/liaohuqiu/android-cube-app

#### 使用eclipse的同学请注意, Intellij IDEA / Android Studio 请忽略

**demo可以直接在eclipse中运行, 编译demo项目的同学看这里: http://www.liaohuqiu.net/cn/posts/compile-ultra-ptr-in-eclipse/**
Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
###### [Please follow me on GitHub, I need your support](http://www.liaohuqiu.net/posts/follow-me-on-github/)
# Modification

Github: https://github.com/liaohuqiu
I had made a modification of the Ultra-Pull-to-Refresh library and let it support load-more function. It supports every view as like as pull-to-refresh. All the logic is simulated to the pull-to-refresh and the configurations(resistance, duration etc.) are shared for both load-more and pull-to-refresh. Currently, only the `Home page(PtrDemoHomeFragment)` and `AutoRefresh(WithGridView)` page in demo are modified to demonstrate how to use load-more. But the mechanism should be the same as pull-to-refresh.

twitter: https://twitter.com/liaohuqiu
If your want to this fork, please download this project and import to your IDE manually. The gradle and maven below is not supported by this fort yet.

PS: All the API are not modified. If you have used the origin Ultra-Pull-To-Refresh library, there will be no need to change your code if you move to this fork.

Thank you for `Huqiu Liao`, who created such a great library. His code was so scalable and made my modification easier.

This fork is just developed and there would be bugs. Please feel free to report me if you found some bugs.

Github: https://github.com/captainbupt

# New functions and classes

- `setFooterView`: Corresponding to `setHeaderView()`. You may also need call `addPtrUIHandler()` after setting the footer view, which is the same mechanism as setting header.
- `PtrHandler2`: A complementary of `PtrHandler`. When using load more function, you should call `setPtrHandler(new PtrHandler2())`, rather than `setPtrHandler(new PtrHandler())`.
- `PtrDefaultHandler2`: Implemented a default `checkCanDoLoadMore()` logic. The same mechanism as `PtrDefaultHandler`.
- `PtrClassicDefaultFooter`: Serve as a default footer which is the reverse of the default header.
- `setMode(Mode)`: The mode is a new feature of this library. By using `setMode`, you can enable or disable either header or footer. The argument is an enum, you should it like `setMode(Mode.BOTH)`.

---

[![Build Status](https://travis-ci.org/liaohuqiu/android-Ultra-Pull-To-Refresh.svg?branch=master)](https://travis-ci.org/liaohuqiu/android-Ultra-Pull-To-Refresh)
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Ultra%20Pull%20To%20Refresh-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1180)

#### [中文版文档](https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh/blob/master/README-cn.md)

Wanna auto-load-more? This will be what you want: https://github.com/liaohuqiu/android-cube-app
#### [中文版文档](https://github.com/captainbupt/android-Ultra-Pull-To-Refresh-With-Load-More/blob/master/README-cn.md)

# Ultra Pull To Refresh

Expand Down
3 changes: 2 additions & 1 deletion ptr-demo/res/layout/fragmengt_ptr_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
cube_ptr:ptr_keep_header_when_refresh="true"
cube_ptr:ptr_pull_to_fresh="false"
cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
cube_ptr:ptr_resistance="1.7">
cube_ptr:ptr_resistance="1.7"
cube_ptr:ptr_mode="both">

<ScrollView
android:id="@+id/fragment_block_menu_scroll_view"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void onCreate() {

CubeDebug.DEBUG_IMAGE = true;
PtrFrameLayout.DEBUG = true;
PtrFrameLayout.DEBUG = false;
//PtrFrameLayout.DEBUG = false;

ImageLoaderFactory.setDefaultImageReSizer(DemoDuiTangImageReSizer.getInstance());
ImageLoaderFactory.setDefaultImageLoadHandler(new PtrImageLoadHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import in.srain.cube.mints.base.BlockMenuFragment;
import in.srain.cube.util.LocalDisplay;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrHandler2;
import in.srain.cube.views.ptr.demo.R;
import in.srain.cube.views.ptr.demo.ui.classic.*;
import in.srain.cube.views.ptr.demo.ui.storehouse.StoreHouseUsingPointList;
Expand Down Expand Up @@ -219,12 +222,34 @@ protected View createView(LayoutInflater inflater, ViewGroup container, Bundle s
ptrFrameLayout.setDurationToCloseHeader(1500);
ptrFrameLayout.setHeaderView(header);
ptrFrameLayout.addPtrUIHandler(header);
ptrFrameLayout.setPtrHandler(new PtrHandler() {

StoreHouseHeader footer = new StoreHouseHeader(getContext());
footer.setPadding(0, LocalDisplay.dp2px(20), 0, LocalDisplay.dp2px(20));
footer.initWithString("Ultra Footer");

ptrFrameLayout.setFooterView(footer);
ptrFrameLayout.addPtrUIHandler(footer);
ptrFrameLayout.setPtrHandler(new PtrHandler2() {
@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
}

@Override
public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
return PtrDefaultHandler2.checkContentCanBePulledUp(frame, content, footer);
}

@Override
public void onLoadMoreBegin(PtrFrameLayout frame) {
ptrFrameLayout.postDelayed(new Runnable() {
@Override
public void run() {
ptrFrameLayout.refreshComplete();
}
}, 1500);
}

@Override
public void onRefreshBegin(PtrFrameLayout frame) {
ptrFrameLayout.postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
Expand Down Expand Up @@ -48,6 +49,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, l
listViewDataAdapter.setViewHolderClass(this, ViewHolder.class);

ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {

@Override
public void onRefreshBegin(PtrFrameLayout frame) {
ptrFrameLayout.postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected void setupViews(final PtrClassicFrameLayout ptrFrame) {
ptrFrame.postDelayed(new Runnable() {
@Override
public void run() {
ptrFrame.autoRefresh(true);
ptrFrame.autoLoadMore(true);
}
}, 150);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import in.srain.cube.mints.base.TitleBaseFragment;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.demo.R;

Expand All @@ -20,7 +21,17 @@ public View createView(LayoutInflater inflater, ViewGroup container, Bundle save

final PtrClassicFrameLayout ptrFrame = (PtrClassicFrameLayout) contentView.findViewById(R.id.fragment_rotate_header_with_text_view_frame);
ptrFrame.setLastUpdateTimeRelateObject(this);
ptrFrame.setPtrHandler(new PtrDefaultHandler() {
ptrFrame.setPtrHandler(new PtrDefaultHandler2() {
@Override
public void onLoadMoreBegin(PtrFrameLayout frame) {
frame.postDelayed(new Runnable() {
@Override
public void run() {
ptrFrame.refreshComplete();
}
}, 1500);
}

@Override
public void onRefreshBegin(PtrFrameLayout frame) {
frame.postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.widget.GridView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
Expand All @@ -21,8 +22,10 @@
import in.srain.cube.views.list.ViewHolderCreator;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrHandler2;
import in.srain.cube.views.ptr.demo.R;
import in.srain.cube.views.ptr.demo.data.DemoRequestData;
import in.srain.cube.views.ptr.demo.ui.MaterialStyleFragment;
Expand Down Expand Up @@ -65,16 +68,18 @@ public ViewHolderBase<JsonData> createViewHolder(int position) {

mPtrFrame = (PtrClassicFrameLayout) contentView.findViewById(R.id.rotate_header_grid_view_frame);
mPtrFrame.setLastUpdateTimeRelateObject(this);
mPtrFrame.setPtrHandler(new PtrHandler() {
mPtrFrame.setPtrHandler(new PtrDefaultHandler2() {

@Override
public void onRefreshBegin(PtrFrameLayout frame) {
public void onLoadMoreBegin(PtrFrameLayout frame) {
updateData();
}

@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
public void onRefreshBegin(PtrFrameLayout frame) {
updateData();
}

});
// the following are default settings
mPtrFrame.setResistance(1.7f);
Expand Down Expand Up @@ -109,7 +114,7 @@ public void onRequestFinish(final JsonData data) {
@Override
public void run() {
mAdapter.getDataList().clear();
mAdapter.getDataList().addAll(data.optJson("data").optJson("list").toArrayList());
mAdapter.getDataList().addAll(data.optJson("data").optJson("list").toArrayList().subList(0,10));
mPtrFrame.refreshComplete();
mAdapter.notifyDataSetChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import in.srain.cube.mints.base.TitleBaseFragment;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;

import in.srain.cube.mints.base.TitleBaseActivity;
import in.srain.cube.request.JsonData;
import in.srain.cube.request.RequestFinishHandler;
Expand Down Expand Up @@ -149,6 +150,13 @@ public boolean checkCanDoRefresh() {
}
return mCurrentFragment.checkCanDoRefresh();
}

public boolean checkCanDoLoadMore() {
if (mCurrentFragment == null) {
return true;
}
return mCurrentFragment.checkCanDoLoadMore();
}
}

private class HomeCatItemViewHolder extends TabPageIndicator.ViewHolderBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import in.srain.cube.app.CubeFragment;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;
Expand Down Expand Up @@ -51,8 +52,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position >= 0) {
JsonData js = mAdapter.getItem(position);
final String url = js!=null?js.optString("pic"):null;
JsonData js = mAdapter.getItem(position);
final String url = js != null ? js.optString("pic") : null;
if (!TextUtils.isEmpty(url)) {
getContext().pushFragmentToBackStack(MaterialStyleFragment.class, url);
}
Expand Down Expand Up @@ -88,4 +89,12 @@ public boolean checkCanDoRefresh() {
return mListView.getFirstVisiblePosition() == 0 && mListView.getChildAt(0).getTop() == 0;
}

public boolean checkCanDoLoadMore() {
if (mAdapter.getCount() == 0 || mListView == null) {
return true;
}
CLog.d("test", "checkCanDoRefresh: %s %s", mListView.getFirstVisiblePosition(), mListView.getChildAt(0).getTop());
return mListView.getLastVisiblePosition() < mAdapter.getCount() - 1 || mListView.getChildAt(mAdapter.getCount() - 1)
.getBottom() > mListView.getPaddingBottom();
}
}
3 changes: 3 additions & 0 deletions ptr-lib/res/values-zh/cube_ptr_string.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="cube_ptr_pull_up">上拉</string>
<string name="cube_ptr_pull_up_to_load">上拉加载</string>
<string name="cube_ptr_release_to_load">释放加载</string>
<string name="cube_ptr_pull_down">下拉</string>
<string name="cube_ptr_pull_down_to_refresh">下拉刷新</string>
<string name="cube_ptr_release_to_refresh">释放刷新</string>
Expand Down
25 changes: 16 additions & 9 deletions ptr-lib/res/values/cube_ptr_attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@

<declare-styleable name="PtrFrameLayout">
<!-- Optional. If you put header and content in xml, you can you these to specify them. -->
<attr name="ptr_header" format="reference" />
<attr name="ptr_content" format="reference" />
<attr name="ptr_header" format="reference"/>
<attr name="ptr_content" format="reference"/>
<attr name="ptr_footer" format="reference"/>

<!-- the resistance when you are moving the frame -->
<attr name="ptr_resistance" format="float" />
<attr name="ptr_resistance" format="float"/>

<!-- the ration of the height of the header to trigger refresh -->
<attr name="ptr_ratio_of_header_height_to_refresh" format="float" />
<attr name="ptr_ratio_of_header_height_to_refresh" format="float"/>

<attr name="ptr_duration_to_close" format="integer" />
<attr name="ptr_duration_to_close_header" format="integer" />
<attr name="ptr_duration_to_close" format="integer"/>
<attr name="ptr_duration_to_close_header" format="integer"/>

<!-- pull to refresh, otherwise release to refresh, default is release to refresh -->
<attr name="ptr_pull_to_fresh" format="boolean" />
<attr name="ptr_pull_to_fresh" format="boolean"/>

<!-- keep header when refreshing-->
<attr name="ptr_keep_header_when_refresh" format="boolean" />
<attr name="ptr_keep_header_when_refresh" format="boolean"/>
<attr name="ptr_mode" format="enum">
<enum name="none" value="0"/>
<enum name="refresh" value="1"/>
<enum name="loadMore" value="2"/>
<enum name="both" value="3"/>
</attr>
</declare-styleable>

<declare-styleable name="PtrClassicHeader">
<attr name="ptr_rotate_ani_time" format="integer" />
<attr name="ptr_rotate_ani_time" format="integer"/>
</declare-styleable>
</resources>
3 changes: 3 additions & 0 deletions ptr-lib/res/values/cube_ptr_string.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="cube_ptr_pull_up">Pull Up</string>
<string name="cube_ptr_pull_up_to_load">Pull Up to Load</string>
<string name="cube_ptr_release_to_load">Release To Load</string>
<string name="cube_ptr_pull_down">Pull Down</string>
<string name="cube_ptr_pull_down_to_refresh">Pull Down to Refresh</string>
<string name="cube_ptr_release_to_refresh">Release To Refresh</string>
Expand Down
Loading