在Scroll视图中添加Dynamic RadioButtons

时间:2016-08-19 10:23:01

标签: android

我是Android编程新手。 我想在Android应用程序内的滚动视图内创建动态单选按钮单选按钮组。 无线电组具有仅以水平方式显示的限制。但我想要的是像矩阵一样的单选按钮模式。

下面的图片将为您提供我想要达到的目标的线索 第一张图是我能做的。 第二张图是我想要实现的。

enter image description here

与此相关的另一个挑战是,当选中一个时,单选按钮必须取消选中。 (假设它们会在不同的广播组中显示。)

4 个答案:

答案 0 :(得分:1)

在xml布局中添加

        <LinearLayout
            android:id="@+id/linear1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">

            <RadioGroup
                android:id="@+id/radiogroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:orientation="vertical" />
        </LinearLayout>

并将此添加到您希望拥有单选按钮的“活动”中

RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup);
RadioButton[] rb = new RadioButton[items.size()];
for (int i = 0; i < items.size(); i++) {
    rb[i] = new RadioButton(this);
    rg.addView(rb[i]);
    rb[i].setText(items.get(i).getName());
}

答案 1 :(得分:1)

使用此

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioGroup android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/radioGroup">

     </RadioGroup>
</LinearLayout>

您可以添加动态单选按钮,如下所示

    RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
    for (int i=0;i<10,i++) {
        RadioButton radioButton = new RadioButton(getBaseContext());
        radioGroup.addView(radioButton);
    }

答案 2 :(得分:1)

以下功能执行所需的功能: -

/**
 *Returns a linear layout containing radio buttons in nX2 matrix fashion
 * where n is Math.Ceil(iRadioBtnDs.size/2)
 *
 * @param iRadioBtnInfoDs Data Structure containing RadioBtnInformation..
 *                        Contains id and label of the radio buttons.
 * @param iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs Currently checked radio button's index
 *
 * @return returns the linear layout containing the generated nX2 radio buttons.
 */
public LinearLayout getLinearLayoutContainingRadioButtonInNX2MatrixFashion(Context iContext,ArrayList<DynamicRadioBtnDs> iRadioBtnInfoDs ,int iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs){


    final ArrayList<DynamicRadioBtnDs> loanTypeRadioBtns     =      iRadioBtnInfoDs;

    float halfOfRadioCount                                   =      (float) iRadioBtnInfoDs.size() / 2;

    int noOfRadioGroupsNeeded                                =      (int) Math.ceil(halfOfRadioCount);

    LinearLayout radioGrpParentLl                            =      new LinearLayout(iContext);
    LinearLayout.LayoutParams paramsRadioGrpParentLl         =      new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    radioGrpParentLl.setLayoutParams(paramsRadioGrpParentLl);
    radioGrpParentLl.setOrientation(LinearLayout.VERTICAL);

    for (int i = 0; i < noOfRadioGroupsNeeded; i++) {

        final int indexOfCurrentRadioGrpInParentLl = i;

        //Creating i th radio group...

        final RadioGroup radioGroupCurrentInteration                 =       new RadioGroup(iContext);
        RadioGroup.LayoutParams paramsRadio         =       new RadioGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        radioGroupCurrentInteration.setOrientation(RadioGroup.HORIZONTAL);
        radioGroupCurrentInteration.setLayoutParams(paramsRadio);

        //Creating 2*i th radio button...
        //This will always be in the left...

        RadioButton leftRadioButtonInCurrentRadioGroup = new RadioButton(iContext);
        RadioGroup.LayoutParams paramsLeftRadioBtnInCurrentRadioGroup = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);

        String leftRadionBtnLabel = loanTypeRadioBtns.get(2 * i).getLabel();
        leftRadioButtonInCurrentRadioGroup.setText(leftRadionBtnLabel);
        leftRadioButtonInCurrentRadioGroup.setLayoutParams(paramsLeftRadioBtnInCurrentRadioGroup);

        radioGroupCurrentInteration.addView(leftRadioButtonInCurrentRadioGroup);
        paramsLeftRadioBtnInCurrentRadioGroup.weight = 1f;
        leftRadioButtonInCurrentRadioGroup.setTypeface(Typeface.DEFAULT_BOLD);

        class CustomCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

            private int indexOfCurrentlyCheckedRbInRadioBtnInfoDs;

            CustomCheckedChangeListener(int iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs) {
                indexOfCurrentlyCheckedRbInRadioBtnInfoDs       =  iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs;
            }

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    indexOfCurrentlyCheckedRbInRadioBtnInfoDs   = indexOfCurrentRadioGrpInParentLl * 2;
                }
            }
        }

        CustomCheckedChangeListener checkChangedListenerForLeftRb = new CustomCheckedChangeListener(iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs);

        leftRadioButtonInCurrentRadioGroup.setOnCheckedChangeListener(checkChangedListenerForLeftRb);



        //Right radio button may or may not be required..
        //(i+1) represents the level starting from 1....
        //For example i+1 is 5 means we are now in the 5th iteration
        //ie 5th radio group...

        //If we need right radio button,
        //The size will be equal to no of radio groups*2

        if((i+1)*2 <=loanTypeRadioBtns.size()) {
            //The right radio group is needed

            RadioButton rightRadioButtonInCurrentRadioGroup                     =   new RadioButton(iContext);
            RadioGroup.LayoutParams paramsRightRadioBtnInCurrentRadioGroup      =   new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            rightRadioButtonInCurrentRadioGroup.setLayoutParams(paramsLeftRadioBtnInCurrentRadioGroup);
            paramsRightRadioBtnInCurrentRadioGroup.weight                       =   1f;
            rightRadioButtonInCurrentRadioGroup.setTypeface(Typeface.DEFAULT_BOLD);
            String rightRadioBtnLabel                                           =   loanTypeRadioBtns.get((2 * i) + 1).getLabel();
            rightRadioButtonInCurrentRadioGroup.setText(rightRadioBtnLabel);
            radioGroupCurrentInteration.addView(rightRadioButtonInCurrentRadioGroup);


            class CustomCheckedChangeListenerForRightRb implements CompoundButton.OnCheckedChangeListener {

                private int indexOfCurrentlyCheckedRbInRadioBtnInfoDs;

                CustomCheckedChangeListenerForRightRb(int iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs) {
                    indexOfCurrentlyCheckedRbInRadioBtnInfoDs       =  iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs;
                }

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        indexOfCurrentlyCheckedRbInRadioBtnInfoDs   = (indexOfCurrentRadioGrpInParentLl * 2)+1;
                    }
                }
            }

            CustomCheckedChangeListenerForRightRb checkChangedListenerForRightRb = new CustomCheckedChangeListenerForRightRb(iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs);

            rightRadioButtonInCurrentRadioGroup.setOnCheckedChangeListener(checkChangedListenerForRightRb);


        }

        radioGroupCurrentInteration.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                RadioButton checkChangedRadioBtn = (RadioButton) group.findViewById(checkedId);
                if (checkChangedRadioBtn.isChecked()) {
                    //Some radio button in currentradioGrpParentLl
                    //Radio group has been checked....
                    LinearLayout loanEmiParentLl = (LinearLayout) group.getParent();
                    for (int i = 0; i < loanEmiParentLl.getChildCount(); i++) {
                        RadioGroup childRadioGroupAtIndexI = (RadioGroup) loanEmiParentLl.getChildAt(i);
                        if (!childRadioGroupAtIndexI.equals(group)) {
                            //Means the current radio group is a sister radio group....
                            //So clear checks...
                            childRadioGroupAtIndexI.setOnCheckedChangeListener(null);
                            childRadioGroupAtIndexI.clearCheck();
                            childRadioGroupAtIndexI.setOnCheckedChangeListener(this);

                        } else {
                            //Current radio group...
                            //So do nothing...
                        }
                    }
                }
            }
        });

        radioGrpParentLl.addView(radioGroupCurrentInteration);
    }

    return  radioGrpParentLl;

}







 public class DynamicRadioBtnDs {

    DynamicRadioBtnDs(String iId,String iLabel){
        id      =   iId;
        label   =   iLabel;
    }

    private String id;
    private String label;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
}

如果他们不打算处理单选按钮的id,用户可以将DynamicRadioBtnDs更改为String。

答案 3 :(得分:0)

您可以动态添加任何视图到 Scrollview ,例如:

layoutInflater = LayoutInflater.from(this);
layoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
layoutParam.setMargins(10, 0, 10, 10);

for (int i = 0; i < 10; i++) {               
    View view = layoutInflater.inflate(R.layout.deals_style, null);
    ((TextView) deals.findViewById(R.id.tv_product_name))
        .setText("my text")

        layoutNewDeals.addView(deals, layoutParam);
}

TextView 替换为单选按钮

您必须创建自定义布局文件或在运行时创建单选按钮