Skip to content

Commit b3cb4ad

Browse files
committed
fix the bug of ScrollListener and ItemClickListener.
1 parent 819a4b4 commit b3cb4ad

File tree

12 files changed

+145
-47
lines changed

12 files changed

+145
-47
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ startAnim则是在onRefresh/onLoadMore之后才会回调的过程(此处是显
293293
- 修复刷新状态重复回调问题
294294
- 添加Apache License 2.0开源协议
295295

296+
#### v1.05
297+
- 修复触摸监听失效问题
298+
296299
#### v1.04
297300
##### 新增功能
298301
- **第二次重构完成**,将核心逻辑拆分为RefreshProcessor、AnimProcessor、OverScrollProcessor、CoProcessor

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66
defaultConfig {
77
applicationId 'com.lcodecore.twinklingrefreshlayout'
88
minSdkVersion 14
9-
targetSdkVersion 23
9+
targetSdkVersion 22
1010
versionCode 1
1111
versionName "1.0"
1212
}

app/src/main/java/com/lcodecore/twinklingrefreshlayout/MusicActivity.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.support.v7.widget.Toolbar;
77
import android.view.View;
8+
import android.widget.AdapterView;
89
import android.widget.ListView;
910

10-
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
1111
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
1212
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
13+
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
1314
import com.lcodecore.twinklingrefreshlayout.adapter.MusicAdapter;
15+
import com.lcodecore.twinklingrefreshlayout.utils.ToastUtil;
1416

15-
//TODO 1.优化SwipeCircle显示问题 2.有FixedHeader的界面fling有问题
17+
//TODO 有FixedHeader的界面fling有问题
1618
public class MusicActivity extends AppCompatActivity {
1719

1820
private MusicAdapter adapter;
@@ -46,6 +48,13 @@ private void setupListView(ListView listView) {
4648
listView.setAdapter(adapter);
4749
adapter.refreshCard();
4850

51+
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
52+
@Override
53+
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
54+
ToastUtil.show("item clicked!");
55+
}
56+
});
57+
4958
refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
5059
@Override
5160
public void onRefresh(final TwinklingRefreshLayout refreshLayout) {

app/src/main/java/com/lcodecore/twinklingrefreshlayout/ScienceActivity.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.lcodecore.twinklingrefreshlayout;
22

3+
import android.os.Bundle;
34
import android.os.Handler;
45
import android.support.v7.app.AppCompatActivity;
5-
import android.os.Bundle;
66
import android.view.View;
7+
import android.widget.AdapterView;
78
import android.widget.GridView;
89

910
import com.lcodecore.tkrefreshlayout.Footer.LoadingView;
10-
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
1111
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
1212
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
13+
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
1314
import com.lcodecore.twinklingrefreshlayout.adapter.ScienceAdapter;
15+
import com.lcodecore.twinklingrefreshlayout.utils.ToastUtil;
1416

1517
public class ScienceActivity extends AppCompatActivity {
1618

@@ -45,6 +47,13 @@ private void setupGridView(GridView gridView) {
4547
gridView.setAdapter(adapter);
4648
adapter.refreshCard();
4749

50+
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
51+
@Override
52+
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
53+
ToastUtil.show("item clicked!");
54+
}
55+
});
56+
4857
refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
4958
@Override
5059
public void onRefresh(final TwinklingRefreshLayout refreshLayout) {

app/src/main/java/com/lcodecore/twinklingrefreshlayout/TkApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lcodecore.twinklingrefreshlayout;
22

33
import android.app.Application;
4+
import android.content.Context;
45
import android.os.StrictMode;
56

67
import com.squareup.leakcanary.LeakCanary;
@@ -13,10 +14,15 @@
1314
*/
1415

1516
public class TkApplication extends Application {
17+
18+
public static Context appContext;
19+
1620
@Override
1721
public void onCreate() {
1822
super.onCreate();
1923

24+
appContext = this;
25+
2026
if (LeakCanary.isInAnalyzerProcess(this)) {
2127
return;
2228
}

app/src/main/java/com/lcodecore/twinklingrefreshlayout/adapter/FoodAdapter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lcodecore.twinklingrefreshlayout.adapter;
22

33
import android.content.Context;
4+
import android.view.View;
45
import android.view.ViewGroup;
56
import android.widget.ImageView;
67
import android.widget.TextView;
@@ -9,6 +10,7 @@
910
import com.lcodecore.twinklingrefreshlayout.adapter.base.BaseRecyclerAdapter;
1011
import com.lcodecore.twinklingrefreshlayout.adapter.base.CommonHolder;
1112
import com.lcodecore.twinklingrefreshlayout.beans.Food;
13+
import com.lcodecore.twinklingrefreshlayout.utils.ToastUtil;
1214
import com.lcodecore.twinklingrefreshlayout.views.CircleImageView;
1315

1416
import butterknife.Bind;
@@ -47,6 +49,13 @@ public void bindData(Food food) {
4749
iv_food.setImageResource(food.imageSrc);
4850
tv_food.setText(food.title);
4951
tv_info.setText(food.info);
52+
53+
itemView.setOnClickListener(new View.OnClickListener() {
54+
@Override
55+
public void onClick(View view) {
56+
ToastUtil.show("item clicked!");
57+
}
58+
});
5059
}
5160
}
5261
}

app/src/main/java/com/lcodecore/twinklingrefreshlayout/adapter/PhotoAdapter.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.lcodecore.twinklingrefreshlayout.adapter;
22

33
import android.content.Context;
4+
import android.view.View;
45
import android.view.ViewGroup;
56
import android.widget.ImageView;
67
import android.widget.TextView;
78

89
import com.lcodecore.twinklingrefreshlayout.R;
910
import com.lcodecore.twinklingrefreshlayout.adapter.base.BaseRecyclerAdapter;
1011
import com.lcodecore.twinklingrefreshlayout.adapter.base.CommonHolder;
11-
import com.lcodecore.twinklingrefreshlayout.beans.Food;
1212
import com.lcodecore.twinklingrefreshlayout.beans.Photo;
13+
import com.lcodecore.twinklingrefreshlayout.utils.ToastUtil;
1314

1415
import butterknife.Bind;
1516

@@ -39,6 +40,13 @@ public CardHolder(Context context, ViewGroup root) {
3940
public void bindData(Photo photo) {
4041
iv_pic.setImageResource(photo.imgSrc);
4142
tv_info.setText(photo.name);
43+
44+
itemView.setOnClickListener(new View.OnClickListener() {
45+
@Override
46+
public void onClick(View view) {
47+
ToastUtil.show("item clicked!");
48+
}
49+
});
4250
}
4351
}
4452
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lcodecore.twinklingrefreshlayout.utils;
2+
3+
import android.widget.Toast;
4+
5+
import com.lcodecore.twinklingrefreshlayout.TkApplication;
6+
7+
/**
8+
* Created by lcodecore on 2017/2/28.
9+
*/
10+
11+
public class ToastUtil {
12+
public static void show(String msg){
13+
Toast.makeText(TkApplication.appContext, msg, Toast.LENGTH_SHORT).show();
14+
}
15+
}

library/src/main/java/com/lcodecore/tkrefreshlayout/OverScrollProcessor.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public boolean onTouch(View v, MotionEvent event) {
9797

9898
if (!cp.enableOverScroll()) return;
9999

100-
if (mChildView instanceof AbsListView) {
100+
/*if (mChildView instanceof AbsListView) {
101101
((AbsListView) mChildView).setOnScrollListener(new AbsListView.OnScrollListener() {
102102
@Override
103103
public void onScrollStateChanged(AbsListView view, int scrollState) {
@@ -138,7 +138,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
138138
super.onScrollStateChanged(recyclerView, newState);
139139
}
140140
});
141-
}
141+
}*/
142142
}
143143

144144
private void obtainTracker(MotionEvent event) {
@@ -179,14 +179,30 @@ public void handleMessage(Message msg) {
179179

180180
View mChildView = cp.getContent();
181181

182-
if (!(mChildView instanceof AbsListView || mChildView instanceof RecyclerView)) {
182+
if (cp.allowOverScroll()) {
183+
if (mVelocityY >= OVER_SCROLL_MIN_VX) {
184+
if (ScrollingUtil.isViewToTop(mChildView, mTouchSlop)) {
185+
cp.getAnimProcessor().animOverScrollTop(mVelocityY, cur_delay_times);
186+
mVelocityY = 0;
187+
cur_delay_times = ALL_DELAY_TIMES;
188+
}
189+
} else if (mVelocityY <= -OVER_SCROLL_MIN_VX) {
190+
if (ScrollingUtil.isViewTopBottom(mChildView, mTouchSlop)) {
191+
cp.getAnimProcessor().animOverScrollBottom(mVelocityY, cur_delay_times);
192+
mVelocityY = 0;
193+
cur_delay_times = ALL_DELAY_TIMES;
194+
}
195+
}
196+
}
183197

198+
/*if (!(mChildView instanceof AbsListView || mChildView instanceof RecyclerView)) {
199+
//判断顶部越界
184200
if (cp.allowOverScroll() && mVelocityY >= OVER_SCROLL_MIN_VX && (mChildView != null && Math.abs(mChildView.getScrollY()) <= 2 * mTouchSlop)) {
185201
cp.getAnimProcessor().animOverScrollTop(mVelocityY, cur_delay_times);
186202
mVelocityY = 0;
187203
cur_delay_times = ALL_DELAY_TIMES;
188204
}
189-
205+
//判断底部越界
190206
if (cp.allowOverScroll() && mVelocityY <= -OVER_SCROLL_MIN_VX && mChildView != null) {
191207
if (mChildView instanceof WebView) {
192208
WebView webview = (WebView) (mChildView);
@@ -209,7 +225,7 @@ public void handleMessage(Message msg) {
209225
cur_delay_times = ALL_DELAY_TIMES;
210226
}
211227
}
212-
}
228+
}*/
213229

214230
if (cur_delay_times < ALL_DELAY_TIMES)
215231
mHandler.sendEmptyMessageDelayed(MSG_CONTINUE_COMPUTE_SCROLL, 10);

library/src/main/java/com/lcodecore/tkrefreshlayout/TwinklingRefreshLayout.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.support.v7.widget.RecyclerView;
56
import android.util.AttributeSet;
67
import android.view.MotionEvent;
78
import android.view.View;
@@ -105,6 +106,9 @@ public TwinklingRefreshLayout(Context context, AttributeSet attrs, int defStyleA
105106
a.recycle();
106107

107108
cp = new CoProcessor();
109+
110+
addHeader();
111+
addFooter();
108112
}
109113

110114
private void init() {
@@ -114,49 +118,48 @@ private void init() {
114118
setPullListener(new TwinklingPullListener());
115119
}
116120

117-
@Override
118-
protected void onAttachedToWindow() {
119-
super.onAttachedToWindow();
120-
121-
//添加头部
122-
if (mHeadLayout == null) {
123-
FrameLayout headViewLayout = new FrameLayout(getContext());
124-
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
125-
layoutParams.addRule(ALIGN_PARENT_TOP);
126-
layoutParams.addRule(CENTER_VERTICAL);
121+
private void addHeader(){
122+
FrameLayout headViewLayout = new FrameLayout(getContext());
123+
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
124+
layoutParams.addRule(ALIGN_PARENT_TOP);
125+
layoutParams.addRule(CENTER_VERTICAL);
127126

128-
FrameLayout extraHeadLayout = new FrameLayout(getContext());
129-
extraHeadLayout.setId(R.id.ex_header);
130-
LayoutParams layoutParams2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
127+
FrameLayout extraHeadLayout = new FrameLayout(getContext());
128+
extraHeadLayout.setId(R.id.ex_header);
129+
LayoutParams layoutParams2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
131130

132-
this.addView(extraHeadLayout, layoutParams2);
133-
this.addView(headViewLayout, layoutParams);
131+
this.addView(extraHeadLayout, layoutParams2);
132+
this.addView(headViewLayout, layoutParams);
134133

135-
mExtraHeadLayout = extraHeadLayout;
136-
mHeadLayout = headViewLayout;
134+
mExtraHeadLayout = extraHeadLayout;
135+
mHeadLayout = headViewLayout;
137136

138-
if (mHeadView == null) setHeaderView(new GoogleDotView(getContext()));
139-
}
137+
if (mHeadView == null) setHeaderView(new GoogleDotView(getContext()));
138+
}
140139

141-
//添加底部
142-
if (mBottomLayout == null) {
143-
FrameLayout bottomViewLayout = new FrameLayout(getContext());
144-
LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
145-
layoutParams2.addRule(ALIGN_PARENT_BOTTOM);
146-
layoutParams2.addRule(CENTER_VERTICAL);
147-
bottomViewLayout.setLayoutParams(layoutParams2);
140+
private void addFooter(){
141+
FrameLayout bottomViewLayout = new FrameLayout(getContext());
142+
LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
143+
layoutParams2.addRule(ALIGN_PARENT_BOTTOM);
144+
layoutParams2.addRule(CENTER_VERTICAL);
145+
bottomViewLayout.setLayoutParams(layoutParams2);
148146

149-
mBottomLayout = bottomViewLayout;
150-
this.addView(mBottomLayout);
147+
mBottomLayout = bottomViewLayout;
148+
this.addView(mBottomLayout);
151149

152-
if (mBottomView == null) {
153-
BottomProgressView mProgressView = new BottomProgressView(getContext());
154-
setBottomView(mProgressView);
155-
}
150+
if (mBottomView == null) {
151+
BottomProgressView mProgressView = new BottomProgressView(getContext());
152+
setBottomView(mProgressView);
156153
}
154+
}
157155

156+
@Override
157+
protected void onFinishInflate() {
158+
super.onFinishInflate();
158159
//获得子控件
159-
mChildView = getChildAt(0);
160+
//onAttachedToWindow方法中mChildView始终是第0个child,把header、footer放到构造函数中,mChildView最后被inflate
161+
//TODO 可能引入新问题,fixedHeader显示异常
162+
mChildView = getChildAt(3);
160163

161164
cp.init();
162165
}

0 commit comments

Comments
 (0)