我用navigation drawer实现了我的导航应用程序,但其中一个条目用两个选项卡打开片段。
我实现的选项卡是“fragmenttabhost”,因为Roman Nurik不建议将“viewPager”与“NavigationDrawer”一起使用。
罗马努里克https://plus.google.com/u/0/+DavidTaSmoochibi/posts/8dWEkFcbTFX
一切正常我的问题是,fragmenttabhost是否可以在选项卡之间实现水平滑动?
附加代码:
主要活动.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

main活动.java
public class MainActivity extends ActionBarActivity {

private DrawerLayout            mDrawerLayout;
private ListView                mDrawerList;
private ActionBarDrawerToggle   mDrawerToggle;
private Fragment                fragment1;
private Fragment                fragment2;

private String[]                title;
private String[]                subtitle;
private int[]                   icon;

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

    fragment1 = new Fragment1();
    fragment2 = new Fragment2();

    /**
     * Implementacion del NavigationDrawer
     */
    title = new String[] {"Noticias", "Promociones"};
    subtitle = new String[] {"Noticias", "Promociones"};

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, title));

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    //Enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // TODO Auto-generated method stub
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
            super.onDrawerOpened(drawerView);
        }
    };

   mDrawerLayout.setDrawerListener(mDrawerToggle);

   if (savedInstanceState ==  null) {
       selectItem(0);
   }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    if (item.getItemId() == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

    return super.onOptionsItemSelected(item);
}

/**
 * The click listener for ListView in the navigation drawer
 * @author TeRRo
 */
private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        selectItem(position);
    }

}

private void selectItem (int position) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    switch (position) {
    case 0:
        ft.replace(R.id.content_frame, fragment1);
        break;

    case 1:
        ft.replace(R.id.content_frame, fragment2);
        break;
    }
    ft.commit();
    //Close drawer
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    mDrawerToggle.onConfigurationChanged(newConfig);
}
}

片段1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

片段1.java
public class Fragment1 extends Fragment {

//Declare variable of TabHost
private FragmentTabHost mTabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    mTabHost = new FragmentTabHost(getActivity());
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment1);
    mTabHost.addTab(mTabHost.newTabSpec("noticas").setIndicator("Noticias"), FragmentTab1.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("promociones").setIndicator("Promociones"), FragmentTab2.class, null);


    return mTabHost;

}

//Detach FragmentTabHost
@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class
                .getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }

}

//Remove FragmentTabHost
@Override
public void onDestroyView() {
    super.onDestroyView();
    mTabHost = null;
}

}

可以用fragmenttabhost实现水平滑动吗?
我搜索了一些信息,但都说是为了更好地使用viewpager。

最佳答案

所以他完整的回答是
不应该使用带有操作栏选项卡的导航抽屉。如果你是
针对类似于google play music的用户界面,您应该
手动实现制表符(注意在平板电脑上的外观
不希望平板电脑上有全宽标签栏)。还要确保检查
在今年的安卓应用程序设计会议上
google i/o,用于各种界面的详细运行
可用于公开应用程序层次结构的模式。
所以基本上他建议重新考虑用户界面…我想这是不推荐的,因为拉出抽屉和在标签之间滑动实际上是用同样的手势。
编辑:如果你真的想手动刷tabhost,我想唯一的方法是使用flipper和手势检测器(实现gesturelister)。这是您需要实现的一点代码,但我希望this example of Swipe Action and ViewFlipper in Android可以帮您一点忙。;)
edit2:还有一个例子(就像作者知道我们昨天在谈论它一样;)

关于android - FragmentTabHost水平滑动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20150670/

10-10 23:04