如何更改fragmentTabhost中相同选项卡上的tab的主体

时间:2014-04-25 04:33:56

标签: java android android-fragments fragment-tab-host android-nested-fragment

我在做什么

  • 我的片段中有fragmentabhost,其中包含3-tabs
  • 点击标签我可以动态更改片段
  • 我显示On click 的第一个标签的
  • RatingAscending.class

enter image description here

我想做什么

  • 我要显示的同一标签的Onclick RatingDescending.class

注意:在onResume()我能够检测到click event

firsttab

现在,当我点击RatingDescending.class

的标签时,如何将标签从RatingAscending.class更改为second time

enter image description here


Fragment1.java

public class Fragment1 extends SherlockFragment{

    private FragmentTabHost mTabHost;

    private static int count=0;
    //Mandatory Constructor
    public Fragment1() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment1,container, false);


        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Rating"),
                RatingAscending.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Price"),
                PriceAscending.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Distance"),
                DistanceAscending.class, null);


        return rootView;
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                count++;
                Log.d("You clicked ", count+"time");

            }
        });
    }   
}

1 个答案:

答案 0 :(得分:0)

  • 我终于在这里找到了答案(I refered this post),我希望这有助于将来的某人
  • 下面的代码对于实现这一目标非常重要

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;
import com.dev.navigation.DistanceAscending;
import com.dev.navigation.DistanceDescending;
import com.dev.navigation.PriceAscending;
import com.dev.navigation.RatingAscending;
import com.dev.navigation.RatingDescending;

public class Fragment1 extends SherlockFragment{

    private FragmentTabHost mTabHost;

    private static int count=0;
    //Mandatory Constructor
    public Fragment1() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("Log", "onCreate");

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment1,container, false);
        Log.d("Log", "onCreateView");

        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Rating"),RatingAscending.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Price"),PriceAscending.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Distance"),DistanceAscending.class, null);
        return rootView;
    }


    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d("Log", "onStart");

        mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                count++;
                Log.d("You clicked ", count+"time");


                RatingDescending fObj=new RatingDescending();
                //FragmentTransaction manager = getChildFragmentManager().beginTransaction();
                FragmentTransaction ft = getChildFragmentManager().beginTransaction();
                //android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, fObj);
                ft.commit();

                /*
                 *-------OVERLAPPING----FRAGMENT------CODE 
                 * RatingDescending fObj=new RatingDescending();
                android.support.v4.app.FragmentManager manager = getActivity().getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, fObj);
                ft.commit();
                */
            }
        });
    }   

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d("Log", "onResume");

    }
}