gpt4 book ai didi

android - Fragment TabHost 水平滑动

转载 作者:搜寻专家 更新时间:2023-11-01 08:01:51 24 4
gpt4 key购买 nike

我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com