从固定标签中删除滑动操作

Elv*_*ira 1 java android android-layout android-tabs

我使用ADT为Android 4+版本创建了一个Android项目,并使用"固定标签+滑动"(使用向导)创建了一个主要活动...但我不需要滑动操作,因此,我如何禁用它我的应用?这是可能的?

非常感谢!

Kar*_*uri 5

  1. 将ViewPager替换为activity_main.xml其他内容(如FrameLayout)并将其id更改为合理的内容,例如@+id/activity_root
  2. 从MainActivity中删除与ViewPager和SectionsPagerAdapter相关的所有内容.
  3. 使用onTabSelected回调切换片段.

这样的事情应该有效.您必须添加逻辑来创建和维护片段.

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) {}
}
Run Code Online (Sandbox Code Playgroud)

布局:

<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" />
Run Code Online (Sandbox Code Playgroud)