我创建了一个包含4个CardView
的XML,其中包含4个RadioButton
和其他布局,如下所示:
fragment_list_of_transport.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:id="@+id/parent_card_1"
android:layout_width="0dp"
android:background="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginEnd="@dimen/_5sdp"
app:cardCornerRadius="@dimen/_5sdp"
app:cardElevation="@dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view_1"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/image_truck_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/image_truck_1" />
<ImageView
android:id="@+id/image_truck_1"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="@+id/title_text_view_1"
app:layout_constraintStart_toEndOf="@+id/radio_button_1"
app:layout_constraintTop_toTopOf="@+id/title_text_view_1"
app:srcCompat="@drawable/ic_truck" />
<TextView
android:id="@+id/title_text_view_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/image_truck_1"
app:layout_constraintTop_toBottomOf="@+id/image_view_1" />
<Button
android:layout_width="@dimen/_90sdp"
android:layout_height="@dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_round_colors"
android:paddingLeft="@dimen/_10sdp"
android:paddingRight="@dimen/_10sdp"
android:text="@string/button_next"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text_view_1"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/parent_card_2"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginEnd="@dimen/_5sdp"
app:cardCornerRadius="@dimen/_5sdp"
app:cardElevation="@dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/parent_card_1">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view_2"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/image_truck_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/image_truck_2" />
<ImageView
android:id="@+id/image_truck_2"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="@+id/title_text_view_2"
app:layout_constraintStart_toEndOf="@+id/radio_button_2"
app:layout_constraintTop_toTopOf="@+id/title_text_view_2"
app:srcCompat="@drawable/ic_truck" />
<TextView
android:id="@+id/title_text_view_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/image_truck_2"
app:layout_constraintTop_toBottomOf="@+id/image_view_2" />
<Button
android:layout_width="@dimen/_90sdp"
android:layout_height="@dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_round_colors"
android:paddingLeft="@dimen/_10sdp"
android:paddingRight="@dimen/_10sdp"
android:text="@string/button_next"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text_view_2"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:id="@+id/parent_card_3"
android:layout_width="0dp"
android:background="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginEnd="@dimen/_5sdp"
app:cardCornerRadius="@dimen/_5sdp"
app:cardElevation="@dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/parent_card_2">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view_3"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/image_truck_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/image_truck_3" />
<ImageView
android:id="@+id/image_truck_3"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="@+id/title_text_view_3"
app:layout_constraintStart_toEndOf="@+id/radio_button_3"
app:layout_constraintTop_toTopOf="@+id/title_text_view_3"
app:srcCompat="@drawable/ic_truck" />
<TextView
android:id="@+id/title_text_view_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/image_truck_3"
app:layout_constraintTop_toBottomOf="@+id/image_view_3" />
<Button
android:layout_width="@dimen/_90sdp"
android:layout_height="@dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_round_colors"
android:paddingLeft="@dimen/_10sdp"
android:paddingRight="@dimen/_10sdp"
android:text="@string/button_next"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text_view_3"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:id="@+id/parent_card_4"
android:background="?attr/selectableItemBackground"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_5sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginEnd="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_10sdp"
app:cardCornerRadius="@dimen/_5sdp"
app:cardElevation="@dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/parent_card_3">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view_4"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="@+id/radio_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/image_truck_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/image_truck_4" />
<ImageView
android:id="@+id/image_truck_4"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="@+id/title_text_view_4"
app:layout_constraintStart_toEndOf="@+id/radio_button_4"
app:layout_constraintTop_toTopOf="@+id/title_text_view_4"
app:srcCompat="@drawable/ic_truck" />
<TextView
android:id="@+id/title_text_view_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/image_truck_4"
app:layout_constraintTop_toBottomOf="@+id/image_view_4" />
<Button
android:layout_width="@dimen/_90sdp"
android:layout_height="@dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/background_round_colors"
android:paddingLeft="@dimen/_10sdp"
android:paddingRight="@dimen/_10sdp"
android:text="@string/button_next"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text_view_4"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</layout>
输出:
我想同时点击RadioButton
和CardView
来管理RadioButton
,我使用了以下代码:
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentListTransport extends Fragment implements View.OnClickListener {
FragmentListOfTransportBinding mBinding;
public FragmentListTransport() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_list_of_transport, container, false);
mBinding.parentCard1.setOnClickListener(this);
mBinding.parentCard2.setOnClickListener(this);
mBinding.parentCard3.setOnClickListener(this);
mBinding.parentCard4.setOnClickListener(this);
return mBinding.getRoot();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.parent_card_1:
mBinding.radioButton1.setChecked(true);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(false);
break;
case R.id.parent_card_2:
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(true);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(false);
break;
case R.id.parent_card_3:
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(true);
mBinding.radioButton4.setChecked(false);
break;
case R.id.parent_card_4:
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(true);
break;
}
}
}
是否有最佳的管理方式?
答案 0 :(得分:0)
然后,我不是最简单的方法(如果您有简单的方法,那么我会接受的。):
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentListTransport extends Fragment implements View.OnClickListener {
FragmentListOfTransportBinding mBinding;
public FragmentListTransport() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_list_of_transport, container, false);
mBinding.parentCard1.setOnClickListener(this);
mBinding.parentCard2.setOnClickListener(this);
mBinding.parentCard3.setOnClickListener(this);
mBinding.parentCard4.setOnClickListener(this);
mBinding.radioButton1.setOnClickListener(this);
mBinding.radioButton2.setOnClickListener(this);
mBinding.radioButton3.setOnClickListener(this);
mBinding.radioButton4.setOnClickListener(this);
return mBinding.getRoot();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.parent_card_1:
case R.id.radio_button_1:
clickedRadioButton(mBinding.radioButton1);
break;
case R.id.parent_card_2:
case R.id.radio_button_2:
clickedRadioButton(mBinding.radioButton2);
break;
case R.id.parent_card_3:
case R.id.radio_button_3:
clickedRadioButton(mBinding.radioButton3);
break;
case R.id.parent_card_4:
case R.id.radio_button_4:
clickedRadioButton(mBinding.radioButton4);
break;
}
}
private void clickedRadioButton(RadioButton radioButton) {
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(false);
radioButton.setChecked(true);
}
}
如果有人最简单的方法,那么请发布答案。我会接受的。