Skip to content

Commit 3ed6f46

Browse files
committed
修复悬停控件悬停时onLayout消失BUG
1 parent 154acfd commit 3ed6f46

File tree

6 files changed

+75
-17
lines changed

6 files changed

+75
-17
lines changed

.idea/misc.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.

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5-
import android.view.Gravity;
65
import android.view.View;
6+
import android.widget.ArrayAdapter;
77
import android.widget.TextView;
88
import android.widget.Toast;
99

1010
import com.wkp.sticklayout_lib.widget.StickLayout;
1111

12+
import java.util.ArrayList;
13+
import java.util.List;
14+
1215
public class MainActivity extends AppCompatActivity {
1316

1417
private StickLayout mSl;
@@ -17,6 +20,15 @@ public class MainActivity extends AppCompatActivity {
1720
private View mTv7;
1821
private View mTv4;
1922
private int currentPosition = -1;
23+
private NoScrollListView mLv;
24+
private List<String> mDatas = new ArrayList<>();
25+
private ArrayAdapter<String> mAdapter;
26+
27+
{
28+
for (int i = 0; i < 20; i++) {
29+
mDatas.add("" + i);
30+
}
31+
}
2032

2133
@Override
2234
protected void onCreate(Bundle savedInstanceState) {
@@ -27,6 +39,9 @@ protected void onCreate(Bundle savedInstanceState) {
2739
mTv3 = findViewById(R.id.tv3);
2840
mTv4 = findViewById(R.id.tv4);
2941
mTv7 = findViewById(R.id.tv7);
42+
mLv = findViewById(R.id.lv);
43+
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mDatas);
44+
mLv.setAdapter(mAdapter);
3045
// mSl.setStickView(findViewById(R.id.tv2)); //设置粘性控件
3146
// mSl.setStickView(findViewById(R.id.tv3));
3247
// mSl.canScrollToEndViewTop(true); //设置是否开启最后控件滑动到顶部
@@ -35,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {
3550
@Override
3651
public void onScrollChange(StickLayout v, View currentView, int position, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
3752
//直到当前控件改变在做事情
38-
if (currentPosition != position) {
53+
if (currentPosition != position && position <= 7) {
3954
Toast.makeText(v.getContext(), ((TextView) currentView).getText().toString(), Toast.LENGTH_SHORT).show();
4055
currentPosition = position;
4156
}
@@ -44,11 +59,13 @@ public void onScrollChange(StickLayout v, View currentView, int position, int sc
4459
}
4560

4661
public void addView(View view) {
47-
TextView textView = new TextView(view.getContext());
48-
textView.setGravity(Gravity.CENTER);
49-
textView.setPadding(10, 10, 10, 10);
50-
textView.setText("新条目");
51-
mSl.addView(textView, 0);
62+
// TextView textView = new TextView(view.getContext());
63+
// textView.setGravity(Gravity.CENTER);
64+
// textView.setPadding(10, 10, 10, 10);
65+
// textView.setText("新条目");
66+
// mSl.addView(textView, 0);
67+
mDatas.add("新条目");
68+
mAdapter.notifyDataSetChanged();
5269
}
5370

5471
public void scrollTo2(View view) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.wkp.sticklayout;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.ListView;
6+
7+
/**
8+
* Created by user on 2018/2/5.
9+
*/
10+
11+
public class NoScrollListView extends ListView {
12+
public NoScrollListView(Context context) {
13+
super(context);
14+
}
15+
16+
public NoScrollListView(Context context, AttributeSet attrs) {
17+
super(context, attrs);
18+
}
19+
20+
public NoScrollListView(Context context, AttributeSet attrs, int defStyle) {
21+
super(context, attrs, defStyle);
22+
}
23+
24+
@Override
25+
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
26+
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
27+
super.onMeasure(widthMeasureSpec, expandSpec);
28+
}
29+
30+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
xmlns:tools="http://schemas.android.com/tools"
66
android:layout_width="match_parent"
7-
android:layout_height="match_parent"
7+
android:layout_height="wrap_content"
88
android:orientation="vertical">
99

1010
<TextView
@@ -16,8 +16,8 @@
1616
android:layout_width="match_parent"
1717
android:layout_height="wrap_content"/>
1818

19+
<!--app:wkp_canScrollToEndViewTop="true"-->
1920
<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">
@@ -120,6 +120,13 @@
120120
android:layout_width="match_parent"
121121
android:layout_height="200dp"/>
122122

123+
<com.wkp.sticklayout.NoScrollListView
124+
android:id="@+id/lv"
125+
android:layout_width="match_parent"
126+
android:layout_height="match_parent">
127+
128+
</com.wkp.sticklayout.NoScrollListView>
129+
123130
</com.wkp.sticklayout_lib.widget.StickLayout>
124131

125132
</LinearLayout>

sticklayout-lib/build.gradle

Lines changed: 3 additions & 4 deletions
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 4
15-
versionName "1.0.4"
14+
versionCode 5
15+
versionName "1.0.5"
1616

1717
}
1818

@@ -28,14 +28,13 @@ android {
2828
dependencies {
2929
implementation fileTree(include: ['*.jar'], dir: 'libs')
3030
implementation 'com.android.support:appcompat-v7:26.1.0'
31-
implementation 'com.android.support:design:26.1.0'
3231
}
3332

3433
publish {
3534
userOrg = 'wkp'
3635
groupId = 'com.wkp'
3736
artifactId = 'StickLayout'
38-
publishVersion = '1.0.4'
37+
publishVersion = '1.0.5'
3938
desc = 'a library to create a stickable layout'
4039
website = 'https://github.com/wkp111/StickLayout'
4140
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class StickLayout extends FrameLayout implements NestedScrollingParent,
7373
private SparseArray<LayoutParams> mLayoutParamsArray = new SparseArray<>();
7474
private int mChildCount;
7575
private boolean mScrollToEnd;
76+
private int mCurrentTop;
7677

7778
/**
7879
* Interface definition for a callback to be invoked when the scroll
@@ -659,6 +660,7 @@ public void setSmoothScrollingEnabled(boolean smoothScrollingEnabled) {
659660
@Override
660661
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
661662
super.onScrollChanged(l, t, oldl, oldt);
663+
mCurrentTop = t;
662664
if (mViewPair != null) {
663665
View view = mViewPair.second;
664666
if (t >= mCrisisHeight) {
@@ -1880,7 +1882,12 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
18801882
super.onLayout(changed, l, t, r, b);
18811883
if (mViewPair != null) {
18821884
View first = mViewPair.first;
1883-
mViewPair.second.layout(first.getLeft(), first.getTop(), first.getRight(), first.getBottom());
1885+
View view = mViewPair.second;
1886+
if (mCurrentTop >= mCrisisHeight && mCrisisHeight != 0) {
1887+
view.layout(first.getLeft(), mCurrentTop, first.getRight(), mCurrentTop + view.getHeight());
1888+
} else {
1889+
view.layout(first.getLeft(), first.getTop(), first.getRight(), first.getBottom());
1890+
}
18841891
LinearLayout linearLayout = (LinearLayout) ((FrameLayout) super.getChildAt(0)).getChildAt(0);
18851892
int index = linearLayout.indexOfChild(first);
18861893
mCrisisHeight = 0;
@@ -1905,15 +1912,13 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
19051912
final int childHeight = (getChildCount() > 0) ? super.getChildAt(0).getMeasuredHeight() : 0;
19061913
final int scrollRange = Math.max(0,
19071914
childHeight - (b - t - getPaddingBottom() - getPaddingTop()));
1908-
19091915
// Don't forget to clamp
19101916
if (getScrollY() > scrollRange) {
19111917
scrollTo(getScrollX(), scrollRange);
19121918
} else if (getScrollY() < 0) {
19131919
scrollTo(getScrollX(), 0);
19141920
}
19151921
}
1916-
19171922
// Calling this with the present values causes it to re-claim them
19181923
scrollTo(getScrollX(), getScrollY());
19191924
mIsLaidOut = true;

0 commit comments

Comments
 (0)