如何向TabActivity添加水平滑动?

时间:2013-11-16 05:41:20

标签: android swipe tabactivity

我在这里看过一些类似的帖子,但我没有找到任何答案。我想在我的标签活动中添加滑动手势,以便我可以使用手势更改标签。

这是我的代码:

public class Domiciliaria extends TabActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seleccion);

        //ActionBar bar = getActionBar();
        //bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cd3b05")));

        final TabHost tabHost = getTabHost();
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
                @Override
                public void onTabChanged(String tabId) {
                    // TODO Auto-generated method stub
                    setTabColor(tabHost);
                }
            }
        );

        // Tab for primera
        TabSpec primeraspec = tabHost.newTabSpec("info 1");
        primeraspec.setIndicator("Datos", getResources().getDrawable(R.drawable.primera));
        Intent primeraIntent = new Intent(this, Parte_primera.class);
        primeraspec.setContent(primeraIntent);

        // Tab for segunda
        TabSpec segundaspec = tabHost.newTabSpec("info 2");        
        segundaspec.setIndicator("Motivos", getResources().getDrawable(R.drawable.segunda));
        Intent segundaIntent = new Intent(this, Parte_segunda.class);
        segundaspec.setContent(segundaIntent);

        // Tab for tercero
        TabSpec tercerospec = tabHost.newTabSpec("info 3");        
        tercerospec.setIndicator("Examen", getResources().getDrawable(R.drawable.tercera));
        Intent terceroIntent = new Intent(this, Parte_tercera.class);
        tercerospec.setContent(terceroIntent);

        // Tab for Cuarto
        TabSpec cuartospec = tabHost.newTabSpec("info 4");        
        cuartospec.setIndicator("otro", getResources().getDrawable(R.drawable.cuarta));
        Intent cuartoIntent = new Intent(this, Parte_cuarta.class);
        cuartospec.setContent(cuartoIntent);

        // Tab for Final
        TabSpec finalspec = tabHost.newTabSpec("info 4");        
        finalspec.setIndicator("Final", getResources().getDrawable(R.drawable.pago));
        Intent finalIntent = new Intent(this, Final.class);
        finalspec.setContent(finalIntent);

        // agregando TabSpec to TabHost
        tabHost.addTab(primeraspec); // Adding photos tab
        tabHost.addTab(segundaspec); // Adding songs tab
        tabHost.addTab(tercerospec);
        tabHost.addTab(cuartospec);
        tabHost.addTab(finalspec);
        tabHost.getTabWidget().setStripEnabled(true);
        tabHost.getTabWidget().setRightStripDrawable(R.drawable.barra_mamei);
        tabHost.getTabWidget().setLeftStripDrawable(R.drawable.barra_mamei);
        setTabColor(tabHost);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.about:
            startActivity(new Intent(this, About.class));
            return true;
            case R.id.help:
            startActivity(new Intent(this, Help.class));
            return true;
            case R.id.CasoEspecial:
            startActivity(new Intent(this, CasoEspecial.class));
            return true;
            default:
            return super.onOptionsItemSelected(item);
        }
        // respond to menu item selection
    }

    protected void setTabColor(TabHost tabHost) {
        // TODO Auto-generated method stub
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E04006")); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fbfbf3")); // selected
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用带有Fragments和ActionBar的ViewPager来实现TabSwipe。

如果您希望使用Minimum API 11,以下链接将向您展示如何使用。从API 11开始,ActionBar就可以在应用程序中使用片段创建选项卡。

如果您需要支持旧版本,则应使用Support Library  或ActionBarSherlock

Check out this Link as well

答案 1 :(得分:0)

这是直接来自谷歌的水平滑动视图的最佳教程。

Creating Swipe Views with Tabs

我建议使用FragmentsFragmentActivity代替TabActivity