При нажатии кнопки вид не удаляется из parentView? - PullRequest
0 голосов
/ 20 декабря 2018

Я надуваю представление из родительского представления. Это надувается нормально. Представление добавляется столько раз, сколько я хотел, но когда я хочу удалить конкретное представление, оно не удаляет это представление.пока эта кнопка работает на toasts.but, а не на удалении вида.

вот мое основное действие:

<LinearLayout 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_width="match_parent"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_height="wrap_content"
tools:context=".Activity.Reminders">

<android.support.v7.widget.Toolbar
    android:id="@+id/remindertoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reminders"
        android:textSize="17sp"
        android:layout_marginStart="20dp"/>


    <ImageView
        android:id="@+id/addreminder"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginStart="190dp"
        android:layout_marginEnd="30dp"
        android:src="@drawable/add"
        tools:ignore="ContentDescription" />


</android.support.v7.widget.Toolbar>


<ImageView
    android:id="@+id/imageView10"

    android:layout_width="157dp"
    android:layout_height="69dp"
    android:layout_marginStart="230dp"
    android:layout_marginTop="8dp"
    app:srcCompat="@drawable/addreminder1" />

<ImageView
    android:id="@+id/imageView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="132dp"
    android:layout_marginTop="134dp"
    android:layout_marginEnd="123dp"

    app:srcCompat="@drawable/alarmclock"
    tools:ignore="ContentDescription" />

<TextView
    android:id="@+id/textView68"
    android:layout_width="wrap_content"
    android:layout_height="28dp"
    android:layout_marginStart="126dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="126dp"
    android:text="@string/no_reminders_added_yet" />

А вот мое действие, которое я раздуваю:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:background="@drawable/shape2"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView
            android:id="@+id/reminder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reminder" />

        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="200dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/custom_mesaage"
            android:importantForAutofill="no"
            android:inputType="text"
            tools:targetApi="o" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="36sp" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/everyday" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioGroup
            android:id="@+id/group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:ignore="UselessParent">


            <RadioButton
                android:id="@+id/day"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/day" />

            <RadioButton
                android:id="@+id/month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/month" />


        </RadioGroup>


    </LinearLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/listofmode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/listofmode2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>



    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/deleteimage"
            android:layout_width="40dp"
            android:layout_height="40dp"
           android:background="@drawable/delete"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/deletetext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/delete" />


        <ImageView
            android:id="@+id/downimage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginStart="220dp"
            android:src="@drawable/downn"
            tools:ignore="ContentDescription" />
        <ImageView
            android:id="@+id/upimage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:layout_marginStart="20dp"
            android:src="@drawable/upp"
            tools:ignore="ContentDescription" />

    </LinearLayout>

</LinearLayout>

Я хочу, чтобы добавленное представление было удалено из родительского объекта, но его not.i добавил тост к нажатию кнопки, которую он показывает.

вот код:

 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            @SuppressLint("InflateParams") final View rowView = inflater.inflate(R.layout.reminderaddition, null);

  deleteme.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                layout.removeView((View) v.getParent());
                  Toast.makeText(getApplicationContext(), "this is a textview", Toast.LENGTH_LONG).show();
                            }
                        });
                        delete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                layout.removeView((View) v.getParent());
                                Toast.makeText(getApplicationContext(), "this is a button", Toast.LENGTH_LONG).show();
                            }
                        });

Я использую этот учебник :

Ответы [ 2 ]

0 голосов
/ 20 декабря 2018

Для удаления просмотра используйте следующий код:

layout.removeView(findViewById(VIEW_ID_YOU_WANT_TO_REMOVE));
0 голосов
/ 20 декабря 2018

Замените layout.removeView((View) v.getParent());

на layout.removeView((View) v.getParent().getParent());

, поскольку у вас есть 2-уровневая иерархия в вашем файле макета, поэтому вы должны получить корневой линейный макет, который снова является родителем вашего внутреннего линейного макета,

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...