Search in sources :

Example 76 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project UltimateAndroid by cymcsg.

the class RecyclerViewItemAnimatorActivity method setupSpinner.

private void setupSpinner() {
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.animators, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
            switch(position) {
                case 0:
                    mRecyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(mRecyclerView));
                    break;
                case 1:
                    mRecyclerView.setItemAnimator(new SlideInOutRightItemAnimator(mRecyclerView));
                    break;
                case 2:
                    mRecyclerView.setItemAnimator(new SlideInOutTopItemAnimator(mRecyclerView));
                    break;
                case 3:
                    mRecyclerView.setItemAnimator(new SlideInOutBottomItemAnimator(mRecyclerView));
                    break;
                case 4:
                    mRecyclerView.setItemAnimator(new ScaleInOutItemAnimator(mRecyclerView));
                    break;
                case 5:
                    mRecyclerView.setItemAnimator(new SlideScaleInOutRightItemAnimator(mRecyclerView));
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });
}
Also used : Spinner(android.widget.Spinner) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) AdapterView(android.widget.AdapterView) AdapterView(android.widget.AdapterView)

Example 77 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project UltimateAndroid by cymcsg.

the class FixedGridLayoutManager method fillGrid.

private void fillGrid(int direction, int emptyLeft, int emptyTop, RecyclerView.Recycler recycler) {
    if (mFirstVisiblePosition < 0)
        mFirstVisiblePosition = 0;
    if (mFirstVisiblePosition >= getItemCount())
        mFirstVisiblePosition = (getItemCount() - 1);
    /*
         * First, we will detach all existing views from the layout.
         * detachView() is a lightweight operation that we can use to
         * quickly reorder views without a full add/remove.
         */
    SparseArray<View> viewCache = new SparseArray<View>(getChildCount());
    int startLeftOffset = getPaddingLeft() + emptyLeft;
    int startTopOffset = getPaddingTop() + emptyTop;
    if (getChildCount() != 0) {
        final View topView = getChildAt(0);
        startLeftOffset = getDecoratedLeft(topView);
        startTopOffset = getDecoratedTop(topView);
        switch(direction) {
            case DIRECTION_START:
                startLeftOffset -= mDecoratedChildWidth;
                break;
            case DIRECTION_END:
                startLeftOffset += mDecoratedChildWidth;
                break;
            case DIRECTION_UP:
                startTopOffset -= mDecoratedChildHeight;
                break;
            case DIRECTION_DOWN:
                startTopOffset += mDecoratedChildHeight;
                break;
        }
        //Cache all views by their existing position, before updating counts
        for (int i = 0; i < getChildCount(); i++) {
            int position = positionOfIndex(i);
            final View child = getChildAt(i);
            viewCache.put(position, child);
        }
        // Views we still need will be added back at the proper index.
        for (int i = 0; i < viewCache.size(); i++) {
            detachView(viewCache.valueAt(i));
        }
    }
    /*
         * Next, we advance the visible position based on the fill direction.
         * DIRECTION_NONE doesn't advance the position in any direction.
         */
    switch(direction) {
        case DIRECTION_START:
            mFirstVisiblePosition--;
            break;
        case DIRECTION_END:
            mFirstVisiblePosition++;
            break;
        case DIRECTION_UP:
            mFirstVisiblePosition -= getTotalColumnCount();
            break;
        case DIRECTION_DOWN:
            mFirstVisiblePosition += getTotalColumnCount();
            break;
    }
    /*
         * Next, we supply the grid of items that are deemed visible.
         * If these items were previously there, they will simple be
         * re-attached. New views that must be created are obtained
         * from the Recycler and added.
         */
    int leftOffset = startLeftOffset;
    int topOffset = startTopOffset;
    for (int i = 0; i < getVisibleChildCount(); i++) {
        int nextPosition = positionOfIndex(i);
        if (nextPosition >= getItemCount()) {
            //Item space beyond the data set, don't attempt to add a view
            continue;
        }
        //Layout this position
        View view = viewCache.get(nextPosition);
        if (view == null) {
            /*
                 * The Recycler will give us either a newly constructed view,
                 * or a recycled view it has on-hand. In either case, the
                 * view will already be fully bound to the data by the
                 * adapter for us.
                 */
            view = recycler.getViewForPosition(nextPosition);
            addView(view);
            /*
                 * It is prudent to measure/layout each new view we
                 * receive from the Recycler. We don't have to do
                 * this for views we are just re-arranging.
                 */
            measureChildWithMargins(view, 0, 0);
            layoutDecorated(view, leftOffset, topOffset, leftOffset + mDecoratedChildWidth, topOffset + mDecoratedChildHeight);
        } else {
            //Re-attach the cached view at its new index
            attachView(view);
            viewCache.remove(nextPosition);
        }
        if (i % mVisibleColumnCount == (mVisibleColumnCount - 1)) {
            leftOffset = startLeftOffset;
            topOffset += mDecoratedChildHeight;
        //If we wrapped without setting the column count, we've reached it
        } else {
            leftOffset += mDecoratedChildWidth;
        }
    }
    /*
         * Finally, we ask the Recycler to scrap and store any views
         * that we did not re-attach. These are views that are not currently
         * necessary because they are no longer visible.
         */
    for (int i = 0; i < viewCache.size(); i++) {
        recycler.recycleView(viewCache.valueAt(i));
    }
}
Also used : SparseArray(android.util.SparseArray) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 78 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project UltimateAndroid by cymcsg.

the class StaticGridLayoutManager method onLayoutChildren.

/*
     * This method is your initial call from the framework. You will receive it when you
     * need to start laying out the initial set of views. This method will not be called
     * repeatedly, so don't rely on it to continually process changes during user
     * interaction.
     *
     * This method will be called when the data set in the adapter changes, so it can be
     * used to update a layout based on a new item count.
     */
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    //We have nothing to show for an empty data set but clear any existing views
    if (getItemCount() == 0) {
        detachAndScrapAttachedViews(recycler);
        return;
    }
    //Make the grid as square as possible, column count is root of the data set
    mTotalColumnCount = (int) Math.round(Math.sqrt(getItemCount()));
    if (getChildCount() == 0) {
        //First or empty layout
        //Scrap measure one child
        View scrap = recycler.getViewForPosition(0);
        addView(scrap);
        measureChildWithMargins(scrap, 0, 0);
        /*
             * We make some assumptions in this code based on every child
             * view being the same size (i.e. a uniform grid). This allows
             * us to compute the following values up front because they
             * won't change.
             */
        mDecoratedChildWidth = getDecoratedMeasuredWidth(scrap);
        mDecoratedChildHeight = getDecoratedMeasuredHeight(scrap);
        detachAndScrapView(scrap, recycler);
    }
    //Always update the visible row/column counts
    updateWindowSizing();
    int childLeft;
    int childTop;
    if (getChildCount() == 0) {
        //First or empty layout
        /*
             * Reset the visible and scroll positions
             */
        mFirstVisiblePosition = 0;
        childLeft = childTop = 0;
    } else if (getVisibleChildCount() > getItemCount()) {
        //Data set is too small to scroll fully, just reset position
        mFirstVisiblePosition = 0;
        childLeft = childTop = 0;
    } else {
        //Adapter data set changes
        /*
             * Keep the existing initial position, and save off
             * the current scrolled offset.
             */
        final View topChild = getChildAt(0);
        if (mForceClearOffsets) {
            childLeft = childTop = 0;
            mForceClearOffsets = false;
        } else {
            childLeft = getDecoratedLeft(topChild);
            childTop = getDecoratedTop(topChild);
        }
        /*
             * Adjust the visible position if out of bounds in the
             * new layout. This occurs when the new item count in an adapter
             * is much smaller than it was before, and you are scrolled to
             * a location where no items would exist.
             */
        int lastVisiblePosition = positionOfIndex(getVisibleChildCount() - 1);
        if (lastVisiblePosition >= getItemCount()) {
            lastVisiblePosition = (getItemCount() - 1);
            int lastColumn = mVisibleColumnCount - 1;
            int lastRow = mVisibleRowCount - 1;
            //Adjust to align the last position in the bottom-right
            mFirstVisiblePosition = Math.max(lastVisiblePosition - lastColumn - (lastRow * getTotalColumnCount()), 0);
            childLeft = getHorizontalSpace() - (mDecoratedChildWidth * mVisibleColumnCount);
            childTop = getVerticalSpace() - (mDecoratedChildHeight * mVisibleRowCount);
            // This happens on data sets too small to scroll in a direction.
            if (getFirstVisibleRow() == 0) {
                childTop = Math.min(childTop, 0);
            }
            if (getFirstVisibleColumn() == 0) {
                childLeft = Math.min(childLeft, 0);
            }
        }
    }
    //Clear all attached views into the recycle bin
    detachAndScrapAttachedViews(recycler);
    //Fill the grid for the initial layout of views
    fillGrid(DIRECTION_NONE, childLeft, childTop, recycler);
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 79 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project BaseRecyclerViewAdapterHelper by CymChad.

the class ItemClickActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBackBtn();
    setTitle("ItemClickActivity Activity");
    setContentView(R.layout.activity_item_click);
    mRecyclerView = (RecyclerView) findViewById(R.id.list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    initAdapter();
    mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {

        /**
             * Callback method to be invoked when an item in this AdapterView has
             * been clicked.
             *
             * @param view     The view within the AdapterView that was clicked (this
             *                 will be a view provided by the adapter)
             * @param position The position of the view in the adapter.
             */
        @Override
        public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
            Log.d(TAG, "SimpleOnItemClick: ");
            Toast.makeText(ItemClickActivity.this, "onSimpleItemClick" + position, Toast.LENGTH_SHORT).show();
        }

        /**
             * callback method to be invoked when an chidview in this view has been
             * click and held
             *
             * @param view     The view whihin the AbsListView that was clicked
             * @param position The position of the view int the adapter
             * @return true if the callback consumed the long click ,false otherwise
             */
        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Logger.d("onItemChildClick " + position + " be click");
            Toast.makeText(ItemClickActivity.this, "onItemChildClick" + position, Toast.LENGTH_SHORT).show();
        }

        /**
             * Callback method to be invoked when an item in this view has been clicked and held.
             * @param adapter
             * @param view
             * @param position
             */
        @Override
        public void onItemLongClick(final BaseQuickAdapter adapter, final View view, final int position) {
            Toast.makeText(ItemClickActivity.this, "onItemLongClick" + position, Toast.LENGTH_SHORT).show();
        }

        /**
             * Callback method to be invoked when an itemchild in this view has been clicked and held.
             * @param adapter
             * @param view
             * @param position
             */
        @Override
        public void onItemChildLongClick(final BaseQuickAdapter adapter, final View view, final int position) {
            Toast.makeText(ItemClickActivity.this, "onItemChildLongClick" + position, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : OnItemClickListener(com.chad.library.adapter.base.listener.OnItemClickListener) BaseQuickAdapter(com.chad.library.adapter.base.BaseQuickAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 80 with Adapter

use of android.support.v7.widget.RecyclerView.Adapter in project AndroidChromium by JackyAndroid.

the class NewTabPageView method initialize.

/**
     * Initializes the NTP. This must be called immediately after inflation, before this object is
     * used in any other way.
     *
     * @param manager NewTabPageManager used to perform various actions when the user interacts
     *                with the page.
     * @param searchProviderHasLogo Whether the search provider has a logo.
     * @param scrollPosition The adapter scroll position to initialize to.
     */
public void initialize(NewTabPageManager manager, boolean searchProviderHasLogo, int scrollPosition) {
    mManager = manager;
    mUiConfig = new UiConfig(this);
    ViewStub stub = (ViewStub) findViewById(R.id.new_tab_page_layout_stub);
    mUseCardsUi = manager.getSuggestionsSource() != null;
    if (mUseCardsUi) {
        stub.setLayoutResource(R.layout.new_tab_page_recycler_view);
        mRecyclerView = (NewTabPageRecyclerView) stub.inflate();
        // Don't attach now, the recyclerView itself will determine when to do it.
        mNewTabPageLayout = (NewTabPageLayout) LayoutInflater.from(getContext()).inflate(R.layout.new_tab_page_layout, mRecyclerView, false);
        mNewTabPageLayout.setUseCardsUiEnabled(mUseCardsUi);
        mRecyclerView.setAboveTheFoldView(mNewTabPageLayout);
        // Tailor the LayoutParams for the snippets UI, as the configuration in the XML is
        // made for the ScrollView UI.
        ViewGroup.LayoutParams params = mNewTabPageLayout.getLayoutParams();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mRecyclerView.setItemAnimator(new DefaultItemAnimator() {

            @Override
            public void onAnimationFinished(ViewHolder viewHolder) {
                super.onAnimationFinished(viewHolder);
                // When removing sections, because the animations are all translations, the
                // scroll events don't fire and we can get in the situation where the toolbar
                // buttons disappear.
                updateSearchBoxOnScroll();
            }
        });
    } else {
        stub.setLayoutResource(R.layout.new_tab_page_scroll_view);
        mScrollView = (NewTabPageScrollView) stub.inflate();
        mScrollView.setBackgroundColor(NtpStyleUtils.getBackgroundColorResource(getResources(), false));
        mScrollView.enableBottomShadow(SHADOW_COLOR);
        mNewTabPageLayout = (NewTabPageLayout) findViewById(R.id.ntp_content);
    }
    mMostVisitedDesign = new MostVisitedDesign(getContext());
    mMostVisitedLayout = (MostVisitedLayout) mNewTabPageLayout.findViewById(R.id.most_visited_layout);
    mMostVisitedDesign.initMostVisitedLayout(searchProviderHasLogo);
    mSearchProviderLogoView = (LogoView) mNewTabPageLayout.findViewById(R.id.search_provider_logo);
    mSearchBoxView = (ViewGroup) mNewTabPageLayout.findViewById(R.id.search_box);
    mNoSearchLogoSpacer = mNewTabPageLayout.findViewById(R.id.no_search_logo_spacer);
    initializeSearchBoxTextView();
    initializeVoiceSearchButton();
    initializeBottomToolbar();
    mNewTabPageLayout.addOnLayoutChangeListener(this);
    setSearchProviderHasLogo(searchProviderHasLogo);
    mPendingLoadTasks++;
    mManager.setMostVisitedURLsObserver(this, mMostVisitedDesign.getNumberOfTiles(searchProviderHasLogo));
    // Set up snippets
    if (mUseCardsUi) {
        mNewTabPageAdapter = NewTabPageAdapter.create(mManager, mNewTabPageLayout, mUiConfig);
        mRecyclerView.setAdapter(mNewTabPageAdapter);
        mRecyclerView.scrollToPosition(scrollPosition);
        if (CardsVariationParameters.isScrollBelowTheFoldEnabled()) {
            int searchBoxHeight = NtpStyleUtils.getSearchBoxHeight(getResources());
            mRecyclerView.getLinearLayoutManager().scrollToPositionWithOffset(mNewTabPageAdapter.getFirstHeaderPosition(), searchBoxHeight);
        }
        // Set up swipe-to-dismiss
        ItemTouchHelper helper = new ItemTouchHelper(mNewTabPageAdapter.getItemTouchCallbacks());
        helper.attachToRecyclerView(mRecyclerView);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            private boolean mScrolledOnce = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState != RecyclerView.SCROLL_STATE_DRAGGING)
                    return;
                RecordUserAction.record("MobileNTP.Snippets.Scrolled");
                if (mScrolledOnce)
                    return;
                mScrolledOnce = true;
                NewTabPageUma.recordSnippetAction(NewTabPageUma.SNIPPETS_ACTION_SCROLLED);
            }
        });
        initializeSearchBoxRecyclerViewScrollHandling();
    } else {
        initializeSearchBoxScrollHandling();
    }
}
Also used : ViewGroup(android.view.ViewGroup) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) ViewStub(android.view.ViewStub) ViewHolder(android.support.v7.widget.RecyclerView.ViewHolder) RecyclerView(android.support.v7.widget.RecyclerView) NewTabPageRecyclerView(org.chromium.chrome.browser.ntp.cards.NewTabPageRecyclerView)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)688 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)612 View (android.view.View)593 TextView (android.widget.TextView)245 ArrayList (java.util.ArrayList)179 Intent (android.content.Intent)148 ImageView (android.widget.ImageView)132 Toolbar (android.support.v7.widget.Toolbar)118 GridLayoutManager (android.support.v7.widget.GridLayoutManager)111 AdapterView (android.widget.AdapterView)109 ViewGroup (android.view.ViewGroup)97 AlertDialog (android.support.v7.app.AlertDialog)91 Bundle (android.os.Bundle)85 ListView (android.widget.ListView)85 BindView (butterknife.BindView)85 Nullable (android.support.annotation.Nullable)78 DialogInterface (android.content.DialogInterface)71 Context (android.content.Context)65 ArrayAdapter (android.widget.ArrayAdapter)65 LayoutInflater (android.view.LayoutInflater)64