我在做什么:
fragmentabhost
,其中包含3-tabs
。On click
的第一个标签的RatingAscending.class
我想做什么:
Onclick
RatingDescending.class
注意:在onResume()
我能够检测到click event
firsttab
现在,当我点击RatingDescending.class
RatingAscending.class
更改为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");
}
});
}
}
答案 0 :(得分:0)
下面的代码对于实现这一目标非常重要
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");
}
}