Android 动态切换底部tab按钮

身为一个Android开发人员,总是会遇到很多需求

直接上代码,代码里有注释,我使用的就是原生的radioGroup+radioButton

public class IndexActivity extends AppCompatActivity {

    private RadioGroup index_rg_bottom;
    private RadioButton index_rb_fragment_home, index_rb_fragment_living, index_rb_fragment_art, index_rb_fragment_near, index_rb_fragment_mine;
    private List buttonDatas;
    private List mySimpleTargets;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        initView();
        initRadioButtons();
        initMySimpleTargets();

        index_rg_bottom.setOnCheckedChangeListener(new myOnCheckedChangeListener());
        index_rg_bottom.check(R.id.index_rb_fragment_home);
    }

    //按钮的图片加载
    private void initLoadUrl() {
        String path = "http://benyouhuifile.it168.com/forum/201208/17/1448124i6da4zzi06lry50.jpg";
        String path2 = "https://cdn.pixabay.com/user/2014/05/07/00-10-34-2_96x96.jpg";
        if (nPosition == oPosition) {
            for (int i = 0; i < buttonDatas.size(); i++) {
                if (nPosition == i) {
                    Glide.with(this).load(path2).into(mySimpleTargets.get(i));
                } else {
                    Glide.with(this).load(path).into(mySimpleTargets.get(i));
                }
            }
        } else {
            Glide.with(this).load(path2).into(mySimpleTargets.get(nPosition));
            Glide.with(this).load(path).into(mySimpleTargets.get(oPosition));
        }
    }


    private int nPosition = 0;
    private int oPosition = 0;

    //redioGroup的监听,在radioButton改变时重新遍历底部按钮图片的加载
    class myOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.index_rb_fragment_home:
                    nPosition = 0;
                    break;
                case R.id.index_rb_fragment_living:
                    nPosition = 1;
                    break;
                case R.id.index_rb_fragment_art:
                    nPosition = 2;
                    break;
                case R.id.index_rb_fragment_near:
                    nPosition = 3;
                    break;
                case R.id.index_rb_fragment_mine:
                    nPosition = 4;
                    break;
                default:
                    nPosition = 0;
                    break;
            }
            initLoadUrl();
            oPosition = nPosition;
        }
    }

    //初始化页面
    private void initView() {
        index_rg_bottom = findViewById(R.id.index_rg_bottom);
        index_rb_fragment_home = findViewById(R.id.index_rb_fragment_home);
        index_rb_fragment_living = findViewById(R.id.index_rb_fragment_living);
        index_rb_fragment_art = findViewById(R.id.index_rb_fragment_art);
        index_rb_fragment_near = findViewById(R.id.index_rb_fragment_near);
        index_rb_fragment_mine = findViewById(R.id.index_rb_fragment_mine);

    }

    //buttonDatas添加按钮
    private void initRadioButtons() {
        buttonDatas = new ArrayList<>();
        buttonDatas.add(index_rb_fragment_home);
        buttonDatas.add(index_rb_fragment_living);
        buttonDatas.add(index_rb_fragment_art);
        buttonDatas.add(index_rb_fragment_near);
        buttonDatas.add(index_rb_fragment_mine);
    }

    private void initMySimpleTargets() {
        mySimpleTargets = new ArrayList<>();
        for (RadioButton button : buttonDatas) {
            //为每个按钮创建一个自定义SimpleTarget
            mySimpleTargets.add(new MySimpleTarget(button));
        }
    }

上面的就是java代码了,不过还少了一个自定义方法类 MySimpleTarget.class

public class MySimpleTarget extends SimpleTarget {
    private RadioButton radioButton;

    public MySimpleTarget(RadioButton radioButton) {
        this.radioButton = radioButton;
    }

    @Override
    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) {
        resource.setBounds(0, 0, 69, 69);
        radioButton.setCompoundDrawables(null, resource, null, null);
    }
}
resource.setBounds(0, 0, 69, 69);

四个参数分别代表在图片的左上角和右下角

radioButton.setCompoundDrawables(null,null,null,null);

四个参数分别代表在图片radioButton的左上右下




    

    

        

            

            

            

            

            
        
    

 

android:textColor="@drawable/index_rb_bottom_text_color_selector"

这个东西自己去写吧,网上有很多的,button的样式可以抽出来写一个style

加油吧,小菜

你可能感兴趣的:(Android)