是否可以向现有swipe
添加Tab Menu
功能?即向左或向右滑动以在标签之间移动而不是单击标签。
我已经有一个现有的3 tab menu
并且不想要大大改变它的结构,但是很想添加滑动功能。
我该怎么办?
主菜单/ TabHost:
public class MainMenu extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec tab1 = tabHost.newTabSpec("Games");
Intent tab1Intent = new Intent(this, firstActivity.class);
tab1.setContent(tab1Intent);
tab1.setIndicator("Games");
TabSpec tab2 = tabHost.newTabSpec("Search");
Intent tab2Intent = new Intent(this, secondActivity.class);
tab2.setContent(tab2Intent);
tab2.setIndicator("Search");
TabSpec tab3 = tabHost.newTabSpec("Summary");
Intent tab3Intent = new Intent(this, thirdActivity.class);
tab3.setContent(tab3Intent);
tab3.setIndicator("Summary");
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
@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;
}
}
标签示例(中间标签):
public class secondActivity extends ActionBarActivity implements View.OnClickListener {
TextView instruction;
Button date;
Button game;
Button med;
Button att;
Button score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tab);
initialiseVars();
}
/**
* Initialises vars and sets onclick listeners for buttons
*/
public void initialiseVars() {
// setting up vars
instruction = (TextView) findViewById(R.id.tvSearchHome);
date = (Button) findViewById(R.id.btnSearchHomeDate);
game = (Button) findViewById(R.id.btnSearchHomeGame);
med = (Button) findViewById(R.id.btnSearchHomeMedValues);
att = (Button) findViewById(R.id.btnSearchHomeAttValues);
score = (Button) findViewById(R.id.btnSearchHomeScore);
// set on click listeners for the buttons
date.setOnClickListener(this);
game.setOnClickListener(this);
med.setOnClickListener(this);
att.setOnClickListener(this);
score.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchHomeDate:
Intent openDateSearch = new Intent(
"com.example.brianapp.SearchDate");
// Start activity
startActivity(openDateSearch);
break;
case R.id.btnSearchHomeGame:
Intent openGameSearch = new Intent(
"com.example.brianapp.SearchGame");
// Start activity
startActivity(openGameSearch);
break;
case R.id.btnSearchHomeMedValues:
// change this to make sure it opens the med screen
Intent openMedSearch = new Intent(
"com.example.brianapp.MeditationSearchHome");
// Start activity
startActivity(openMedSearch);
break;
case R.id.btnSearchHomeAttValues:
// change this to make sure it opens the med screen
Intent openAttSearch = new Intent(
"com.example.brianapp.AttentionSearchHome");
// Start activity
startActivity(openAttSearch);
break;
case R.id.btnSearchHomeScore:
// change this to make sure it opens the med screen
Intent openScoreSearch = new Intent(
"com.example.brianapp.SearchScore");
// Start activit
startActivity(openScoreSearch);
break;
}// switch end
}
}
答案 0 :(得分:1)
我可以为您提供以下代码段。请注意,我还没有测试过它。我只是想表明我将如何解决这个问题。
public class MainMenu extends TabActivity implements GestureDetector.OnGestureListener{
...
@Override
public boolean onFling(MotionEvent e0, MotionEvent e1, float arg2, float arg3) {
...
int current = getCurrentTab(); // TabHost.getCurrentTab()
int next;
if(e0.getRawX() - e1.getRawX() < 0 ){
//fling right
next = current + 1; //TODO check for next = n, n=number of tabs
}
else{
next = current - 1; //TODO check for next = -1
}
setCurrentTab(next); // TabHost.setCurrentTab()
}
}