从固定选项卡中删除刷卡动作 [英] Remove Swipe Action from Fixed Tab

查看:160
本文介绍了从固定选项卡中删除刷卡动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Android项目与ADT为Android 4+版本,并创建了固定标签+刷卡(使用向导)主要活动...但我不需要刷卡的动作,所以,我怎么能在我的应用程序禁用它?这是可能的吗?

非常感谢!


解决方案

  1. activity_main.xml中用别的东西(像的FrameLayout)更换ViewPager并改变其ID为@ + ID / activity_root理智的东西像

  2. 卸下MainActivity有关ViewPager和SectionsPagerAdapter一切。

  3. 使用onTabSelected回调来切换片段。

像这样的东西应该工作。你必须添加逻辑来创建和维护您的片段。

 公共类MainActivity扩展活动实现ActionBar.TabListener {    私人诠释mFragmentCount;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //设置操作栏。
        最后的动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        //对于每一个应用中的部分,添加标签,操作栏。
        mFragmentCount = 3;
        的for(int i = 0; I< mFragmentCount;我++){
            //创建一个带文本的标签还指定该活动对象,
            //实现TabListener接口,回调(听众)
            //当选择该选项卡。
            actionBar.addTab(actionBar.newTab()的setText(标签+ I).setTabListener(本));
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
        //开关片段
        //使用fragmentTransaction方法与R.id.activity_root的容器ID
        //不调用commit(),它会被称为为你
    }    @覆盖
    公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){}    @覆盖
    公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){}
}

布局:

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / activity_root
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=。MainActivity/>

I created an Android Project with ADT for Android 4+ versions and created a main Activity with "Fixed Tabs + Swipe" (with the wizard)... but i don't need the swipe action, so, how can i disable it in my app? It's possible?

Thanks a lot!

解决方案

  1. Replace the ViewPager in activity_main.xml with something else (like FrameLayout) and change its id to something sensible, like @+id/activity_root
  2. Remove everything related to ViewPager and SectionsPagerAdapter from MainActivity.
  3. Use the onTabSelected callback to switch fragments.

Something like this should work. You'll have to add logic to create and maintain your fragments.

public class MainActivity extends Activity implements ActionBar.TabListener {

    private int mFragmentCount;

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
        mFragmentCount = 3;
        for (int i = 0; i < mFragmentCount; i++) {
            // Create a tab with text Also specify this Activity object, which
            // implements the TabListener interface, as the callback (listener)
            // for when this tab is selected.
            actionBar.addTab(actionBar.newTab().setText("Tab " + i).setTabListener(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 void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Switch fragments
        // use fragmentTransaction methods with R.id.activity_root for the container id
        // don't call commit(), it will be called for you
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
}

Layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

这篇关于从固定选项卡中删除刷卡动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆