public class

AlphaJumpOverlayView

extends GridLayout

 java.lang.Object

↳ViewGroup

androidx.gridlayout.widget.GridLayout

↳androidx.car.widget.AlphaJumpOverlayView

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

This view shows a grid of alphabetic letters that you can tap on to advance a list to the beginning of that list.

Summary

Fields
from GridLayoutALIGN_BOUNDS, ALIGN_MARGINS, BASELINE, BOTTOM, CENTER, END, FILL, HORIZONTAL, LEFT, RIGHT, START, TOP, UNDEFINED, VERTICAL
Constructors
publicAlphaJumpOverlayView(Context context)

Methods
public voidhide()

Hide the Alpha Jump Overview by toggling its visibility

public voidshow()

Show the Alpha Jump Overview by toggling its visibility

from GridLayoutcheckLayoutParams, generateDefaultLayoutParams, generateLayoutParams, generateLayoutParams, getAlignmentMode, getColumnCount, getOrientation, getPrinter, getRowCount, getUseDefaultMargins, isColumnOrderPreserved, isRowOrderPreserved, onLayout, onMeasure, requestLayout, setAlignmentMode, setColumnCount, setColumnOrderPreserved, setOrientation, setPrinter, setRowCount, setRowOrderPreserved, setUseDefaultMargins, spec, spec, spec, spec, spec, spec, spec, spec
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public AlphaJumpOverlayView(Context context)

Methods

public void show()

Show the Alpha Jump Overview by toggling its visibility

public void hide()

Hide the Alpha Jump Overview by toggling its visibility

Source

/*
 * Copyright 2018 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.widget;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.car.R;
import androidx.gridlayout.widget.GridLayout;

import java.util.List;

/**
 * This view shows a grid of alphabetic letters that you can tap on to advance a list to the
 * beginning of that list.
 */
public class AlphaJumpOverlayView extends GridLayout {
    private AlphaJumpAdapter mAdapter;
    private PagedListView mPagedListView;
    private List<AlphaJumpBucket> mBuckets;
    private LayoutInflater mInflater;
    private Animation mOpenAnimation;
    private Animation mCloseAnimation;

    public AlphaJumpOverlayView(@NonNull Context context) {
        super(context);
        Resources res = context.getResources();

        setOrientation(HORIZONTAL);

        // Get the background color from the theme and set it.
        TypedValue typedValue = new TypedValue();
        boolean hasAttribute = context.getTheme().resolveAttribute(
                R.attr.alphaJumpPickerBackground, typedValue, true);
        setBackgroundColor(hasAttribute
                ? typedValue.data
                : context.getColor(R.color.car_card_dark));

        setColumnCount(res.getInteger(R.integer.car_alpha_jump_button_columns));

        //Subtract the buttons' margin from the side margin.
        int carMargin = res.getDimensionPixelSize(R.dimen.car_margin);
        int buttonMargin = res.getDimensionPixelSize(R.dimen.car_padding_0);
        int padding = carMargin - buttonMargin;
        setPadding(padding, 0, padding, 0);
    }

    void init(PagedListView plv, AlphaJumpAdapter adapter) {
        Context context = getContext();
        mInflater = LayoutInflater.from(context);
        mPagedListView = plv;
        mAdapter = adapter;
        mBuckets = adapter.getAlphaJumpBuckets();

        mOpenAnimation = AnimationUtils
                .loadAnimation(context, R.anim.car_alpha_picker_enter);
        mCloseAnimation = AnimationUtils
                .loadAnimation(context, R.anim.car_alpha_picker_exit);
        createButtons();
    }

    private void createButtons() {
        removeAllViews();
        for (AlphaJumpBucket bucket : mBuckets) {
            View container = mInflater
                    .inflate(R.layout.car_alpha_jump_picker_button, this, false);
            TextView btn = container.findViewById(R.id.button);
            btn.setText(bucket.getLabel());
            btn.setOnClickListener(this::onButtonClick);
            btn.setTag(bucket);
            if (bucket.isEmpty()) {
                btn.setEnabled(false);
                btn.setBackgroundResource(R.color.alpha_picker_unavailable_bg);
            }
            addView(container);
        }
    }

    private void onButtonClick(View v) {
        hide();
        AlphaJumpBucket bucket = (AlphaJumpBucket) v.getTag();
        if (bucket != null) {
            mAdapter.onAlphaJumpLeave(bucket);

            mPagedListView.snapToPosition(bucket.getIndex());
        }
    }

    /**
     * Show the Alpha Jump Overview by toggling its visibility
     */
    public void show() {
        mAdapter.onAlphaJumpEnter();
        setVisibility(VISIBLE);
        startAnimation(mOpenAnimation);
    }

    /**
     * Hide the Alpha Jump Overview by toggling its visibility
     */
    public void hide() {
        startAnimation(mCloseAnimation);
        setVisibility(GONE);
    }
}