Skip to content

Commit 154acfd

Browse files
committed
新增滑动改变监听
1 parent d5aee49 commit 154acfd

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

StickLayout2.gif

705 KB
Loading

app/src/main/java/com/wkp/sticklayout/MainActivity.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class MainActivity extends AppCompatActivity {
1616
private View mTv3;
1717
private View mTv7;
1818
private View mTv4;
19+
private int currentPosition = -1;
1920

2021
@Override
2122
protected void onCreate(Bundle savedInstanceState) {
@@ -26,24 +27,32 @@ protected void onCreate(Bundle savedInstanceState) {
2627
mTv3 = findViewById(R.id.tv3);
2728
mTv4 = findViewById(R.id.tv4);
2829
mTv7 = findViewById(R.id.tv7);
29-
// mSl.setStickView(findViewById(R.id.tv2));
30+
// mSl.setStickView(findViewById(R.id.tv2)); //设置粘性控件
3031
// mSl.setStickView(findViewById(R.id.tv3));
31-
// mSl.canScrollToEndViewTop(true);
32+
// mSl.canScrollToEndViewTop(true); //设置是否开启最后控件滑动到顶部
33+
//设置滑动改变监听(一滑动就会有回调)
34+
mSl.setOnScrollChangeListener(new StickLayout.OnScrollChangeListener() {
35+
@Override
36+
public void onScrollChange(StickLayout v, View currentView, int position, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
37+
//直到当前控件改变在做事情
38+
if (currentPosition != position) {
39+
Toast.makeText(v.getContext(), ((TextView) currentView).getText().toString(), Toast.LENGTH_SHORT).show();
40+
currentPosition = position;
41+
}
42+
}
43+
});
3244
}
3345

3446
public void addView(View view) {
3547
TextView textView = new TextView(view.getContext());
3648
textView.setGravity(Gravity.CENTER);
37-
textView.setPadding(10,10,10,10);
49+
textView.setPadding(10, 10, 10, 10);
3850
textView.setText("新条目");
39-
mSl.addView(textView,0);
51+
mSl.addView(textView, 0);
4052
}
4153

42-
public void click(View view) {
43-
Toast.makeText(this, "第1行", Toast.LENGTH_SHORT).show();
44-
}
45-
46-
public void scrollTo(View view) {
54+
public void scrollTo2(View view) {
55+
//滑动到指定子控件
4756
mSl.scrollToView(mTv2);
4857
}
4958

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
android:layout_width="match_parent"
1717
android:layout_height="wrap_content"/>
1818

19-
<!--app:wkp_canScrollToEndViewTop="true"-->
2019
<com.wkp.sticklayout_lib.widget.StickLayout
20+
app:wkp_canScrollToEndViewTop="true"
2121
android:id="@+id/sl"
2222
android:layout_width="match_parent"
2323
android:layout_height="wrap_content">
2424

2525
<TextView
26-
android:onClick="click"
2726
android:id="@+id/tv1"
2827
android:text="第1行"
2928
android:gravity="center"
@@ -37,7 +36,7 @@
3736
android:layout_height="40dp">
3837

3938
<TextView
40-
android:onClick="scrollTo"
39+
android:onClick="scrollTo2"
4140
android:background="@android:color/holo_blue_light"
4241
android:text="NUM2"
4342
android:gravity="center"

sticklayout-lib/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
// applicationId "com.wkp.sticklayout_lib"
1212
minSdkVersion 15
1313
targetSdkVersion 26
14-
versionCode 3
15-
versionName "1.0.3"
14+
versionCode 4
15+
versionName "1.0.4"
1616

1717
}
1818

@@ -35,7 +35,7 @@ publish {
3535
userOrg = 'wkp'
3636
groupId = 'com.wkp'
3737
artifactId = 'StickLayout'
38-
publishVersion = '1.0.3'
38+
publishVersion = '1.0.4'
3939
desc = 'a library to create a stickable layout'
4040
website = 'https://github.com/wkp111/StickLayout'
4141
}

sticklayout-lib/src/main/java/com/wkp/sticklayout_lib/widget/StickLayout.java

+47-3
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ public interface OnScrollChangeListener {
8787
* Called when the scroll position of a view changes.
8888
*
8989
* @param v The view whose scroll position has changed.
90+
* @param currentView 当前子控件
91+
* @param position 当前子控件索引
9092
* @param scrollX Current horizontal scroll origin.
9193
* @param scrollY Current vertical scroll origin.
9294
* @param oldScrollX Previous horizontal scroll origin.
9395
* @param oldScrollY Previous vertical scroll origin.
9496
*/
95-
void onScrollChange(StickLayout v, int scrollX, int scrollY,
96-
int oldScrollX, int oldScrollY);
97+
void onScrollChange(StickLayout v, View currentView, int position, int scrollX, int scrollY, int oldScrollX, int oldScrollY);
9798
}
9899

99100
private long mLastScroll;
@@ -667,7 +668,50 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
667668
}
668669
}
669670
if (mOnScrollChangeListener != null) {
670-
mOnScrollChangeListener.onScrollChange(this, l, t, oldl, oldt);
671+
View currentView = null;
672+
int position = -1;
673+
if (mViewPair == null) {
674+
for (int i = 0; i < getChildCounter(); i++) {
675+
View view = getChildIndex(i);
676+
int top = view.getTop();
677+
int bottom = view.getBottom();
678+
if (t >= top && t < bottom) {
679+
currentView = view;
680+
position = i;
681+
break;
682+
}
683+
}
684+
}else {
685+
if (t < mCrisisHeight) {
686+
for (int i = 0; i < getChildCounter(); i++) {
687+
View view = getChildIndex(i);
688+
int top = view.getTop();
689+
int bottom = view.getBottom();
690+
if (t >= top && t < bottom) {
691+
currentView = view;
692+
position = i;
693+
break;
694+
}
695+
}
696+
}else {
697+
View first = mViewPair.first;
698+
int index = indexOfChild(first);
699+
for (int i = 0; i < getChildCounter(); i++) {
700+
if (i == index) {
701+
continue;
702+
}
703+
View view = getChildIndex(i);
704+
int top = view.getTop() - first.getHeight();
705+
int bottom = view.getBottom() - first.getHeight();
706+
if (t >= top && t < bottom) {
707+
currentView = view;
708+
position = i;
709+
break;
710+
}
711+
}
712+
}
713+
}
714+
mOnScrollChangeListener.onScrollChange(this,currentView,position, l, t, oldl, oldt);
671715
}
672716
}
673717

0 commit comments

Comments
 (0)