我使用导航抽屉实现了我的导航应用程序,但其中一个项目打开片段有两个标签。
我已经实施了“FragmentTabHost”标签,因为 Roman Nurik 不建议将“ViewPager”与“NavigationDrawer”一起使用。
Roman Nurik https://plus.google.com/u/0/+DavidTaSmoochibi/posts/8dWEkFcbTFX
一切正常我的问题是FragmentTabHost是否可以在标签之间实现水平滑动?
附加代码:
Main_Activity.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" />
MainActivity.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);
}
}
Fragment1.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" >
Fragment1.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可以做得更好。
答案 0 :(得分:4)
所以他的完整答案是
您不应将导航抽屉与操作栏标签一起使用。如果你是 旨在获得类似于Google Play音乐的用户界面,您应该这样做 手动实现标签(并注意这在平板电脑上看起来如何 - 你 不要在平板电脑上使用全宽标签栏。还要确保检查 今年的Android App Design会议中的结构 Google I / O用于详细贯穿各种界面 可用于公开应用层次结构的模式。
所以基本上他建议重新考虑用户界面...而且我认为不建议这样做,因为拉出抽屉和在标签之间滑动实际上是使用相同的手势..
编辑:如果你真的想手动做TabHost-Swipe,我想唯一的方法就是使用Flipper和Gesture探测器(实现GestureListener)..这是你需要实现的一些代码,但我希望{{3} }可以帮你一点..;)EDIT2:还有一个例子this example of Swipe Action and ViewFlipper in Android(就像作者知道我们昨天在谈论它一样;)
答案 1 :(得分:0)
在我的一个项目中,我必须做类似的事情。我想设计一个类似Google News Stand的视图。我在网上找不到任何易于实现的内容。所以我写了自己的。我正在使用FragmentTabHost和ViewPager以及可滚动的标签。完整实施在我的博文here上。