问题
What I am doing:
- I have a
fragmentabhost
in a fragment that has3-tabs
in it. - On click of tabs i am able to change the fragments dynamically
On click
of first tab i am displayingRatingAscending.class
What I am trying to do:
- Now
Onclick
of the same tab I want to displayRatingDescending.class
Note: in the onResume()
I am able to detect click event
for the firsttab
Now how can I change the tab to RatingDescending.class
from RatingAscending.class
when I click the tab for the second time
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:
- I Finally found answer myself here (I refered this post), i hope this helps someone in future
The below piece of code is important to achieve this goal
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");
}
}
来源:https://stackoverflow.com/questions/23284232/how-to-change-the-body-of-tab-onclick-of-same-tab-in-fragmenttabhost