Skip to content

Commit

Permalink
添加xscrollview的滑动监听
Browse files Browse the repository at this point in the history
  • Loading branch information
huxq17 committed Sep 28, 2016
1 parent 7bc5c0b commit 6c72062
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

```groovy
dependencies {
compile 'com.huxq17.xrefreshview:xrefreshview:3.5.2'
compile 'com.huxq17.xrefreshview:xrefreshview:3.5.5'
//依赖下面的库
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
Expand All @@ -31,6 +31,9 @@ dependencies {
还有listview,scrollview,webview等其他的view就不一一截图了。

### 更新日志:<br/>
2016-9-28:
1.添加xscrollview的滑动监听

2016-9-6:
1.解决issue[#36](https://github.com/huxq17/XRefreshView/issues/36)

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile project(':library')
compile 'com.huxq17.xrefreshview:xrefreshview:3.5.2'
compile 'com.huxq17.xrefreshview:xrefreshview:3.5.5'
//依赖下面的库
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/andview/example/ScrollAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.andview.example.ui.raindrop.CustomerFooter;
import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XScrollView;

public class ScrollAdapter extends PagerAdapter {

Expand Down Expand Up @@ -41,6 +43,16 @@ public int getCount() {
public Object instantiateItem(ViewGroup view, int position) {
final View imageLayout = mInflater.inflate(R.layout.adapter_scrollview, view, false);
final XRefreshView outView = (XRefreshView) imageLayout.findViewById(R.id.custom_view);
final XScrollView scrollView = (XScrollView) imageLayout.findViewById(R.id.xscrollview);
scrollView.setOnScrollListener(new XScrollView.OnScrollListener() {
@Override
public void onScrollStateChanged(ScrollView view, int scrollState, boolean arriveBottom) {
}

@Override
public void onScroll(int l, int t, int oldl, int oldt) {
}
});
outView.setAutoRefresh(false);
outView.setPullLoadEnable(true);
outView.setPinnedTime(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void onCreate(Bundle savedInstanceState) {
mViewPager = (ViewPager) findViewById(R.id.index_viewpager);
ScrollAdapter adapter = new ScrollAdapter(this);
mViewPager.setAdapter(adapter);

}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/adapter_scrollview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xrefreshview:autoRefresh="false" >

<com.andview.refreshview.XScrollView
android:id="@+id/gv"
android:id="@+id/xscrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

Expand Down
2 changes: 1 addition & 1 deletion library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.5.2
VERSION_NAME=3.5.5
GROUP=com.huxq17.xrefreshview
POM_NAME=Android universal pull to refresh library
POM_ARTIFACT_ID=xrefreshview
Expand Down
32 changes: 23 additions & 9 deletions library/src/main/java/com/andview/refreshview/XScrollView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

public class XScrollView extends ScrollView {

// 外部设置的监听方法
private OnScrollListener onScrollListener;
private OnScrollListener onScrollListener, mScrollListener;
// 是否在触摸状态
private boolean inTouch = false;
// 上次滑动的最后位置
Expand All @@ -33,18 +32,27 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
if (t != oldt) {
// 有手指触摸,并且位置有滚动
onScrollListener.onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_TOUCH_SCROLL, isBottom());
if (mScrollListener != null) {
mScrollListener.onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_TOUCH_SCROLL, isBottom());
}
}
} else {
if (t != oldt) {
// 没有手指触摸,并且位置有滚动,就可以简单的认为是在fling
onScrollListener.onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_FLING, isBottom());
if (mScrollListener != null) {
mScrollListener.onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_FLING, isBottom());
}
// 记住上次滑动的最后位置
lastT = t;
removeCallbacks(mRunnable);
postDelayed(mRunnable, 20);
}
}
onScrollListener.onScroll(l, t, oldl, oldt);
if (mScrollListener != null) {
mScrollListener.onScroll(l, t, oldl, oldt);
}
}

private Runnable mRunnable = new Runnable() {
Expand All @@ -53,6 +61,9 @@ public void run() {
if (lastT == getScrollY() && !inTouch) {
// 如果上次的位置和当前的位置相同,可认为是在空闲状态
onScrollListener.onScrollStateChanged(XScrollView.this, OnScrollListener.SCROLL_STATE_IDLE, isBottom());
if (mScrollListener != null) {
mScrollListener.onScrollStateChanged(XScrollView.this, OnScrollListener.SCROLL_STATE_IDLE, isBottom());
}
}
}
};
Expand All @@ -62,14 +73,9 @@ private boolean isBottom() {
}


/**
* 设置滚动监听事件
*
* @param onScrollListener {@link OnScrollListener} 滚动监听事件(注意类的不同,虽然名字相同)
*/
public void setOnScrollListener(XRefreshView parent, OnScrollListener onScrollListener) {
protected void setOnScrollListener(XRefreshView parent, OnScrollListener scrollListener) {
mParent = parent;
this.onScrollListener = onScrollListener;
this.onScrollListener = scrollListener;
mParent.addTouchLifeCycle(new XRefreshView.TouchLifeCycle() {
@Override
public void onTouch(MotionEvent event) {
Expand All @@ -91,6 +97,14 @@ public void onTouch(MotionEvent event) {
});
}

/**
* 设置XScrollView的滚动监听
* @param scrollListener
*/
public void setOnScrollListener(OnScrollListener scrollListener) {
mScrollListener = scrollListener;
}

/**
* 滚动监听事件
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import com.andview.refreshview.XRefreshView;
Expand Down Expand Up @@ -134,7 +135,8 @@ public void onViewAttachedToWindow(VH holder) {
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
if (recyclerView.getParent() instanceof XRefreshView) {
ViewParent parent = recyclerView.getParent();
if (parent != null && parent instanceof XRefreshView) {
mParent = (XRefreshView) recyclerView.getParent();
if (mParent != null && !observer.hasAttached()) {
observer.setData(this, mParent);
Expand Down

0 comments on commit 6c72062

Please sign in to comment.