У меня есть родительский вид и дочерний вид. Дочерние представления раздуваются программно. Дочернее представление имеет TextView
с некоторым фоном. На TextView
есть событие onclick. Что я хочу, так это то, что когда пользователь щелкает первый TextView, его цвет фона должен измениться, а когда пользователь выбирает второй, фон первого текстового представления должен вернуться к своему фону по умолчанию, а второй должен измениться.
Я изменил цвет фона, но у меня возникли трудности с восстановлением фона. Вот мой код:
Мой родительский взгляд:
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/viewCategoriesLinearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginStart="@dimen/_5sdp" />
Мой детский вид:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/categoryChip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/category_tag_background"
android:text="TAG"
android:layout_marginTop="@dimen/_25sdp"
android:paddingLeft="@dimen/_5sdp"
android:paddingTop="@dimen/_5sdp"
android:paddingBottom="@dimen/_5sdp"
android:paddingRight="@dimen/_5sdp"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginRight="@dimen/_5sdp"
android:textColor="@color/textcolorLogin"
android:textSize="@dimen/_11ssp" />
<TextView
android:id="@+id/categorychipid"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"/>
</LinearLayout>
Вот как я надуваю и меняю цвет фона:
FlexboxLayout categoryInformationHeader = view.findViewById(R.id.viewCategoriesLinearlayout);
final View editChildView = getActivity().getLayoutInflater().inflate(R.layout.tag_layout, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,2,10,2);
editChildView.setLayoutParams(layoutParams);
editParentLL.addView(editChildView);
final TextView editTvChip = editChildView.findViewById(R.id.chip12345);
final String shelfShareCategoryTitle = crsEditShelfShare.getString(crsEditShelfShare.getColumnIndex("title"));
editTvChip.setText(shelfShareCategoryTitle);
editTvChip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editTvChip.setBackgroundResource(R.color.colorPrimary);
}
});
onClickListener
изeditTvChip
. - person Tenten Ponce   schedule 01.01.2018