public class

DrawerItemViewHolder

extends RecyclerView.ViewHolder

implements OnUxRestrictionsChangedListener

 java.lang.Object

androidx.recyclerview.widget.RecyclerView.ViewHolder

↳androidx.car.drawer.DrawerItemViewHolder

Gradle dependencies

compile group: 'androidx.car', name: 'car', version: '1.0.0-alpha7'

  • groupId: androidx.car
  • artifactId: car
  • version: 1.0.0-alpha7

Artifact androidx.car:car:1.0.0-alpha7 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.car:car com.android.support:car

Overview

Re-usable for displaying items in the CarDrawerAdapter.

Summary

Fields
from RecyclerView.ViewHolderitemView
Constructors
publicDrawerItemViewHolder(View view)

Create a new ViewHolder that will parse the given view that is meant to displaying items in a drawer.

Methods
public TextViewgetBodyView()

Returns the view that is used for the body text that is smaller than the title text and appears beneath the title.

public ImageViewgetEndIconView()

Returns the icon that is displayed at the end of the view.

public ImageViewgetIconView()

Returns the view that should be used to display the main icon.

public TextViewgetTitleView()

Returns the view that will display the main title.

public voidonUxRestrictionsChanged(CarUxRestrictions restrictionsInfo)

Updates child views with current car UX restrictions.

from RecyclerView.ViewHoldergetAbsoluteAdapterPosition, getAdapterPosition, getBindingAdapter, getBindingAdapterPosition, getItemId, getItemViewType, getLayoutPosition, getOldPosition, getPosition, isRecyclable, setIsRecyclable, toString
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructors

public DrawerItemViewHolder(View view)

Create a new ViewHolder that will parse the given view that is meant to displaying items in a drawer.

The given view is required to have a title and icon view with an optional body and end icon view. These views are looked up via ids, specifically:

  • Icon - R.id.icon
  • Title - R.id.title
  • Body - R.id.body
  • End Icon - R.id.end_icon

This method is exposed for testing purposes. The DrawerItemViewHolder is automatically created by the CarDrawerAdapter.

Parameters:

view: The view that will represent an item in a drawer.

Methods

public ImageView getIconView()

Returns the view that should be used to display the main icon.

public TextView getTitleView()

Returns the view that will display the main title.

public TextView getBodyView()

Returns the view that is used for the body text that is smaller than the title text and appears beneath the title.

public ImageView getEndIconView()

Returns the icon that is displayed at the end of the view.

public void onUxRestrictionsChanged(CarUxRestrictions restrictionsInfo)

Updates child views with current car UX restrictions.

Text might be truncated to meet length limit required by regulation.

Parameters:

restrictionsInfo: current car UX restrictions.

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.car.drawer;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.car.R;
import androidx.car.util.CarUxRestrictionsUtils;
import androidx.car.uxrestrictions.CarUxRestrictions;
import androidx.car.uxrestrictions.OnUxRestrictionsChangedListener;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Re-usable {@link RecyclerView.ViewHolder} for displaying items in the
 * {@link androidx.car.drawer.CarDrawerAdapter}.
 */
public class DrawerItemViewHolder extends RecyclerView.ViewHolder implements
        OnUxRestrictionsChangedListener {
    private final ImageView mIcon;
    private final TextView mTitle;
    private final TextView mBody;
    private final ImageView mEndIcon;

    /**
     * Create a new {@code ViewHolder} that will parse the given view that is meant to displaying
     * items in a drawer.
     *
     * <p>The given view is required to have a title and icon view with an optional body and end
     * icon view. These views are looked up via ids, specifically:
     *
     * <ul>
     *     <li>Icon - R.id.icon
     *     <li>Title - R.id.title
     *     <li>Body - R.id.body
     *     <li>End Icon - R.id.end_icon
     * </ul>
     *
     * <p>This method is exposed for testing purposes. The {@code DrawerItemViewHolder} is
     * automatically created by the {@link CarDrawerAdapter}.
     *
     * @param view The view that will represent an item in a drawer.
     */
    @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
    public DrawerItemViewHolder(@NonNull View view) {
        super(view);
        mIcon = view.findViewById(R.id.icon);
        if (mIcon == null) {
            throw new IllegalArgumentException("Icon view cannot be null!");
        }

        mTitle = view.findViewById(R.id.title);
        if (mTitle == null) {
            throw new IllegalArgumentException("Title view cannot be null!");
        }

        // Next two are optional and may be null.
        mBody = view.findViewById(R.id.body);
        mEndIcon = view.findViewById(R.id.end_icon);
    }

    /** Returns the view that should be used to display the main icon. */
    @NonNull
    public ImageView getIconView() {
        return mIcon;
    }

    /** Returns the view that will display the main title. */
    @NonNull
    public TextView getTitleView() {
        return mTitle;
    }

    /**
     * Returns the view that is used for the body text that is smaller than the title text and
     * appears beneath the title.
     */
    @Nullable
    public TextView getBodyView() {
        return mBody;
    }

    /** Returns the icon that is displayed at the end of the view. */
    @Nullable
    public ImageView getEndIconView() {
        return mEndIcon;
    }

    /**
     * Updates child views with current car UX restrictions.
     *
     * <p>{@code Text} might be truncated to meet length limit required by regulation.
     *
     * @param restrictionsInfo current car UX restrictions.
     */
    @Override
    public void onUxRestrictionsChanged(CarUxRestrictions restrictionsInfo) {
        CarUxRestrictionsUtils.apply(itemView.getContext(), restrictionsInfo, getBodyView());
    }
}