我有一个tabHost,里面有4个不同的意图。我试图在标签之间移动时看到动画。我正在使用的代码部分有效:
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
FrameLayout questionsLayout = (FrameLayout)tabHost.findViewById(android.R.id.tabcontent);
questionsLayout.setAnimation(AnimationUtils.loadAnimation(tabHost.getContext(), R.anim.go_left_in));
}
然而它只动画一个动画是“inAnimation”,我也想添加一个“outAnimation”,我怎么能这样做。
顺便说一句,我正在使用此代码添加每个标签:
intent = new Intent().setClass(this, Tabs.class);
intent.putExtra("questions", rawQ);
spec = tabHost.newTabSpec("english").setIndicator(getText(R.string.ingilizce),res.getDrawable(R.drawable.ic_tabs)).setContent(intent);
tabHost.addTab(spec);
最后,我使用的是api版本8。
上次修改,整个代码:
public class Questions extends TabActivity implements OnTabChangeListener {
public static final String TAG = "Questions";
private String macAddr;
private String json;
private TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "Activity State: onCreate() " + TAG);
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
macAddr = extras.getString("macAddr");
json = extras.getString("json");
}
// Make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// parsing json data
Question[] rawQ = parseJson(json);
if (rawQ==null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Mac address could not found in database, please add it via control panel.")
.setCancelable(false)
.setNegativeButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent i = new Intent(Questions.this, AnrdoinActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
setContentView(R.layout.questions);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tabs.class);
intent.putExtra("questions", getLanguageQuestions(rawQ, 1));
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost
.newTabSpec("kyrgyz")
.setIndicator(getText(R.string.kirgizca),
res.getDrawable(R.drawable.ic_tabs))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Tabs.class);
intent.putExtra("questions", getLanguageQuestions(rawQ, 2));
spec = tabHost
.newTabSpec("turkish")
.setIndicator(getText(R.string.turkce),
res.getDrawable(R.drawable.ic_tabs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tabs.class);
intent.putExtra("questions", getLanguageQuestions(rawQ, 3));
spec = tabHost
.newTabSpec("russian")
.setIndicator(getText(R.string.rusca),
res.getDrawable(R.drawable.ic_tabs))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tabs.class);
intent.putExtra("questions", getLanguageQuestions(rawQ, 4));
spec = tabHost
.newTabSpec("english")
.setIndicator(getText(R.string.ingilizce),
res.getDrawable(R.drawable.ic_tabs))
.setContent(intent);
tabHost.addTab(spec);
Log.v(TAG, 0+"");
FrameLayout questionsLayout = (FrameLayout) tabHost.findViewById(android.R.id.tabcontent);
Log.v(TAG, 1+""+questionsLayout.getId());
Log.v(TAG, 2+"");
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(this);
}
}
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
FrameLayout questionsLayout = (FrameLayout) tabHost.findViewById(android.R.id.tabcontent);
questionsLayout.setAnimation(AnimationUtils.loadAnimation(tabHost.getContext(), R.anim.go_left_in));
}
// Database related elements
private Question[] parseJson(String text) {
JSONArray data = null;
JSONObject groups = null;
String[][] rawData = null;
JSONArray[] questions = null;
Question[] rawQ = null;
try {
data = new JSONArray(text);
questions = new JSONArray[data.length() - 1];
rawQ = new Question[questions.length];
groups = data.getJSONObject(0);
rawData = new String[2][groups.length()];
Iterator it = groups.keys();
int index = 0;
while (it.hasNext()) {
rawData[0][index] = (String) it.next();
rawData[1][index] = groups.getString(rawData[0][index]);
index++;
}
for (int i = 0; i < questions.length; i++) {
questions[i] = data.getJSONArray(i + 1);
String[] s = new String[6];
for (int j = 0; j < s.length; j++) {
s[j] = ((questions[i].getString(3 + j) == null) ? ("")
: (questions[i].getString(3 + j)));
}
rawQ[i] = new Question(questions[i].getInt(0),
questions[i].getInt(1), questions[i].getString(2), s);
}
Log.e(TAG, rawQ[1].getQuestion());
return rawQ;
} catch (JSONException e) {
Log.e(ACTIVITY_SERVICE, e.toString());
return null;
// ctv.setText(data.toString());
} catch (ArrayIndexOutOfBoundsException e) {
Log.e(TAG, e.toString());
return null;
}
}
private Question[] getLanguageQuestions(Question[] Qs,int id){
int count=0;
for(Question q:Qs)
count+=((q.getLanguageId()==id)?(1):(0));
Question [] result = new Question[count];
int index=0;
for(Question q:Qs){
if(q.getLanguageId()==id){
result[index]=q;
index++;
}
}
return null;
}
}
答案 0 :(得分:1)
不要使用TabActivity等,我们已按the documentation中所述弃用它们。
使用Fragment执行选项卡。如果您的目标是3.0+,那么与操作栏结合使用非常简单。如果您需要较旧的样式标签,ApiDemos中的示例会显示如何将其与FragmentTabs或Support Library Fragment Tabs等片段一起使用。
或者使用ViewPager,例如Support Fragment Tabs Pager。
答案 1 :(得分:1)
我一直在四处寻找尝试实现这一点,并且实际上通过略微扩展TabHost来获取不同选项卡之间的动画(我的选项卡是单独的活动)。在这个特定的实现中,我已经分离了动画,这样如果你要去旧版的左边或右边的标签,动画会有不同的动画:
public class MyAnimTabHost extends TabHost {
public MyAnimTabHost(Context context) {
super(context);
}
public MyAnimTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setCurrentTab(int index) {
View currentView= this.getCurrentView();
if (this.getCurrentTab()< index){
if (currentView !=null){
currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_out_to_left));
}
super.setCurrentTab(index);
currentView= this.getCurrentView();
if (currentView !=null){
currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_in_from_right));
}
} else {
if (currentView !=null){
currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_out_to_right));
}
super.setCurrentTab(index);
currentView= this.getCurrentView();
if (currentView !=null){
currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_in_from_left));
}
}
}
}
将此作为一个新类添加,ADT会自动将其添加到图形xml编辑器中的自定义视图列表中,这样你就可以在布局中换掉这个TabHost,并且一切都应该是好的(确保你记得实际上实现了不同的动画xml文件。)