【Android UI】顶部or底部菜单的循环滑动效果一

摘要: 实现了分页的滑动效果,做的demo流畅运行注:貌似支持的样式(控件)有一定的限制,我试过短信的listview页面,暂无法实现滑动效果java文件:MainActivity.java、Activity1.java、Activity2.java、Activity3.java、Activity4.jav…阅读全文

实现了分页的滑动效果,做的demo流畅运行

注:貌似支持的样式(控件)有一定的限制,我试过短信的listview页面,暂无法实现滑动效果

java文件:MainActivity.java、Activity1.java、Activity2.java、Activity3.java、Activity4.java

MainActivity.java

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	package com.example.tabhostmove;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


public class MainActivity extends TabActivity {
private TabHost tabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}


private void init() {
// TODO Auto-generated method stub

tabHost = getTabHost();
// 页面1
TabSpec spec1 = tabHost.newTabSpec("1");
spec1.setIndicator("1", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent1 = new Intent(this, Activity1.class);
spec1.setContent(intent1);

// 页面2
TabSpec spec2 = tabHost.newTabSpec("2");
spec2.setIndicator("2", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent2 = new Intent(this, Activity2.class);
spec2.setContent(intent2);

// 页面3
TabSpec spec3 = tabHost.newTabSpec("3");
spec3.setIndicator("3", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent3 = new Intent(this, Activity3.class);
spec3.setContent(intent3);

// 页面4
TabSpec spec4 = tabHost.newTabSpec("4");
spec4.setIndicator("4", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent4 = new Intent(this, Activity4.class);
spec4.setContent(intent4);

tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);

}



private GestureDetector detector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if ((e2.getRawX() - e1.getRawX()) > 80) {
showNext();
return true;
}

if ((e1.getRawX() - e2.getRawX()) > 80) {
showPre();
return true;
}
return super.onFling(e1, e2, velocityX, velocityY);
}

});


@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
return super.onTouchEvent(event);
}

/**
* 当前页面索引
*/
int i = 0;

/**
* 显示下一个页面
*/
protected void showNext() {
// 三元表达式控制3个页面的循环.
//tabHost.setCurrentTab(i = i == 3 ? i = 0 : ++i);
//Log.i("kennet", i + "");
//四个页面的下一个循环
switch(i)
{
case 0:
i++;
tabHost.setCurrentTab(i);
break;
case 1:
i++;
tabHost.setCurrentTab(i);
break;
case 2:
i++;
tabHost.setCurrentTab(i);
break;
case 3:
i=0;
tabHost.setCurrentTab(i);
break;

}
}

/**
* 显示前一个页面
*/
protected void showPre() {
// 三元表达式控制3个页面的循环.
//tabHost.setCurrentTab(i = i == 0 ? i = 3 : --i);
//四个页面的上一个循环
switch(i)
{
case 0:
i=3;
tabHost.setCurrentTab(i);
break;
case 1:
i--;
tabHost.setCurrentTab(i);
break;
case 2:
i--;
tabHost.setCurrentTab(i);
break;
case 3:
i--;
tabHost.setCurrentTab(i);
break;

}
}











@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

xml布局文件:activity_main.xml、activit1.xml、activit2.xml、activit3.xml、activit4.xml

activity_main.xml

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
	<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>

</TabHost>

注:activity1、2、3、4是测试的页面,随便建几个即可,别忘了在AndroidManifest.xml里注册页面的活动

实现效果: