Fragment


转载时请记得标明源地址: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,显示下一个
}

主要 实现的  方法就是我上面说的  那三个方法




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值