Android замена вида другим видом, но с сохранением оригинальных ограничений - PullRequest
1 голос
/ 27 февраля 2020

Итак, у меня есть вид, который я хочу заменить другим видом. Пока я это сделал.

View child = fragmentView.findViewById(R.id.view1);
ViewGroup parent = (ViewGroup) child.getParent();
int index = parent.indexOfChild(child);
parent.removeView(child);
child = new MyCustomView(this.getContext());
parent.addView(child, index);

Пока это работает. Он также удаляет ограничения, поэтому новый вид заканчивается в верхнем левом углу. Кажется, я не могу найти способ сохранить ограничения, наложенные на исходное представление, и позволить новому представлению использовать то же самое.

Обновление:

Я попытался со следующим

View child = fragmentView.findViewById(R.id.view1);
ViewGroup.LayoutParams childParams = child.getLayoutParams();
ViewGroup parent = (ViewGroup) child.getParent();
int index = parent.indexOfChild(child);
parent.removeView(child);
child = new MyCustomView(this.getContext());
parent.addView(child, index,childParams);

Кажется, он находится на правильном пути, но еще не совсем там.

XML:

я включаю свой пользовательский вид в мои фрагменты xml следующим образом :

    <include
        android:id="@+id/view1"
        layout="@layout/my_custom_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/button5"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

my_custom_view XML выглядит следующим образом

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="wrap_content" android:layout_width="match_parent">

        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="B1"
            android:textSize="30sp"
            app:layout_constraintBottom_toTopOf="@+id/timeLeftText"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/peopleText" />

        <TextView
            android:id="@+id/peopleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0/5"
            android:textSize="18sp"
            app:layout_constraintBottom_toTopOf="@+id/button"
            app:layout_constraintEnd_toEndOf="@+id/button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/timeLeftText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="19:30"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginStart="16dp"
            android:src="@mipmap/ic_launcher_round"
            app:layout_constraintStart_toEndOf="@+id/button"
            app:layout_constraintTop_toTopOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

Предполагается, что это представление завышено в MyCustomView. java Однако код никогда не запускает MyCustomView. Java только XML.

MyCustomView. java выглядит следующим образом.

public class MyCustomView extends ConstraintLayout {

    public MyCustomView(@NonNull Context context) {
        super(context);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.my_custom_view, this, true);
    }

}

1 Ответ

0 голосов
/ 27 февраля 2020

Вы можете установить оба вида в XML и из вашей деятельности / фрагмента / независимо от того, установите соответствующую видимость

view1.setVisibility(View.VISIBLE)
view2.setVisibility(View.GONE)
...