Android:UI控件scrollView、TabHost
UI控件scrollView:
容器内最外层只能有一个布局。
UI控件TabHost:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public
class
MainActivity
extends
TabActivity
//继承TabActivity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
/**
* 自定义创建TabHost方法
*/
//TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
//tabHost.setup();
tabHost.addTab(tabHost.newTabSpec(
""
).setIndicator(
"title1"
,
getResources().getDrawable(R.drawable.ic_launcher))
//设置tab背景图
.setContent(
new
Intent(
this
, Second.
class
)));
tabHost.addTab(tabHost.newTabSpec(
""
).setIndicator(
"title1"
)
.setContent(
new
Intent(
this
, Second.
class
)));
tabHost.addTab(tabHost.newTabSpec(
""
).setIndicator(
"title1"
)
.setContent(
new
Intent(
this
, Second.
class
)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//Activity的启动模式
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
|
scrollView相关:
-
监听scrollView滚动到底部:
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
/**
* ******************增加滚动监听接口*****************************
*/
private
OnScrollListener onScrollListener;
/**
* 设置滚动接口
*
* @param onScrollListener
*/
public
void
setOnScrollListener(OnScrollListener onScrollListener)
{
this
.onScrollListener = onScrollListener;
}
@Override
protected
void
onScrollChanged(
int
x,
int
y,
int
oldx,
int
oldy)
{
super
.onScrollChanged(x, y, oldx, oldy);
if
(onScrollListener !=
null
)
{
onScrollListener.onScroll(x, y, oldx, oldy);
//是否滚动到底部
if
(getScrollY() + getHeight() >= computeVerticalScrollRange() )
{
onScrollListener.onScrollBottom(x, y, oldx, oldy);
}
}
}
/**
* 滚动的回调接口
*/
public
interface
OnScrollListener
{
/**
* 回调方法, 返回ScrollView滑动的各方向距离
*/
public
void
onScroll(
int
x,
int
y,
int
oldx,
int
oldy);
public
void
onScrollBottom(
int
x,
int
y,
int
oldx,
int
oldy);
}
|
2.使scrollview内的子控件充满整个屏幕,需要设置android:fillViewport="true"属性。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<ScrollView
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:fillViewport=
"true"
>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<!-- 滚动框内布局 -->
<LinearLayout
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
|
3.scrollTo() 是直接指定滚动条的位置, 但是由于这个动作不是单纯关于 ScrollView 而已, 还要根据 ScrollView 里面包含的View 的实际信息. 所以这动作必须在页面加载完成以后才能执行.
4.网上说的方法乱七八糟,能用的就是自己算高度,其实sdk-9中,ScrollView已经加入了一个方法,能监听到是否已经不能滚动,稍加处理,就可以监听是否滑到底部了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import
android.content.Context;
import
android.util.AttributeSet;
import
android.widget.ScrollView;
public
class
BottomScrollView
extends
ScrollView {
private
OnScrollToBottomListener onScrollToBottom;
public
BottomScrollView(Context context, AttributeSet attrs) {
super
(context, attrs);
}
public
BottomScrollView(Context context) {
super
(context);
}
@Override
protected
void
onOverScrolled(
int
scrollX,
int
scrollY,
boolean
clampedX,
boolean
clampedY) {
super
.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
if
(scrollY !=
0
&&
null
!= onScrollToBottom){
onScrollToBottom.onScrollBottomListener(clampedY);
}
}
public
void
setOnScrollToBottomLintener(OnScrollToBottomListener listener){
onScrollToBottom = listener;
}
public
interface
OnScrollToBottomListener{
public
void
onScrollBottomListener(
boolean
isBottom);
}
}
|