在android中检测Tab上的Onclick事件(SherlockFragmentActivity)

时间:2014-04-19 11:52:37

标签: android actionbarsherlock

当我扩展onClick

时,如何在选项卡上检测SherlockFragmentActivity事件
  • 我的意思是说我点击评分标签.... Log msg Rating tab is clicked = 1
  • 当我再次点击评级标签时.... Log msg Rating tab is clicked = 2

注意::我在stackoverflow上检查了几个解决方案,但是当我使用sherlockFragmentActivity

时,它们都没有回答

MainActivitySherlock.java

public class MainActivitySherlock extends SherlockFragmentActivity {
    // Declare Variables
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_sorting.xml
        setContentView(R.layout.main_sorting);

        // Locate android.R.id.tabhost in main_sorting.xml
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        // Create the tabs in main_sorting.xml
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

        // Create Rating 
        mTabHost.addTab(mTabHost.newTabSpec("rating").setIndicator("Rating"), 
                FragmentRating.class, null);

        // Create Price
        mTabHost.addTab(mTabHost.newTabSpec("Price").setIndicator("Price"),
                FragmentPrice.class, null);

        // Create Distance
        mTabHost.addTab(mTabHost.newTabSpec("Distance").setIndicator("Distance"),
                FragmentDistance.class, null);

    }

    //Split ActionBar Buttons
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getSupportMenuInflater();
        menuInflater.inflate(R.menu.actionbar_sort_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }
}

如何实现这一点!

1 个答案:

答案 0 :(得分:1)

您可以尝试将setOnTabChangedListener设置为TabHost。在你的情况下,

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
   public void onTabChanged(String arg0) {
       Log.i("Tab number", "" + tabHost.getCurrentTab());
   });
}