轉載時請記得標明源地址:http://my.oschina.net/lijindou/blog/718799
本人博客地址: http://my.oschina.net/lijindou/blog
下面 是我的效果圖
這個 就是效果圖
下面的 mainactivity 的 代碼
import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.content.ContextCompat; import android.widget.RadioButton; import android.widget.RadioGroup; import java.util.ArrayList; import java.util.List; public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener { /** * 按鈕的沒選中顯示的圖標 * <p> */ private int[] unselectedIconIds = {R.mipmap.zimo, R.mipmap.ziyue, R.mipmap.luyin, R.mipmap.zhibo, R.mipmap.me}; /** * 按鈕的選中顯示的圖標 */ private int[] selectedIconIds = {R.mipmap.zimo1, R.mipmap.ziyue1, R.mipmap.luyin1, R.mipmap.zhibo1, R.mipmap.me1}; //存放fragment的集合 private List<Fragment> fragments = new ArrayList<Fragment>(); private RadioGroup rg_main; private Main_zimo_fragment zimoFragment; private Main_me_fragment meFragment; private RadioButton rb_main_zimo; private RadioButton rb_main_me; // 用來存放RadioButton的集合 private List<RadioButton> radioButtons = new ArrayList<>(); private RadioButton rb_main_ziyue; private RadioButton rb_main_translate; private RadioButton rb_main_channel; private Main_ziyue_fragment ziyueFragment; private Main_translate_fragment translateFragment; private Main_channel_fragment channelFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg_main = (RadioGroup) findViewById(R.id.rg_main); // Android單選鈕的點擊事件 rg_main.setOnCheckedChangeListener(this); rb_main_zimo = (RadioButton) findViewById(R.id.rb_main_zimo); rb_main_ziyue = (RadioButton) findViewById(R.id.rb_main_ziyue); rb_main_translate = (RadioButton) findViewById(R.id.rb_main_translate); rb_main_channel = (RadioButton) findViewById(R.id.rb_main_channel); rb_main_me = (RadioButton) findViewById(R.id.rb_main_me); initData(); initFragment(); } private void initData() { // 將按鈕添加到集合中去 radioButtons.add(rb_main_zimo); radioButtons.add(rb_main_ziyue); radioButtons.add(rb_main_translate); radioButtons.add(rb_main_channel); radioButtons.add(rb_main_me); } private void initFragment() { // new出各個Fragment zimoFragment = new Main_zimo_fragment(); ziyueFragment = new Main_ziyue_fragment(); translateFragment = new Main_translate_fragment(); channelFragment = new Main_channel_fragment(); meFragment = new Main_me_fragment(); // 將其添加到一個list集合中 fragments.add(zimoFragment); fragments.add(ziyueFragment); fragments.add(translateFragment); fragments.add(channelFragment); fragments.add(meFragment); // 設置默認顯示的碎片 // 創建一個碎片管理器 FragmentManager manager = getSupportFragmentManager(); // 創建一個事物 FragmentTransaction transaction = manager.beginTransaction(); // 將我們的碎片添加到佈局上(參數1:要顯示碎片的控件ID,參數2:要顯示的碎片) RadioButton radioButton; for (int i = 0; i < radioButtons.size(); i++) { radioButton = radioButtons.get(i); Drawable yellow = null; if (i == 0) { yellow = ContextCompat.getDrawable(MainActivity.this, selectedIconIds[i]); } else { yellow = ContextCompat.getDrawable(MainActivity.this, unselectedIconIds[i]); } yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); radioButton.setCompoundDrawables(null, yellow, null, null); if (i == 0) { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_on)); } else { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_off)); } } transaction.replace(R.id.fl_main_show, fragments.get(0)); transaction.commit(); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 創建一個碎片管理器 FragmentManager manager = getSupportFragmentManager(); // 創建一個事物 FragmentTransaction transaction = manager.beginTransaction(); Drawable yellow = null; RadioButton radioButton = null; switch (checkedId) { case R.id.rb_main_zimo:// 子墨 // 將我們的碎片添加到佈局上(參數1:要顯示碎片的控件ID,參數2:要顯示的碎片) transaction.replace(R.id.fl_main_show, fragments.get(0)); for (int i = 0; i < radioButtons.size(); i++) { radioButton = radioButtons.get(i); if (i == 0) { yellow = ContextCompat.getDrawable(MainActivity.this, selectedIconIds[i]); } else { yellow = ContextCompat.getDrawable(MainActivity.this, unselectedIconIds[i]); } yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); radioButton.setCompoundDrawables(null, yellow, null, null); if (i == 0) { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_on)); } else { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_off)); } } break; case R.id.rb_main_ziyue:// 子曰 for (int i = 0; i < radioButtons.size(); i++) { radioButton = radioButtons.get(i); if (i == 1) { yellow = ContextCompat.getDrawable(MainActivity.this, selectedIconIds[i]); } else { yellow = ContextCompat.getDrawable(MainActivity.this, unselectedIconIds[i]); } yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); radioButton.setCompoundDrawables(null, yellow, null, null); if (i == 1) { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_on)); } else { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_off)); } } transaction.replace(R.id.fl_main_show, fragments.get(1)); break; case R.id.rb_main_translate:// 翻譯 for (int i = 0; i < radioButtons.size(); i++) { radioButton = radioButtons.get(i); if (i == 2) { yellow = ContextCompat.getDrawable(MainActivity.this, selectedIconIds[i]); } else { yellow = ContextCompat.getDrawable(MainActivity.this, unselectedIconIds[i]); } yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); radioButton.setCompoundDrawables(null, yellow, null, null); if (i == 2) { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_on)); } else { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_off)); } } transaction.replace(R.id.fl_main_show, fragments.get(2)); break; case R.id.rb_main_channel:// 頻道 for (int i = 0; i < radioButtons.size(); i++) { radioButton = radioButtons.get(i); if (i == 3) { yellow = ContextCompat.getDrawable(MainActivity.this, selectedIconIds[i]); } else { yellow = ContextCompat.getDrawable(MainActivity.this, unselectedIconIds[i]); } yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); radioButton.setCompoundDrawables(null, yellow, null, null); if (i == 3) { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_on)); } else { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_off)); } } transaction.replace(R.id.fl_main_show, fragments.get(3)); break; case R.id.rb_main_me:// 我 for (int i = 0; i < radioButtons.size(); i++) { radioButton = radioButtons.get(i); if (i == 4) { yellow = ContextCompat.getDrawable(MainActivity.this, selectedIconIds[i]); } else { yellow = ContextCompat.getDrawable(MainActivity.this, unselectedIconIds[i]); } yellow.setBounds(0, 0, yellow.getMinimumWidth(), yellow.getMinimumHeight()); radioButton.setCompoundDrawables(null, yellow, null, null); if (i == 4) { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_on)); } else { radioButton.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.main_text_off)); } } transaction.replace(R.id.fl_main_show, fragments.get(4)); break; default: break; } transaction.commit(); } }
大家看看 ,我的想法 很簡單 ,就是每次 點擊 下面的選項 的時候,放一次 圖片,自己感覺 這個好 浪費 資源 的 就像問下,有沒有什麼可以優化的!!!
這是 這個 fragment 的源碼:http://pan.baidu.com/s/1i4MlZLF
補充 20160904
這個我上面的代碼在切換Fragment 的時候 是 用replace()切換的 , 切一次就new 一個 Fragment 不得不說這是一個婁的方式,這樣不僅在每次切換的時候就要new一個,很是浪費資源,並且,如果你的Fragment中有網絡請求,那麼,每切一次就要訪問網絡一次,那麼,對於用戶是一個很不好的體驗,我再網上找了這方面的資料,發現其實Fragment提供了這方面的方法
add() 如果你的 Fragment 沒有new,那麼用這個 添加
hide() 用於隱藏Fragment
show() 用於顯示
代碼沒有在現在的電腦上,在公司,等到了公司了,有時間 ,就給大家貼上!!!
20160905 補充
今天從公司將代碼考回來了,寫的DEMO 沒有找到,這個是從我的項目中扣得代碼
主要的代碼 是:
// 將我們的碎片添加到佈局上(參數1:要顯示碎片的控件ID,參數2:要顯示的碎片) if (!fragments.get(0).isAdded()) { // 先判斷是否被add過 Log.e(TAG, "onCheckedChanged:0+ "); transaction.hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).hide(fragments.get(4)).add(R.id.fl_main_show, fragments.get(0)).show(fragments.get(0)).commit(); // 隱藏當前的fragment,add下一個到Activity中 } else { Log.e(TAG, "onCheckedChanged:0 -"); transaction.hide(fragments.get(1)).hide(fragments.get(2)).hide(fragments.get(3)).hide(fragments.get(4)).show(fragments.get(0)).commit(); // 隱藏當前的fragment,顯示下一個 }
主要 實現的 方法就是我上面說的 那三個方法