在2个RadioGroups中选择RadioButton时显示隐藏的LinearLayouts

时间:2018-11-30 06:06:17

标签: java android android-studio

六个RadioButton在RadioGroup中对齐为3列和2行,因此在RG中为6个RB-s。如果用户选择一个RB,则在其右下方显示image1和text1,在交替的RB-s下方也显示image1和text2。用户可以选择任何一个RB。正确的行为看起来像https://photos.app.goo.gl/NEqTaxsY6daxD3kJA

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg">

        <include layout="@layout/app_bar" />

        <fragment
            android:id="@+id/refreshLayoutFragment"
            class="kz.fingram.RefreshLayoutFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:layout="@layout/fragment_refresh_layout" />

        <LinearLayout
            android:id="@+id/root"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingEnd="@dimen/activity_margin"
            android:paddingLeft="@dimen/activity_margin"
            android:paddingRight="@dimen/activity_margin"
            android:paddingStart="@dimen/activity_margin"
            android:paddingTop="@dimen/activity_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/when_u_want_money"
                    android:textAppearance="@style/TextAppearance.Medium" />

                <RadioGroup
                    android:id="@+id/months"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/primary_transparent_round_bg"
                    android:orientation="horizontal"
                    app:setOnCheckedChangeListener="@{viewModel.mMonthOnCheckedChangeListener}" />

                <LinearLayout
                    android:id="@+id/thisIs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:orientation="horizontal" />

                <RadioGroup
                    android:id="@+id/months2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/primary_transparent_round_bg"
                    android:orientation="horizontal"
                    app:setOnCheckedChangeListener="@{viewModel.mMonthOnCheckedChangeListener2}" />

                <LinearLayout
                    android:id="@+id/thatIs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:orientation="horizontal" />

             <Button
                style="@style/Button.Primary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:enabled="@{viewModel.mIsNextEnabled}"
                android:onClick="@{viewModel::nextOnClick}"
                android:text="@string/next" />
        </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Java代码:

RadioGroup rg1 = findViewById(R.id.months);
rg1.setOnCheckedChangeListener(null);
rg1.clearCheck();                  
rg1.setOnCheckedChangeListener(mMonthOnCheckedChangeListener);
View view = radioGroup.findViewById(i);
if (view != null) {
mMonth = (TeamFreeMonth) view.getTag();
}
selectMonth();
checkButtonsState();


public final class OptionsActivity extends BaseAppCompatActivity {
    private ActivityOptionsBinding mBinding;
public static void show(@NonNull final Context ctx) {
    StorageHelper.getInstance().setInvited(false);
    BaseAppCompatActivity.show(ctx, OptionsActivity.class, true, false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_options);
    setupToolbar();
    mBinding.setViewModel(new ViewModel());
}

public final class ViewModel extends BaseObservable {
    private static final int TERM_6_MONTH_ID = 1;
    private static final int INSTALLMENT_10000_ID = 1;
    private final RefreshLayoutFragment mRefreshLayout;
    public boolean mIsNextEnabled;
    public TeamFreeMonth mMonth;
    private TeamFreeMonth[] mMonths;

    ViewModel() {
        mMonth = null;
        mMonths = null;

        mRefreshLayout = (RefreshLayoutFragment) getSupportFragmentManager()
                .findFragmentById(R.id.refreshLayoutFragment);
        mRefreshLayout.setup(mBinding.root, new RefreshLayoutFragment.Callback() {
            @Override
            public void reload() {
                try {
                    loadData();
                } catch (Exception e) {
                    ExceptionHelper.displayException(OptionsActivity.this, e);
                }
            }
        });


    public RadioGroup.OnCheckedChangeListener mMonthOnCheckedChangeListener =
            new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                    if (i != -1) {
                        RadioGroup rg2 = findViewById(R.id.months2);
                        rg2.setOnCheckedChangeListener(null);
                        rg2.clearCheck();
                        rg2.setOnCheckedChangeListener(mMonthOnCheckedChangeListener2);
                        View view = radioGroup.findViewById(i);
                        if (view != null) {
                            mMonth = (TeamFreeMonth) view.getTag();
                        }
                        selectMonth();
                        checkButtonsState();
                    }
                }
            };

    public RadioGroup.OnCheckedChangeListener mMonthOnCheckedChangeListener2 =
            new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                    if (i != -1) {
                        RadioGroup rg1 = findViewById(R.id.months);
                        rg1.setOnCheckedChangeListener(null);
                        rg1.clearCheck();
                        rg1.setOnCheckedChangeListener(mMonthOnCheckedChangeListener);
                        View view = radioGroup.findViewById(i);
                        if (view != null) {
                            mMonth = (TeamFreeMonth) view.getTag();
                        }
                        selectMonth();
                        checkButtonsState();
                    }
                }
            };
    private void refreshMonths() {
        mBinding.months.removeAllViews();
        mBinding.months.clearCheck();
        mBinding.months2.removeAllViews();
        mBinding.months2.clearCheck();

        mBinding.thisIs.removeAllViews();
        mBinding.thatIs.removeAllViews();

        final TeamFreeMonth oldMonth = mMonth;

        mMonth = null;

        checkButtonsState();


        if (mMonths == null) {
            return;
        }

        final TeamFreeMonth[] months = mMonths;

        if (months.length == 0) {
            return;
        }

        int halfMonths = months.length / 2;
        for (int i = 0; i < halfMonths; i++) {
            final String date = UtilitiesHelper.dateToStr(months[i].getDate(), Constants.DATE_FORMAT_LLLL);// Получили месяц от даты
            final String date2 = UtilitiesHelper.dateToStr(months[i + halfMonths].getDate(), Constants.DATE_FORMAT_LLLL);// Получили месяц от даты
            if (TextUtils.isEmpty(date) || TextUtils.isEmpty(date2)) {
                continue;
            }
            final RadioButton rb = (RadioButton) LayoutInflater.from(OptionsActivity.this).inflate(R.layout.radio_button_tab, mBinding.months, false);
            rb.setId(i);
            rb.setTag(months[i]);
            final SpannableString btnCaption = new SpannableString(date);
            btnCaption.setSpan(new AbsoluteSizeSpan(getResources().getDimensionPixelSize(R.dimen.font_size_18)), 0, date.length(), 0);
            rb.setText(btnCaption);

            final RadioButton rb2 = (RadioButton) LayoutInflater.from(OptionsActivity.this).inflate(R.layout.radio_button_tab, mBinding.months2, false);
            rb2.setId(i + halfMonths);
            rb2.setTag(months[i + halfMonths]);
            final SpannableString btnCaption2 = new SpannableString(date2);
            btnCaption2.setSpan(new AbsoluteSizeSpan(getResources().getDimensionPixelSize(R.dimen.font_size_18)), 0, date2.length(), 0);
            rb2.setText(btnCaption2);

            if (i == 0) {
                rb.setBackgroundResource(R.drawable.ll_radio_button_start);
                rb2.setBackgroundResource(R.drawable.ll_radio_button_start);
            } else if (i == halfMonths - 1) {
                rb.setBackgroundResource(R.drawable.ll_radio_button_end);
                rb2.setBackgroundResource(R.drawable.ll_radio_button_end);
            }
            mBinding.months.addView(rb);
            mBinding.months2.addView(rb2);

            TextView thisIs = (TextView) LayoutInflater.from(OptionsActivity.this).inflate(R.layout.options_this_is_item, mBinding.thisIs, false);
            thisIs.setId(rb.getId());
            thisIs.setText(getString(R.string.thisIs));
            thisIs.setVisibility(View.INVISIBLE);
            mBinding.thisIs.addView(thisIs);

            TextView thatIs = (TextView) LayoutInflater.from(OptionsActivity.this).inflate(R.layout.options_that_is_item, mBinding.thatIs, false);
            thatIs.setId(rb2.getId());
            thatIs.setText(getString(R.string.thatIs));
            thatIs.setVisibility(View.INVISIBLE);
            mBinding.thatIs.addView(thatIs);

            if (oldMonth != null && UtilitiesHelper.isDateEquals(oldMonth.getDate(), months[i].getDate())) {
                mBinding.months.check(rb.getId());
                mBinding.months2.check(rb2.getId());
            }
        }
        selectMonth();
    }

    private void selectMonth()
    {
        int selectedId1 = mBinding.months.getCheckedRadioButtonId();
        int selectedId = selectedId1 !=-1 ? selectedId1 : mBinding.months2.getCheckedRadioButtonId();
        int n = mBinding.months.getChildCount();
        List<TextView> views = new ArrayList<TextView>();

        for (int i = 0; i < n; i++) {
            views.add(i, (TextView) mBinding.thisIs.getChildAt(i));
            views.add(i+3, (TextView) mBinding.thatIs.getChildAt(i));
        }

        int length = views.size();
        for (int i = 0; i < length; i++) {
            TextView child = views.get(i);
        }

    private void checkButtonsState() {
        mIsNextEnabled = mMonth != null;
        notifyChange();
    }

    public void nextOnClick(final View view) {
        try {

            final TeamFreeMonth[] months = mMonths;

            if (mMonth == null || months == null || months.length == 0) {
                throw new Exception(getString(R.string.enter_month));
            }

            SettingsHelper.setTermId(OptionsActivity.this, TERM_6_MONTH_ID);//
            SettingsHelper.setTermRateId(OptionsActivity.this, mMonth.getTermRateId());//
            SettingsHelper.setInstallmentId(OptionsActivity.this, INSTALLMENT_10000_ID);
            SettingsHelper.setGoalDateInMillis(OptionsActivity.this, mMonth.getDate().getTime());//дата получения
            SettingsHelper.setFirstGoalDateInMillis(OptionsActivity.this, months[0].getDate().getTime());
            SettingsHelper.setGoalSum(OptionsActivity.this, mMonth.getSum());

            BaseAppCompatActivity.show(OptionsActivity.this, SetGoalActivity.class, true, false);
        } catch (Exception e) {
            ExceptionHelper.displayException(OptionsActivity.this, e);
        }
    }

    private void loadData() {
        try {
            final AsyncTask<Void, Void, TeamFreeMonth[]> task = new AsyncTask<Void, Void, TeamFreeMonth[]>() {
                private Exception mError = null;

                @Override
                protected void onPreExecute() {
                    mRefreshLayout.setRefreshing(true);
                }

                @Override
                protected TeamFreeMonth[] doInBackground(Void... params) {
                    try {
                        TeamFreeMonth[] months = ServerHelper.getInstance().syncGetOptionMonths(OptionsActivity.this, TERM_6_MONTH_ID, INSTALLMENT_10000_ID);
                        if (months != null && months.length > 0)
                        return months;
                    } catch (Exception e) {
                        mError = e;
                    }

                    return null;
                }

                @Override
                protected void onPostExecute(TeamFreeMonth[] result) {
                    try {
                        if (mError != null) {
                            throw mError;
                        }

                        mMonths = result;

                        refreshMonths();
                        checkButtonsState();
                        mRefreshLayout.setRefreshing(false);
                    } catch (Exception e) {
                        mRefreshLayout.setError(e);
                    }
                }
            };
            task.execute();
        } catch (Exception e) {
            mRefreshLayout.setError(e);
        }
    }

}

}

Image1始终相同,而text1和text2不同。文本1“您”显示在所选RB下,文本2“您的朋友”显示在交替的RB下。 问题仍然是如何交替显示/隐藏image1和text 1或text 2? 我可以在同一列的RB-s下显示/隐藏image1和text-s,但不能交替显示RB-s。

0 个答案:

没有答案