- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我使用 Navigation Drawer 实现了我的导航应用程序,但其中一个项目打开 fragment 带有两个选项卡。
我实现了“FragmentTabHost”的选项卡,因为 Roman Nurik 不建议将“ViewPager”与“NavigationDrawer”一起使用。
罗曼努里克 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做这样的事情比较好。
最佳答案
所以他的完整答案是
You shouldn't use navigation drawers with action bar tabs. If you're aiming for a UI similar to that of Google Play Music, you should implement tabs manually (and beware of how this looks on tablet—you don't want full-width tab bars on tablets). Also make sure to check out the Structure in Android App Design session from this year's Google I/O for a detailed run-through of the various interface patterns available for exposing app hierarchy.
所以基本上他是在建议重新考虑 UI ...我想这不是推荐的,因为拉出抽屉和在选项卡之间滑动实际上使用的是相同的手势。
编辑:如果你真的想手动执行 TabHost-Swipe,我想唯一的方法是使用 Flipper 和手势检测器(实现 GestureListener)。你需要实现一些代码,但我希望 this example of Swipe Action and ViewFlipper in Android可以帮助你一点.. ;)
EDIT2:还有一个例子here (就像作者知道我们昨天在谈论它一样;))
关于android - Fragment TabHost 水平滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20150670/
我正在做一个学校项目,在 android studio 上制作一个信息应用程序。我需要创建一个包含 4 个选项卡的 tabHost。在选项卡 1 中,我必须能够左右滑动才能显示 5 个不同的 Acti
我有一个 tabhost,它位于我的主要 Activity (抽屉导航)中。 在每个选项卡中,我都有 ViewPager。 ViewPager 由不同的 Fragment 组成。 在这些 fragme
我注意到使用 tabhost 有很多问题,但没有一个真正适合我的问题。我正在尝试使用 action bar 创建 View 在顶部,然后是栏下方的一排选项卡。这是我的 Activity 布局。
我是 Android 新手。 我正在实现与选项卡主机相关的编码。 我在选项卡主机中创建了三个选项卡,分别命名为 tab1、tab2、tab3。 当我启动我的应用程序时,tab1 会自动触发。 我的意图
我有一个正在切换 Activity 的 TabHost Activity ,所以我的主要内容如下所示: 我的一个子 Activity XML
我创建了一个应用程序,其中包含四个加载了特定 WebView 的选项卡。我计划在选项卡下方添加广告。在我的代码中,我将内容 View 设置为创建为 TabHost 的 ViewGroup。如果我将此
下面是我想要的图片。 现在。当我单击某个项目(参见图像中的 2)时,会显示不正确的版本(参见图像中的 3)。 如何将第二个 tabhost(参见图中的 3)加载到第一个 tabhost 的框架布局中?
我有一个问题: Java 代码 public class VisualizzaListaActivity extends TabActivity { /** Called when the activ
我的程序的很多用户都报告了相同错误的“强制关闭”,我无法重现该错误,因此很难调试! 好像和TabHost有关(我的申请里有两个) 现在我尝试扩展 TabHost 覆盖 dispatchWindowFo
我正在使用 Android 中的 Eclipse 创建一个应用程序。我的软件运行完美,直到我创建了一个函数,该函数从我的一个选项卡式 Activiy 创建了一个返回 MainActivity 的新 I
我试图在项目中使用垂直选项卡,但我无法让 tabhost 工作。我认为我没有从 Java 正确进入,但我不确定我做错了什么。虽然例子很多,但似乎已经过时了。看来 tabhost 已经改变了很多,所以我
有人可以帮助我完成这个 TabHost 示例吗?我遇到的问题是,当我尝试运行应用程序时我遇到空指针异常。 这是代码,如果有人需要看一下。 public class TabBarExample exte
我的 TabHost Activity 由于错误而无法正确加载。这是我的代码、xml 和错误日志: public class TabScreen extends TabActivity { @Over
我的应用程序中有一些选项卡,我希望最后一个选项卡在默认系统浏览器中启动谷歌。我认为这会起作用: Uri uri = Uri.parse("http://www.google.com/"); tabHo
我想要不同数量的选项卡并且我正在使用 TabHost。这些可能在 1 到 8 之间,具体取决于数据。 我想添加水平滚动条,这样当所有 8 个滚动条都在那里时,它看起来就不会显得局促。 问题是当有 5
我在 TabHost 中有一个 FrameLayout。我收到以下错误。如何让它在 Eclipse 中呈现? Error during post inflation process: The Fram
我在主布局中使用 TabHost,目前只有 2 个选项卡。如果我在第二个选项卡上并更改方向,应用程序会将我返回到第一个选项卡。有什么办法可以防止这种情况发生吗? 最佳答案 将以下代码添加到定义了 ta
我正在创建总共有 3 个 Activity 的简单应用程序 登录 - 具有用户名、密码字段以及登录和注册按钮 注册 - 具有各种表单字段和提交按钮 留言-登录成功后我们就来参加这个 Activity
我这里发生了一些奇怪的事情! 我有一个 Tabhost,有 3 个选项卡。在我的模拟器中,它运行良好,来回运行。但是在我的平板电脑(HP Touchpad 和 Samsung Galaxy Tab 1
我的目标是创建两个选项卡,每个选项卡都有不同的 Activity 。我希望选项卡始终在顶部可见,以便我可以轻松地在两者之间切换。我相信已经设置好一切以使用 tabHost 创建两个选项卡,但是当我运行
我是一名优秀的程序员,十分优秀!