我对Android编程很陌生,但我会尽力提供所需的所有信息。我正在使用新的android.support.v7.app.ActionBarActivity
和android.support.v4.app.Fragment
来显示Android API的标签布局,范围从8到17。
我正面临一个问题,在我的活动中正确显示我的两个片段,因为它们在我选择其中一个片段后相互叠加。所以,这是我的主要活动代码:
package it.koopa.scank;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
private final static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//have to use getSupportActionBar from android.support.v7.app
ActionBar actionBar = getSupportActionBar();
//hello tab
Tab tab = actionBar.newTab()
.setText(R.string.tab_hello)
.setTabListener(new TabListener<HelloFragment>(this, "hello", HelloFragment.class));
actionBar.addTab(tab);
//handle content tab
tab = actionBar.newTab()
.setText(R.string.tab_send)
.setTabListener(new TabListener<HandleContentFragment>(this, "handle", HandleContentFragment.class));
actionBar.addTab(tab);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.scank, menu);
return true;
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final ActionBarActivity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(ActionBarActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(getCorrectActionBarId(), mFragment, mTag);
Log.i(TAG, "FragID " + mFragment.getId() + ", FragTAG=" + mFragment.getTag() + " ADDED!!!");
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
Log.i(TAG, "FragID " + mFragment.getId() + ", FragTAG=" + mFragment.getTag() + " attached.");
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
Log.i(TAG, "FragID " + mFragment.getId() + ", FragTAG=" + mFragment.getTag() + " detached.");
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
/**
* Returns the correct id of the action bar
* @return
*/
public static int getCorrectActionBarId () {
int androidVersion = Build.VERSION.SDK_INT;
if (androidVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return android.R.id.content;
} else {
return R.id.action_bar_activity_content;
}
}
}
这些是我的两个片段(都使用自己的xml布局)。第一个:
package it.koopa.scank;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HelloFragment extends Fragment {
private int index;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello, container, false);
Log.i("HelloFragment","I'm " + HelloFragment.class);
return v;
}
}
和第二个:
package it.koopa.scank;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HandleContentFragment extends Fragment {
private int index;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.handle_content, container, false);
Log.i("HandleContentFragment","I'm " + HandleContentFragment.class);
return v;
}
}
并且因此,如果我选择第二个选项卡,则视图将被放置在另一个选项卡上! 我在Tabs using android.support.v7.app.ActionBar找到了一个类似的问题,但是接受的解决方案对我来说似乎不起作用。事实上,在我的主要活动中,您可以看到我使用
获取内容ID int androidVersion = Build.VERSION.SDK_INT;
if (androidVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return android.R.id.content;
} else {
return R.id.action_bar_activity_content;
}
但片段内容仍然被覆盖(因为我的声誉几乎为零,我无法上传图片)。 我哪里错了?
更新:
我的活动使用android:theme="@style/Theme.AppCompat.Light"
。我正在使用Android 4.2.1测试Nexus i9250
答案 0 :(得分:2)
这是一个已知的错误。 您可以在此处找到解决方法:
答案 1 :(得分:0)
让每个Fragment都实现ActionBar.TabListener。将以下这些方法与片段中的其他代码一起添加
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = new MyFragment();
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
您可以从主要活动中删除标签监听器。这可以解决所有问题。
答案 2 :(得分:0)
只需在ft.show(mfragment)
中调用onTabSelected
方法,在ft.hide(mfragment)
调用onTabUnselected
方法。
看下面
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.show(mfragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.hide(mfragment);
}