setOnCheckedChangeListener方法参数类型不匹配
ackage gke_code_jrxd.Demo.ui;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import gke_code_jrxd.Demo.BuildConfig;
import gke_code_jrxd.Demo.R;
import gke_code_jrxd.Demo.ui.index.FunnyFragment;
import gke_code_jrxd.Demo.ui.index.IndexFragment;
import gke_code_jrxd.Demo.ui.index.NewsFragment;
import gke_code_jrxd.Demo.ui.widget.TitleBar;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends FragmentActivity {
@Bind(R.id.titleBar)
TitleBar titleBar;
@Bind(R.id.rgTabBar)
RadioButton rgTabBar;
@Bind(R.id.rbTabIndex)
RadioButton rbTabIndex;
@Bind(R.id.rbTabNews)
RadioButton rbTabNews;
@Bind(R.id.rbTabFunny)
RadioButton rbTabFunny;
IndexFragment mIndexFragment = new IndexFragment();
NewsFragment mNewsFragment = new NewsFragment();
FunnyFragment mFunnyFragment = new FunnyFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
titleBar.setTitle("首页");
titleBar.setLeftTextLeftDrawable("", 0);
addFragment();
rgTabBar.setOnCheckedChangeListener(mOnCheckedChangeListener);
// rgTabBar.check(R.id.rbTabIndex);
// 在这里为什么不用 check(R.id.rbTabIndex)默认选中第一项,是因为y
// mOnCheckedChangeListener事件会执行多次,所以弃用它,
// 直接让第一个RadioButton设置选中解决该问题
rbTabIndex.setChecked(true);
}
/**
* 首页tab索引
**/
final int TAB_INDEX = 0;
/**
* 今日新闻tab索引
**/
final int TAB_NEWS = 1;
/**
* 今日笑点tab索引
**/
final int TAB_FUNNY = 2;
/**
* 当前展示的tab索引
**/
int mCurShowFragmentIndex;
/**
* tab选项卡监听器
*/
RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void OnCheckedChanged(RadioGroup group,int checkedId) {
switch (checkedId) {
case R.id.rbTabIndex:
changeTab(TAB_INDEX);
break;
case R.id.rbTabNews:
changeTab(TAB_NEWS);
break;
case R.id.rbTabFunny:
changeTab(TAB_FUNNY);
break;
}
}
};
public void addFragment() {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.flContent, mIndexFragment);
transaction.add(R.id.flContent, mNewsFragment);
transaction.add(R.id.flContent, mFunnyFragment);
transaction.commit();
showFragment(TAB_INDEX);
}
/**
* 切换 tab选项卡
* @param tabItem
*/
public void changeTab(final int tabItem) {
switch (tabItem) {
case TAB_INDEX:
titleBar.setTitle("首页");
mCurShowFragmentIndex = TAB_INDEX;
showFragment(TAB_INDEX);
break;
case TAB_NEWS:
titleBar.setTitle("今日新闻");
mCurShowFragmentIndex = TAB_NEWS;
showFragment(TAB_NEWS);
break;
case TAB_FUNNY:
titleBar.setTitle("今日笑点");
mCurShowFragmentIndex = TAB_FUNNY;
showFragment(TAB_FUNNY);
break;
}
//showFragment(mCurShowFragmentIndex);
}
/**
* hide所有的fragment
*/
private void hideFragment() {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(mIndexFragment);
transaction.hide(mNewsFragment);
transaction.hide(mFunnyFragment);
transaction.commit();
}
/**
* show选中的fragment
*
* @param tab
*/
public void showFragment(final int tab) {
hideFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
switch (tab) {
case TAB_INDEX:
transaction.show(mIndexFragment);
break;
case TAB_NEWS:
transaction.show(mNewsFragment);
break;
case TAB_FUNNY:
transaction.show(mFunnyFragment);
break;
}
transaction.commit();
}
}