简体   繁体   中英

Android tab bar with different fragments and different action bar icons

Problem Description and Question

I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)

在此输入图像描述

Wen user click on the items in the grid view I want my application to take following actions:

  • Change icon of application to grid view item image on which I pressed.
  • Change the test near the icon
  • Add standard back button near icon, which will go back to grid view screen.
  • Change the tab fragment to the one which I specify.

Like it is shown in the image below:

在此输入图像描述

As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?

This might help: Android studio - is possible to add tabs pointing to fragments from designer? It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^

John

The first link you can check is THIS .

And you should read more about ActionBar .

The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.

You have to use actionbarsherlock library for it.

use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    style="@style/yourstyle"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        style="@style/realtabFrameContentStyle" />


       <TabWidget
          android:id="@android:id/tabs"
         style="@style/yourtabstyle" />

</android.support.v4.app.FragmentTabHost>

Use SherlockFragmentActivity for showing the tabs.

In the activities code use following code (preferably in a function) to set the ctionbar icon and text :

activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);


((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");

        ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
        homeButton.setVisibility(View.VISIBLE);
        homeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, YourHOmeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                activity.startActivity(intent);
                activity.finish();
            }
        });
        ActionBar mActionBar = activity.getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        mActionBar.show();

then call this function with your icon and your text in your fragments onResume() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM