设置RecyclerView中所有ImageView的可见性

时间:2017-03-10 11:36:15

标签: android android-layout android-recyclerview recycler-adapter android-viewholder

已解决 - https://ru.stackoverflow.com/questions/637809/Задать-setvisibility-всем-imageview-в-recyclerview-Вызов-метода-адаптера-из-акт#637817

我有使用ImageView的RV项目布局,它负责拖放操作。默认情况下,我想让它们不可见,然后点击工具栏菜单中的按钮,使可见,以便用户可以移动RV中的项目。

请告诉我如何设置RVAdapter中所有必要ImageView的可见性。 我需要通过设置主要活动的可见性来调用方法。

项目布局

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/rv_card"
        style="@style/MainCardStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <ImageView
                    android:id="@+id/station_logo"
                    android:layout_width="100dp"
                    android:layout_height="130dp"
                    android:layout_gravity="center"
                    android:layout_margin="5dp"
                    android:padding="10dp"
                    android:src="@drawable/avtoradio_logo"
                    tools:ignore="ContentDescription" />

                <View
                    android:id="@+id/separator"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/separator_color" />

                <TextView
                    android:id="@+id/station_name"
                    style="@style/MainTextViewStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Европа плюс"
                    android:textColor="@color/text_color_nav"
                    tools:ignore="RtlHardcoded" />

                <TextView
                    android:id="@+id/station_freq"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:paddingLeft="10dp"
                    android:text="105,3 FM"
                    android:textColor="@color/main_card_small_text_color"
                    android:textSize="13sp"
                    tools:ignore="RtlHardcoded" />
            </LinearLayout>


        // This ImageView I need to make visible
            <ImageView
                android:id="@+id/dragView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:layout_margin="13dp"
                android:visibility="gone"
                android:src="@drawable/ic_action_drag_album"
                tools:ignore="ContentDescription" />
        </FrameLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

ViewHolder类

public static class StationViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {
        CardView cardView;
        TextView stationName;
        TextView stationFrequency;
        ImageView stationLogo;
        ImageView dragView;

        StationViewHolder(final View itemView) {
            super(itemView);
            final Context context = itemView.getContext();
            cardView = (CardView) itemView.findViewById(R.id.rv_card);
            stationName = (TextView) itemView.findViewById(R.id.station_name);
            stationFrequency = (TextView) itemView.findViewById(R.id.station_freq);
            stationLogo = (ImageView) itemView.findViewById(R.id.station_logo);
            dragView = (ImageView) itemView.findViewById(R.id.dragView);
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            itemView.setBackgroundColor(0);
        }
    }

2 个答案:

答案 0 :(得分:0)

您需要在Adapter类中添加一个方法,该方法具有圆顶标志,并且根据标记的值,您可以看到并消失ImageView。 您可以在StationViewHolder中添加一个if条件。

您需要通知适配器。

答案 1 :(得分:0)

使用此代码,

@Override
    public void onItemSelected() {
        itemView.setBackgroundColor(Color.LTGRAY);
        dragView.setVisiblity(View.VISIBLE);
    }