1

我正在尝试像 Iphone 一样实现 TabBar,并希望在 ListView 上显示信息,在选择城市后,我得到另一个包含与该城市相关的事物的列表,它会在新视图中打开,但是当我调用新视图时,它会丢失 TabBAr,只显示视图,我需要知道他们的任何方式 ** 要在下一个视图中显示信息,但无论什么活动正在进行,TABBAR 都应保持在底部 **

我得到了我的第一个选项卡(主页选项卡)的屏幕截图

我在哪里选择城市移动到下一个屏幕,TabBar 停留在底部,然后到下一个视图,然后到下一个,依此类推,直到我显示图片的最后一个屏幕截图。

但我不知道我应该如何在 Android 中做到这一点。

我应该如何在我的主页选项卡中的所有视图上显示 TabBar

到目前为止,我有这个代码:

TabView.java

TabHostProvider.java

import android.app.Activity;

  public abstract class TabHostProvider {
public Activity context;

public TabHostProvider(Activity context){
    this.context = context;
}

public abstract TabView getTabHost(String category);
 }

[标签.java][2]

MyTabHostProvider.java

import android.app.Activity;
import android.content.Intent;
 import android.graphics.Color;
 import android.graphics.drawable.GradientDrawable;

  public class MyTabHostProvider extends TabHostProvider {

private Tab cityTab;
private Tab searchTab;
private Tab mapTab;
private Tab favouriteTab;
private Tab settingTab;

private TabView tabView;
private GradientDrawable gradientDrawable, transGradientDrawable;

public MyTabHostProvider(Activity context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public TabView getTabHost(String category) {
    // TODO Auto-generated method stub
    tabView = new TabView(context);
    tabView.setOrientation(TabView.Orientation.BOTTOM);
  tabView.setBackgroundID(R.drawable.tab_background_gradient);

    gradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0xFFB2DA1D, 0xFF85A315});
    gradientDrawable.setCornerRadius(0f);
    gradientDrawable.setDither(true);

    transGradientDrawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] {0x00000000, 0x00000000});
    transGradientDrawable.setCornerRadius(0f);
    transGradientDrawable.setDither(true);

    cityTab = new Tab(context, category);
    cityTab.setIcon(R.drawable.navbar_allnormal);
    cityTab.setIconSelected(R.drawable.navbar_allselected);
    cityTab.setBtnText("City");
    cityTab.setBtnTextColor(Color.WHITE);
    cityTab.setSelectedBtnTextColor(Color.BLACK);

    cityTab.setBtnGradient(transGradientDrawable);
    cityTab.setSelectedBtnGradient(gradientDrawable);
    cityTab.setIntent(new Intent(context, CityActivity.class));

    searchTab = new Tab(context, category);
    searchTab.setIcon(R.drawable.navbar_picturenormal);
    searchTab.setIconSelected(R.drawable.navbar_pictureselected);
    searchTab.setBtnText("Search");
    searchTab.setBtnTextColor(Color.WHITE);
    searchTab.setSelectedBtnTextColor(Color.BLACK);

    searchTab.setBtnGradient(transGradientDrawable);
    searchTab.setSelectedBtnGradient(gradientDrawable);
    searchTab.setIntent(new Intent(context, SearchActivity.class));

    mapTab = new Tab(context, category);
    mapTab.setIcon(R.drawable.navbar_filenormal);
    mapTab.setIconSelected(R.drawable.navbar_fileselected);
    mapTab.setBtnText("Map");
    mapTab.setBtnTextColor(Color.WHITE);
    mapTab.setSelectedBtnTextColor(Color.BLACK);

    mapTab.setBtnGradient(transGradientDrawable);
    mapTab.setSelectedBtnGradient(gradientDrawable);
    mapTab.setIntent(new Intent(context, MapFragment.class));

    favouriteTab = new Tab(context, category);
    favouriteTab.setIcon(R.drawable.navbar_videonormal);
    favouriteTab.setIconSelected(R.drawable.navbar_videoselected);
    favouriteTab.setBtnText("Favourites");
    favouriteTab.setBtnTextColor(Color.WHITE);
    favouriteTab.setSelectedBtnTextColor(Color.BLACK);

    favouriteTab.setBtnGradient(transGradientDrawable);
    favouriteTab.setSelectedBtnGradient(gradientDrawable);
    favouriteTab.setIntent(new Intent(context, FavouritesActivity.class));

    settingTab = new Tab(context, category);
    settingTab.setIcon(R.drawable.navbar_morenormal);
    settingTab.setIconSelected(R.drawable.navbar_moreselected);
    settingTab.setBtnText("Settings");
    settingTab.setBtnTextColor(Color.WHITE);
    settingTab.setSelectedBtnTextColor(Color.BLACK);

    settingTab.setBtnGradient(transGradientDrawable);
    settingTab.setSelectedBtnGradient(gradientDrawable);
    settingTab.setIntent(new Intent(context, SettingsActivity.class));

    tabView.addTab(cityTab);
    tabView.addTab(searchTab);
    tabView.addTab(mapTab);
    tabView.addTab(favouriteTab);
    tabView.addTab(settingTab);

    return tabView;
}

    }

CityActivity.java

public class CityActivity extends Activity {

private final String TAG = CityActivity.class.getSimpleName();
private ListView moreList = null;
private final String[] moreItems = { "Srinagar", "Gulmarg", "Pehlgam", "Jammu", "Leh",
        "Patnitop"};

private TextView tv;
private Button action_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHostProvider tabProvider = new MyTabHostProvider(CityActivity.this);
    TabView tabView = tabProvider.getTabHost("City");
    tabView.setCurrentView(R.layout.city);
    setContentView(tabView.render(0));

    moreList = (ListView) findViewById(R.id.moreactivity_list);
    moreList.setAdapter(new MoreCityAdapter(CityActivity.this, moreItems));
    moreList.setOnItemClickListener(new OnItemClickListener() 
    {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            final String activity = moreList.getAdapter().getItem(position).toString().trim();
            Log.d(TAG, "Clicked Item: " + activity);
            tv = (TextView)findViewById(R.id.tv_header_title);
            tv.setText(activity);
              Intent intent = new Intent(CityActivity.this, Second.class);
                startActivity(intent);


    }
});
 }

二.java

  public class Second extends Activity{

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

        Here i need to show the Tab bar with my List View
       How should i get the Tab Bar here and on my following Third,Fourth & Fifth Activity

}
   }

[MoreCityAdapter.java][3]

选择城市

选择地点

全图

4

6 回答 6

3

请尝试在您的代码中实现项目/链接。我认为它对你有用..你只需要在你的项目/代码中实现活动组,它就会像你想要的那样与标签一起工作。:)

于 2013-05-08T07:53:07.293 回答
1

我会建议你几种方法将这种交互集成到我能想到的 android 应用程序中。

  1. 首先,您可以LIST_NAVIGATION在 ActionBar 中使用,您可以在其中添加所有“选项卡”并Fragments根据用户选择添加/删除它们来使用它们。如果你想从 2.2+ 开始支持旧的 Android 版本,你可以使用它是旧版本 API 的ActionBarSherlock一个非常好的实现。ActionBar您可以在 Android 版 Gmail 应用程序的 ActionBar 中找到列表导航的示例,以及如何在此处实现此目的的示例:ActionBarSherlock

  2. 您可以用来实现此类目标的第二种方法是在 Google+、Youtube、Facebook 应用程序中使用 SlidingDrawer,我认为这也是一个不错的选择。这个SlidingMenu有一个很棒的库。

这取决于您认为实现您想要的事情的最佳方式。最后一件事,如果你真的想使用像 IOS 这样的标签,我建议你做这样的事情:

活动主.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

    </LinearLayout>
</TabHost>

MainTabActivity

public class MainActivity extends SherlockFragmentActivity {

private TabHost mTabHost;
private TabManager mTabManager;

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

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);


    TabHost.TabSpec spec = mTabHost.newTabSpec("collections");
    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, mTabHost.getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title); // tab title
    title.setText(getString(R.string.collection_tab));
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);  // tab icon
    icon.setImageResource(R.drawable.ic_tab_collections);
    spec.setIndicator(tabIndicator);
    mTabManager.addTab(spec, MyCollectionList.class, null); // WHERE MyCollectionList is Fragment attached to first tab.

    // MYLIST TAB
    TabHost.TabSpec spec5 = mTabHost.newTabSpec("mylist");
    View tabIndicator5 = LayoutInflater.from(this).inflate(R.layout.tab_layout, mTabHost.getTabWidget(), false); // used for custom design of tab
    TextView title5 = (TextView) tabIndicator5.findViewById(R.id.title); // tab title
    title5.setText(getString(R.string.my_list));
    ImageView icon5 = (ImageView) tabIndicator5.findViewById(R.id.icon); // tab icon
    icon5.setImageResource(R.drawable.ic_tab_mylist);
    spec5.setIndicator(tabIndicator5);
    mTabManager.addTab(spec5, MyList.class, null); // MyList - second Fragment attached to second tab

    // ADD as many tabs as you want


    mTabHost.setCurrentTab(0); // set selected tab

    }

}

TabManager.class(取自 ActionBarSherlock 示例,稍作修改):

import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;

/**
 * This is a helper class that implements the management of tabs and all
 * details of connecting a ViewPager with associated TabHost.  It relies on a
 * trick.  Normally a tab host has a simple API for supplying a View or
 * Intent that each tab will show.  This is not sufficient for switching
 * between pages.  So instead we make the content part of the tab host
 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
 * view to show as the tab content.  It listens to changes in tabs, and takes
 * care of switch to the correct paged in the ViewPager whenever the selected
 * tab changes.
 */
public class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private int mLastFragmentId;
    private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    TabInfo mLastTab;

    static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
        mActivity = activity;
        mTabHost = tabHost;
        mContainerId = containerId;
        mTabHost.setOnTabChangedListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
        if (info.fragment != null && !info.fragment.isDetached()) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            ft.detach(info.fragment);
            ft.commit();
        }

        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                FragmentManager mManager = mActivity.getSupportFragmentManager();
                if(mManager.getBackStackEntryCount() > 0){
                    mLastFragmentId = mManager.getBackStackEntryAt(0).getId();
                    mManager.popBackStack(mLastFragmentId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                }
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
        }
    }

希望这篇文章能帮助你找到实现你想要的东西的最佳方法!:)

于 2013-04-26T10:11:38.977 回答
0

就像已经建议的那样,我也建议使用操作栏,不仅因为不推荐使用 TabBarActivity,还因为它是 Android 的 UI 设计。Android 用户会想要一个类似 Android 的 UI 而不是 iOS 的,因为他或她不习惯这种工作流程。

如果你想要传统平台支持,我会推荐这个库。 http://actionbarsherlock.com/ 许多应用程序都使用它,包括 whatsappfoursquare 等等。

对于您的 porpuse,您正在寻找寻呼机,进行 google 搜索,您会发现很多与 actionbar sherlock 组合的信息。或者查看 Actionbar 提供的示例,他们几乎都在示例中给出了场景。

希望这可以帮助。

于 2013-04-26T09:00:35.650 回答
0

是的,你可以这样做

我通过为 TabHost 上的每个选项卡创建一个堆栈来做到这一点,该堆栈包含所有视图,我使用 ActivityGroup 就像这样。

1 - 创建您的堆栈管理器

import java.util.Stack;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

@SuppressWarnings("deprecation")
public class FirstStackManager extends ActivityGroup {

        private Stack<String> MY_STACK;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (MY_STACK == null)
            MY_STACK = new Stack<String>();
        // start default activity
        push("FirstActivity", new Intent(this, FirstActivity.class));
    }

    @Override
    public void finishFromChild(Activity child) {
        pop();
    }

    @Override
    public void onBackPressed() {
        pop();
    }

    public void push(String id, Intent intent) {
        Window window = getLocalActivityManager().startActivity(id,
                intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK));
        if (window != null) {
                MY_STACK.push(id);
            setContentView(window.getDecorView());
        }
    }

    public void pop() {
        if (MY_STACK.size() == 1)
            finish();
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity(MY_STACK.pop(), true);
        if (MY_STACK.size() > 0) {
            Intent lastIntent = manager.getActivity(MY_STACK.peek())
                    .getIntent();
            Window newWindow = manager.startActivity(
                    MY_STACK.peek(), lastIntent);
            setContentView(newWindow.getDecorView());
        }
    }
}

不要忘记在 Manifest 文件中添加您的 FirstStackManager

2 - 在你的主要活动中使用这样的堆栈

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

        TabHost tabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.tab);


            tabHost = getTabHost();
        setTabs();
    }

    private void setTabs() {
        addTab("FirstTab", R.drawable.tab_first, FirstStackManager.class);
        addTab("SecondTab", R.drawable.tab_second, SecondStackManager.class);
                /*Other tab*/
    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        Intent intent = new Intent().setClass(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, getTabWidget(), false);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

你可以在这里找到如何创建 xmls 文件Raised-Center-Tab-in-Android

3 - 在你的 FirstActivity 上推送这样的新视图

public class FirstActivity extends Activity{

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

      /*push a view on list click listeneer*/

       Intent intent = new Intent();
       intent.setClass(FirstActivity.this, DetailActivity.class);
       FirstStackManager activityStack = (FirstStackManager) getParent();
       activityStack.push("DetailActivity", intent); 

}
   }

4 - 在你的 DetailActivity 弹出视图上

public class DetailActivity extends Activity{

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

      /*pop a view on back button click listeneer*/

       FirstStackManager activityStack = (FirstStackManager) getParent();
       activityStack.pop(); 

}
   }
于 2013-04-26T10:15:51.967 回答
0

我建议您使用 Android 设计模式中为此目的而设计的 ActionBar。- TabBarActivity 已弃用 http://developer.android.com/reference/android/app/TabActivity.html

更多信息在这里: http ://www.androiduipatterns.com/2011/12/how-should-android-apps-look-like_16.html

于 2013-04-26T08:49:56.910 回答
0

您需要哪个类该类名称给出该选项卡意图..

   Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
   // Intent intent;  // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    Intent intent1 = new Intent().setClass(this, Second.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("app_name").setIndicator("Places",
                  .setContent(intent1);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0);
    // Do the same for the other tabs
    Intent intent2 = new Intent().setClass(this, Third.class);
    spec = tabHost.newTabSpec("application").setIndicator("City",
                  .setContent(intent2);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(1);
    Intent intent3 = new Intent().setClass(this, First.class);
    spec = tabHost.newTabSpec("toplinks").setIndicator("VISTED",
                  .setContent(intent3);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(2);
    tabHost.setCurrentTab(0);
}

选项卡.xml

       <?xml version="1.0" encoding="utf-8"?>
    <TabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

    <LinearLayout 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">
   <TabWidget 
android:id="@android:id/tabs" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></TabWidget>
    <FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"></FrameLayout>
    </LinearLayout>
    </TabHost>

让我的问题解决与否...

于 2013-04-26T08:56:30.213 回答