Skip to content

Commit b737647

Browse files
cco3Android Git Automerger
authored andcommitted
am 76616b1: Merge "Scolling using arrow keys with padding"
* commit '76616b138eb68a77a838a93fdf2f0322e982f706': Scolling using arrow keys with padding
2 parents 19653c6 + 76616b1 commit b737647

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

core/java/android/widget/ScrollView.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ public boolean fullScroll(int direction) {
832832
int count = getChildCount();
833833
if (count > 0) {
834834
View view = getChildAt(count - 1);
835-
mTempRect.bottom = view.getBottom();
835+
mTempRect.bottom = view.getBottom() + mPaddingBottom;
836836
mTempRect.top = mTempRect.bottom - height;
837837
}
838838
}
@@ -912,9 +912,7 @@ public boolean arrowScroll(int direction) {
912912
} else if (direction == View.FOCUS_DOWN) {
913913
if (getChildCount() > 0) {
914914
int daBottom = getChildAt(0).getBottom();
915-
916-
int screenBottom = getScrollY() + getHeight();
917-
915+
int screenBottom = getScrollY() + getHeight() - mPaddingBottom;
918916
if (daBottom - screenBottom < maxJump) {
919917
scrollDelta = daBottom - screenBottom;
920918
}

core/tests/coretests/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@
417417
</intent-filter>
418418
</activity>
419419

420+
<activity android:name="android.widget.scroll.arrowscroll.MultiPageTextWithPadding" android:label="arrowscrollMultiPageTextWithPadding">
421+
<intent-filter>
422+
<action android:name="android.intent.action.MAIN" />
423+
<category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
424+
</intent-filter>
425+
</activity>
426+
420427
<activity android:name="android.view.Include" android:label="IncludeTag">
421428
<intent-filter>
422429
<action android:name="android.intent.action.MAIN" />

core/tests/coretests/src/android/util/ScrollViewScenario.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private interface ViewFactory {
6161

6262
/**
6363
* Partially implement ViewFactory given a height ratio.
64+
* A negative height ratio means that WRAP_CONTENT will be used as height
6465
*/
6566
private static abstract class ViewFactoryBase implements ViewFactory {
6667

@@ -87,6 +88,9 @@ public static class Params {
8788

8889
List<ViewFactory> mViewFactories = Lists.newArrayList();
8990

91+
int mTopPadding = 0;
92+
int mBottomPadding = 0;
93+
9094
/**
9195
* Add a text view.
9296
* @param text The text of the text view.
@@ -186,6 +190,13 @@ public View create(Context context) {
186190
});
187191
return this;
188192
}
193+
194+
public Params addPaddingToScrollView(int topPadding, int bottomPadding) {
195+
mTopPadding = topPadding;
196+
mBottomPadding = bottomPadding;
197+
198+
return this;
199+
}
189200
}
190201

191202
/**
@@ -239,13 +250,17 @@ protected void onCreate(Bundle savedInstanceState) {
239250

240251
// create views specified by params
241252
for (ViewFactory viewFactory : params.mViewFactories) {
253+
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
254+
if (viewFactory.getHeightRatio() >= 0) {
255+
height = (int) (viewFactory.getHeightRatio() * screenHeight);
256+
}
242257
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
243-
ViewGroup.LayoutParams.MATCH_PARENT,
244-
(int) (viewFactory.getHeightRatio() * screenHeight));
258+
ViewGroup.LayoutParams.MATCH_PARENT, height);
245259
mLinearLayout.addView(viewFactory.create(this), lp);
246260
}
247261

248262
mScrollView = createScrollView();
263+
mScrollView.setPadding(0, params.mTopPadding, 0, params.mBottomPadding);
249264
mScrollView.addView(mLinearLayout, new ViewGroup.LayoutParams(
250265
ViewGroup.LayoutParams.MATCH_PARENT,
251266
ViewGroup.LayoutParams.MATCH_PARENT));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2011 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.widget.scroll.arrowscroll;
18+
19+
import android.util.ScrollViewScenario;
20+
21+
/**
22+
* One TextView with a text covering several pages. Padding is added
23+
* above and below the ScrollView.
24+
*/
25+
public class MultiPageTextWithPadding extends ScrollViewScenario {
26+
27+
@Override
28+
protected void init(Params params) {
29+
30+
String text = "This is a long text.";
31+
String longText = "First text.";
32+
for (int i = 0; i < 300; i++) {
33+
longText = longText + " " + text;
34+
}
35+
longText = longText + " Last text.";
36+
params.addTextView(longText, -1.0f).addPaddingToScrollView(50, 50);
37+
}
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (C) 2011 Sony Ericsson Mobile Communications AB.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.widget.scroll.arrowscroll;
18+
19+
import android.widget.scroll.arrowscroll.MultiPageTextWithPadding;
20+
import android.test.ActivityInstrumentationTestCase;
21+
import android.test.suitebuilder.annotation.LargeTest;
22+
import android.test.suitebuilder.annotation.MediumTest;
23+
import android.view.KeyEvent;
24+
import android.widget.TextView;
25+
import android.widget.ScrollView;
26+
27+
public class MultiPageTextWithPaddingTest extends
28+
ActivityInstrumentationTestCase<MultiPageTextWithPadding> {
29+
30+
private ScrollView mScrollView;
31+
32+
private TextView mTextView;
33+
34+
public MultiPageTextWithPaddingTest() {
35+
super("com.android.frameworks.coretests", MultiPageTextWithPadding.class);
36+
}
37+
38+
@Override
39+
protected void setUp() throws Exception {
40+
super.setUp();
41+
42+
mScrollView = getActivity().getScrollView();
43+
mTextView = getActivity().getContentChildAt(0);
44+
}
45+
46+
@MediumTest
47+
public void testPreconditions() {
48+
assertTrue("text should not fit on screen",
49+
mTextView.getHeight() > mScrollView.getHeight());
50+
}
51+
52+
@LargeTest
53+
public void testScrollDownToBottom() throws Exception {
54+
// Calculate the number of arrow scrolls needed to reach the bottom
55+
int scrollsNeeded = (int)Math.ceil(Math.max(0.0f,
56+
(mTextView.getHeight() - mScrollView.getHeight()))
57+
/ mScrollView.getMaxScrollAmount());
58+
for (int i = 0; i < scrollsNeeded; i++) {
59+
sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
60+
}
61+
62+
assertEquals(
63+
"should be fully scrolled to bottom",
64+
getActivity().getLinearLayout().getHeight()
65+
- (mScrollView.getHeight() - mScrollView.getPaddingTop() - mScrollView
66+
.getPaddingBottom()), mScrollView.getScrollY());
67+
}
68+
}

0 commit comments

Comments
 (0)