Skip to content

Commit 4bc972b

Browse files
committed
处理点击事件不执行的问题
1 parent e79deef commit 4bc972b

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ android {
2323

2424
dependencies {
2525
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
26-
compile fileTree(include: ['*.jar'], dir: 'libs')
26+
implementation fileTree(include: ['*.jar'], dir: 'libs')
2727
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2828
exclude group: 'com.android.support', module: 'support-annotations'
2929
})

app/src/main/java/com/zhengsr/viewpagerhelper/activity/loop/CircleIndicatorActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import android.graphics.Color;
44
import android.support.v7.app.AppCompatActivity;
55
import android.os.Bundle;
6+
import android.util.Log;
67
import android.view.View;
8+
import android.widget.Button;
79
import android.widget.ImageView;
810
import android.widget.Toast;
911

@@ -124,17 +126,21 @@ private void showBanner(BannerViewPager bannerViewPager, CircleIndicator indicat
124126
bannerViewPager.setPageListener(R.layout.loop_layout, mDatas, new PageHelperListener<TestBean>() {
125127
@Override
126128
public void bindView(View view, final TestBean data, int position) {
127-
setText(view, R.id.loop_text, data.msg);
129+
setText(view, R.id.loop_text, data.msg);
130+
128131

129132
//注册子控件事件
130133
//addChildrenClick(view,R.id.item_text,position);
131134

135+
132136
ImageView imageView = view.findViewById(R.id.loop_icon);
133137
GlideApp.with(view)
134138
.load(data.resId)
135139
.into(imageView);
136140
}
137141

142+
143+
138144
@Override
139145
public void onItemClick(View view, TestBean data, int position) {
140146
super.onItemClick(view, data, position);

app/src/main/res/layout/loop_layout.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
android:layout_height="30dp">
2121
<TextView
2222
android:id="@+id/loop_text"
23-
android:layout_width="match_parent"
23+
android:layout_width="wrap_content"
2424
android:layout_height="match_parent"
2525
android:layout_marginLeft="8dp"
2626
android:textSize="16sp"
2727
android:gravity="center_vertical"
28+
android:clickable="true"
2829
android:textColor="@color/white"/>
30+
31+
2932
</FrameLayout>
3033

3134

viewpagerlib/src/main/java/com/zhengsr/viewpagerlib/callback/PageHelperListener.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
package com.zhengsr.viewpagerlib.callback;
22

33
import android.graphics.Bitmap;
4+
import android.util.Log;
5+
import android.util.SparseArray;
6+
import android.view.MotionEvent;
47
import android.view.View;
58
import android.widget.ImageView;
69
import android.widget.TextView;
710

11+
import java.util.ArrayList;
12+
import java.util.List;
13+
814
/**
915
* Created by Administrator on 2017/11/9.
1016
*/
1117

1218
public abstract class PageHelperListener<T> {
19+
private static final String TAG = "PageHelperListener";
20+
21+
private List<Object> mDatas;
22+
public PageHelperListener() {
23+
mDatas = new ArrayList<>();
24+
}
1325

26+
public void setDatas(List<Object> datas){
27+
mDatas.addAll(datas);
28+
}
1429

1530
/**
1631
* 拿到数据
@@ -25,14 +40,14 @@ public abstract class PageHelperListener<T> {
2540
* @param childView
2641
* @param position
2742
*/
28-
public void onItemChildClick(View childView,int position){}
43+
public void onItemChildClick(View childView, T data,int position){}
2944

3045
/**
3146
* 子控件长按事件
3247
* @param childView
3348
* @param position
3449
*/
35-
public boolean onItemChildLongClick(View childView,int position){
50+
public boolean onItemChildLongClick(View childView,T data,int position){
3651
return true;
3752
}
3853

@@ -47,7 +62,7 @@ public PageHelperListener addChildrenClick(View view, int viewId, final int posi
4762
child.setOnClickListener(new View.OnClickListener() {
4863
@Override
4964
public void onClick(View view) {
50-
onItemChildClick(child,position);
65+
onItemChildClick(child, (T) mDatas.get(position),position);
5166
}
5267
});
5368
}
@@ -64,14 +79,16 @@ public PageHelperListener addChildrenLongClick(View view, int viewId, final int
6479
child.setOnLongClickListener(new View.OnLongClickListener() {
6580
@Override
6681
public boolean onLongClick(View v) {
67-
return onItemChildLongClick(child,position);
82+
return onItemChildLongClick(child, (T) mDatas.get(position),position);
6883
}
6984
});
7085
}
7186
return this;
7287
}
7388

7489

90+
91+
7592
/**
7693
* 常用模板
7794
*/

viewpagerlib/src/main/java/com/zhengsr/viewpagerlib/view/BannerViewPager.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.util.AttributeSet;
1313
import android.util.DisplayMetrics;
1414
import android.util.Log;
15+
import android.util.SparseArray;
1516
import android.view.LayoutInflater;
1617
import android.view.MotionEvent;
1718
import android.view.View;
@@ -39,7 +40,7 @@
3940
* Created by Administrator on 2017/11/9.
4041
*/
4142

42-
public class BannerViewPager extends ViewPager implements View.OnTouchListener {
43+
public class BannerViewPager extends ViewPager {
4344
/**
4445
* const
4546
*/
@@ -124,7 +125,6 @@ public BannerViewPager(Context context, AttributeSet attrs) {
124125

125126
ta.recycle();
126127
mInflater = LayoutInflater.from(context);
127-
setOnTouchListener(this);
128128
ViewPagerHelperUtils.initSwitchTime(getContext(), this, mSmoothTime);
129129

130130
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
@@ -261,6 +261,7 @@ public <T> void setPageListener(int layoutId, final List<T> datas, final PageHel
261261
}
262262
mDatas.clear();
263263
mDatas.addAll(datas);
264+
listener.setDatas(mDatas);
264265

265266
CusViewPagerAdapter adapter = new CusViewPagerAdapter<T>(datas, layoutId, listener);
266267
setAdapter(adapter);
@@ -276,6 +277,7 @@ public <T> void setPageListener(int layoutId, final List<T> datas, final PageHel
276277

277278

278279

280+
279281
}
280282

281283

@@ -295,26 +297,6 @@ private void chooseIndicator(int count, View indicator) {
295297
}
296298

297299

298-
/**
299-
* 当有触摸时停止
300-
*/
301-
@Override
302-
public boolean onTouch(View view, MotionEvent motionEvent) {
303-
mHandler.removeMessages(LOOP_MSG);
304-
switch (motionEvent.getAction()) {
305-
case MotionEvent.ACTION_DOWN:
306-
break;
307-
case MotionEvent.ACTION_UP:
308-
if (isAutoLoop) {
309-
mHandler.sendEmptyMessageDelayed(LOOP_MSG, mLoopTime);
310-
}
311-
break;
312-
313-
default:
314-
break;
315-
}
316-
return false;
317-
}
318300

319301
/**
320302
* 手动停止
@@ -427,10 +409,9 @@ public boolean onTouch(View v, MotionEvent event) {
427409
case MotionEvent.ACTION_DOWN:
428410
mLastTime = System.currentTimeMillis();
429411
break;
430-
431412
case MotionEvent.ACTION_UP:
432413
long time = System.currentTimeMillis() - mLastTime;
433-
if (time > 200){
414+
if (time < 200){
434415
if (listener != null && mDatas.size() > 0) {
435416
listener.onItemClick(view,mDatas.get(position),position);
436417
}
@@ -443,6 +424,8 @@ public boolean onTouch(View v, MotionEvent event) {
443424
return false;
444425
}
445426
});
427+
428+
446429
}
447430
}
448431

0 commit comments

Comments
 (0)