簡體   English   中英

是否可以為另一個視圖中的元素設置可見性屬性?

[英]Is it possible to set the visibility property for an element that is in another view?

我在我的一項活動中使用了回收器視圖,並且我正在使用ArrayList填充該回收器視圖。 但是,我希望隱藏一個元素(位於布局文件example_item.xml )。 有沒有辦法做到這一點?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.circularreveal.CircularRevealRelativeLayout 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:layout_height="match_parent"
    xmlns:numberpicker="http://schemas.android.com/apk/res-auto"
    tools:context=".FlightResults">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:scrollbars="vertical">

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

</android.support.design.circularreveal.CircularRevealRelativeLayout>

我正在工作的活動的 XML 布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginBottom="4dp"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">

        <ImageView
            android:id="@+id/outbound"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="2dp"
            android:src="@drawable/ic_return"/>

        <TextView
            android:id="@+id/fromTo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/outbound"
            android:text="From - To"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fromTo"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/outbound"
            android:text="Time leaving - time arriving"
            android:textSize="15sp" />

        <ImageView
            android:id="@+id/returning"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/outbound"
            android:padding="2dp"
            android:src="@drawable/ic_outbound"/>

        <TextView
            android:id="@+id/fromTo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:layout_toEndOf="@+id/returning"
            android:layout_below="@id/time"
            android:text="From - To"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/time2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fromTo2"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/returning"
            android:text="Time leaving - time arriving"
            android:textSize="15sp" />

    </RelativeLayout>


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

我希望更改的布局(此布局中的一個元素 - id:returning)

如果視圖與您的適配器在同一個活動中,您應該將視圖傳遞給適配器的構造函數,當您想隱藏時只需隱藏它。

如果視圖在另一個活動中,您只需要創建一個回調,當該回調被調用時,您可以在該其他活動中做任何您想做的事情。

在您希望更改視圖的事件被調用的活動中添加此代碼。

private static OnTagItemClickListener onTagClickListener;

public static void setOnTagClickListener(OnTagItemClickListener onTagClick) {
        onTagClickListener = onTagClick;
    }


    public static interface OnTagItemClickListener {
        void onTagItemClickListener(Object params //in here you can pass what ever you want);
    }

當你想調用這個回調時,你就從 Activity1 開始

onTagClickListener.onTagItemClickListener(object); // pass that object to the callback

現在從您的活動中更改視圖的可見性添加此

Activity1.setOnTagClickListener(new Activity1.OnTagItemClickListener() {
            @Override
            public void onTagItemClickListener(Object params) {
                yourView.setVisibility(View.GONE);
            }
        });

這比我的例子更解釋你應該去看看。 如何在 Android 中定義回調?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM