片段内容相互重叠

时间:2015-02-28 15:59:01

标签: android android-fragments

我使用了谷歌提供的文档。一切都很好但是当通过制表符切换时片段的内容彼此重叠。

这是代码。

主要活动

public class login_register extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            setContentView(R.layout.login_register);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009688")));
            ActionBar.Tab  loginTab = actionBar.newTab().setIcon(R.drawable.icon);
            ActionBar.Tab   RegisterTab = actionBar.newTab().setIcon(R.drawable.icon);
            RegisterTab.setText("Register");
            loginTab.setText("Login");
            RegisterTab.setTabListener(new TabListener<register>(this, "register", register.class));
            loginTab.setTabListener(new TabListener<login>(this, "login", login.class));
            actionBar.addTab(loginTab);
            actionBar.addTab(RegisterTab);
    }

    public  class TabListener<T extends Fragment> implements ActionBar.TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;
        public TabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
           mFragment = getSupportFragmentManager().findFragmentByTag(mTag);
            if (mFragment == null) {
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(android.R.id.content, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            mFragment = getSupportFragmentManager().findFragmentByTag(mTag);

            if (mFragment == null ) {
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Usually do nothing.
        }

    }
}

这是其中一个片段。

public class login extends Fragment {
    TextView txt;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.login, container, false);
        return rootView;
    }
}

我不知道我做错了什么。请帮忙。

编辑 这是图片

enter image description here

3 个答案:

答案 0 :(得分:0)

你的问题在这里:

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
   mFragment = getSupportFragmentManager().findFragmentByTag(mTag);
    if (mFragment == null) {
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        ft.attach(mFragment);
    }
}

fragmentTransaction.add(...)会将提供的片段添加到视图中,而不是已存在的任何内容。您想要替换现有的:

ft.replace(android.R.id.content, mFragment, mTag);

答案 1 :(得分:0)

您也可以使用fragmentTransaction.add(...),将android:background属性设置为两个片段布局以解决此问题。

答案 2 :(得分:0)

onTabSelected使用中:

ft.attach(mFragment).commit(); replace ft.attach(mFragment);

onUnTabSelecteddetach应该是if (mFragment != null )时, 所以它应该是:

     if (mFragment != null ) {
            ft.detach(mFragment).commit();
        }