Android 动态生成 radiobutton 解决 radiogroup 多行显示的问题 单选

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16624353/article/details/69791319
项目有需求,然后看了下网上的方法,好多都太麻烦了,而且也不怎么靠谱。。


先上效果图



先上布局的代码

  <RadioGroup
                android:id="@+id/radio_group_directory_filter_game"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="3"
                android:layout_below="@id/text_title_game"/>

然后是java代码
 public void setGameRadioButton(RadioGroup gameRadioGroup){
        Gson gson = new Gson();
        List<Group> allGroups = new ArrayList<>();
        allGroups.add(new Group("Dota2","Dota2"));
        allGroups.add(new Group("LOL","LOL"));
        allGroups.add(new Group("守望先锋","守望先锋"));
        allGroups.add(new Group("王者荣耀","王者荣耀"));
        float density = getResources().getDisplayMetrics().density;
        RadioGroup.LayoutParams radioParams = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
        int radioMargin = (int) (4 * density);
        radioParams.setMargins(0, radioMargin, 16, radioMargin);
        int i = 0;
        while (i < allGroups.size()) {
            int j = 0;
            LinearLayout ll = new LinearLayout(getActivity());
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ll.setWeightSum(3.0f);
            while (j < 3 && i < allGroups.size()) {
                ll.addView(getGameGroupRadio(allGroups.get(i),i), radioParams);
                i++;
                j++;
            }
            gameRadioGroup.addView(ll);
        }

    }
 private RadioButton getGameGroupRadio(Group group,int position) {
        RadioButton btn = (RadioButton) getActivity().getLayoutInflater().inflate(R.layout.radio_directory_sidebar, null);
        btn.setText(group.getName());
        switch (position){
            case 0:
                DOTA2_ID = btn;
                break;
            case 1:
                LOL_ID = btn;
                break;
            case 2:
                OVERWATCH_ID = btn;
                break;
            case 3:
                KINGGLORY_ID = btn;
                break;
        }
        btn.setOnClickListener(new View.OnClickListener() {//单独给每个生成的radiobutton生成点击事件,我是通过position来分辨不同radiobutto的,原因是,我通过对radiogroup再来监听radiobutton失败了,反正监听没反应,所以只能这么干了
            @Override
            public void onClick(View view) {
                for (int i = 0; i < mRadioGame.getChildCount(); i++) {
                LinearLayout row = (LinearLayout) mRadioGame.getChildAt(i);
                for (int j = 0; j < row.getChildCount(); j++) {
                    RadioButton radio = (RadioButton) row.getChildAt(j);
                    radio.setChecked(false);
                }
            }
            switch (position){
                case 0:
                    DOTA2_ID.setChecked(true);
                    mDirectoryOption.setGameId(Game.DOTA2_ID);
                    break;
                case 1:
                    LOL_ID.setChecked(true);
                    mDirectoryOption.setGameId(Game.LOL_ID);
                    break;
                case 2:
                    OVERWATCH_ID.setChecked(true);
                    mDirectoryOption.setGameId(Game.OVERWATCH_ID);
                    break;
                case 3:
                    KINGGLORY_ID.setChecked(true);
                    mDirectoryOption.setGameId(Game.KING_GLORY_ID);
                    break;
            }
                updateBar();
            }
        });
        return btn;
    }




猜你喜欢

转载自blog.csdn.net/qq_16624353/article/details/69791319