onTabChanged不起作用,片段重叠

时间:2014-11-27 04:43:31

标签: android android-fragments

我有一个FragmentTabHost并且在片段切换时遇到了一些问题。尝试片段切换然后单击另一个选项卡后,选项卡内容重叠。然后我尝试覆盖onTabChanged方法(不知道它是否可以作为解决方案......),但是我发现onTabChanged没有运行!

- 更新 -

onTabChanged现在可以运行但重叠问题仍然存在。

这是我的代码:

MainActivity

public class MainActivity extends FragmentActivity implements OnTabChangeListener{
    private static final String TAB = "MainActivity";

    private FragmentTabHost tabMenu;
    public static final String NEWS_TAB = "news";
    public static final String SHARE_TAB = "share";
    public static final String CAMERA_TAB = "camera";
    public static final String STATUS_TAB = "status";
    public static final String OTHER_TAB = "other";


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

        tabMenu = (FragmentTabHost)findViewById(R.id.main_tabhost);
        tabMenu.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        String[] tabTitle = getResources().getStringArray(R.array.tab_title);
        for (int i=0; i<tabTitle.length; i++){

            String tagId = "";
            int tabIconDrawable = 0;
            Class tabClass = BlankTab.class;

            switch(tabTitle[i]){
            case "最新消息":
                tabIconDrawable = R.drawable.ic_action_view_as_list;            
                tagId = this.NEWS_TAB;
                tabClass = News.class;
                break;
            case "貨件分享":
                tabIconDrawable = R.drawable.ic_action_important;
                tagId = this.SHARE_TAB;
                tabClass = GoodsShare.class;
                break;
            case "":
                tabIconDrawable = R.drawable.ic_action_camera;
                tagId = this.CAMERA_TAB;
                break;
            case "物流狀態":
                tabIconDrawable = R.drawable.ic_action_airplane_mode_on;
                tagId = this.STATUS_TAB;
                tabClass = LogisticStatus.class;
                break;
            case "其他":
                tabIconDrawable = R.drawable.ic_action_overflow;
                tagId = this.OTHER_TAB;
                tabClass = Others.class;
                break;
            }

            View tabView = getLayoutInflater().inflate(R.layout.tab_layout, null, false);
            ImageView tabIcon = (ImageView)tabView.findViewById(R.id.tab_icon);
            TextView tabText = (TextView)tabView.findViewById(R.id.tab_title);

            tabIcon.setImageDrawable(getResources().getDrawable(tabIconDrawable));
            tabText.setText(tabTitle[i]);

            TabSpec spec = tabMenu.newTabSpec(tagId).setIndicator(tabView);

            tabMenu.addTab(spec, tabClass, null);
            tabMenu.getTabWidget().getChildAt(i).setBackground(getResources().getDrawable(R.drawable.main_tab_selector));
        }

        tabMenu.setOnTabChangedListener(this);

    }

    @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.

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub
        Log.d(TAB, tabId);
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        Fragment fragment = null;
        if (tabId.equals(this.NEWS_TAB)){
            fragment = new News();
        } else if (tabId.equals(this.SHARE_TAB)){
            fragment = new GoodsShare();
        } else if (tabId.equals(this.CAMERA_TAB)){
            fragment = new Camera();
        } else if (tabId.equals(this.STATUS_TAB)){
            fragment = new LogisticStatus();
        } else if (tabId.equals(this.OTHER_TAB)){
            fragment = new Others();
        }

        if (fragment != null){
            t.replace(android.R.id.tabcontent, fragment);
        } else {
            Log.e(TAB, "fragment creation error");
        }
    }


}

其他&lt; ---有片段切换

public class Others extends Fragment{
    private View view;
    private Controller controller;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        controller = (Controller)this.getActivity().getApplication();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        view = inflater.inflate(R.layout.others, container, false);
        setLayout();
        return view;
    }

    private void setLayout(){
        ListView othersMenu = (ListView)view.findViewById(R.id.others_menu);
        String[] othersItem = getResources().getStringArray(R.array.others);

        ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, othersItem);
        othersMenu.setAdapter(adapter);
        othersMenu.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String item = (String)parent.getItemAtPosition(position);
                Fragment fragment = null;
                switch(item){
                case "貨倉地址":
                    fragment = new WarehouseAddress();
                    break;
                case "托運流程":
                    fragment = new TransProcess();
                    break;
                case "常見問題":
                    fragment = new FAQ();
                    break;
                case "聯絡我們":
                    fragment = new ContactUs();
                    break;
                case "運費計算器":
                    fragment = new FeeCalculater();
                    break;
                default:
                    fragment = new BlankTab();
                }

                controller.fragmentSwitch(getActivity(), fragment);

            }

        });
    }
}

fragmentSwitch方法

public void fragmentSwitch(Fragment fragment){
    FragmentManager fragmentManager = slideMenuActivity.getSupportFragmentManager();
    fragmentManager.beginTransaction()
            //.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .replace(android.R.id.tabcontent, fragment)
            .commit();
    fragmentManager.executePendingTransactions();
}

1 个答案:

答案 0 :(得分:0)

最后,我发现了它。在onTabChanged中错过了commit()。

@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    Log.d(TAB, tabId);
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    Fragment fragment = null;
    if (tabId.equals(this.NEWS_TAB)){
        fragment = new News();
    } else if (tabId.equals(this.SHARE_TAB)){
        fragment = new GoodsShare();
    } else if (tabId.equals(this.CAMERA_TAB)){
        fragment = new Camera();
    } else if (tabId.equals(this.STATUS_TAB)){
        fragment = new LogisticStatus();
    } else if (tabId.equals(this.OTHER_TAB)){
        fragment = new Others();
    }

    if (fragment != null){
        t.replace(android.R.id.tabcontent, fragment).commit(); //missing commit() here
    } else {
        Log.e(TAB, "fragment creation error");
    }
}