Search in sources :

Example 91 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project bilibili-android-client by HotBitmapGG.

the class SectionedRecyclerViewAdapter method onCreateViewHolder.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    View view = null;
    for (Map.Entry<String, Integer> entry : sectionViewTypeNumbers.entrySet()) {
        if (viewType >= entry.getValue() && viewType < entry.getValue() + VIEW_TYPE_QTY) {
            Section section = sections.get(entry.getKey());
            int sectionViewType = viewType - entry.getValue();
            switch(sectionViewType) {
                case VIEW_TYPE_HEADER:
                    {
                        Integer resId = section.getHeaderResourceId();
                        if (resId == null) {
                            throw new NullPointerException("Missing 'header' resource id");
                        }
                        view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
                        // get the header viewholder from the section
                        viewHolder = section.getHeaderViewHolder(view);
                        break;
                    }
                case VIEW_TYPE_FOOTER:
                    {
                        Integer resId = section.getFooterResourceId();
                        if (resId == null) {
                            throw new NullPointerException("Missing 'footer' resource id");
                        }
                        view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
                        // get the footer viewholder from the section
                        viewHolder = section.getFooterViewHolder(view);
                        break;
                    }
                case VIEW_TYPE_ITEM_LOADED:
                    {
                        view = LayoutInflater.from(parent.getContext()).inflate(section.getItemResourceId(), parent, false);
                        // get the item viewholder from the section
                        viewHolder = section.getItemViewHolder(view);
                        break;
                    }
                case VIEW_TYPE_LOADING:
                    {
                        Integer resId = section.getLoadingResourceId();
                        if (resId == null) {
                            throw new NullPointerException("Missing 'loading state' resource id");
                        }
                        view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
                        // get the loading viewholder from the section
                        viewHolder = section.getLoadingViewHolder(view);
                        break;
                    }
                case VIEW_TYPE_FAILED:
                    {
                        Integer resId = section.getFailedResourceId();
                        if (resId == null) {
                            throw new NullPointerException("Missing 'failed state' resource id");
                        }
                        view = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
                        // get the failed load viewholder from the section
                        viewHolder = section.getFailedViewHolder(view);
                        break;
                    }
                default:
                    throw new IllegalArgumentException("Invalid viewType");
            }
        }
    }
    return viewHolder;
}
Also used : RecyclerView(android.support.v7.widget.RecyclerView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 92 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder in project Gadgetbridge by Freeyourgadget.

the class ControlCenterv2 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (GBApplication.isDarkThemeEnabled()) {
        setTheme(R.style.GadgetbridgeThemeDark_NoActionBar);
    } else {
        setTheme(R.style.GadgetbridgeTheme_NoActionBar);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_controlcenterv2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            launchDiscoveryActivity();
        }
    });
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.controlcenter_navigation_drawer_open, R.string.controlcenter_navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    //end of material design boilerplate
    deviceManager = ((GBApplication) getApplication()).getDeviceManager();
    deviceListView = (RecyclerView) findViewById(R.id.deviceListView);
    deviceListView.setHasFixedSize(true);
    deviceListView.setLayoutManager(new LinearLayoutManager(this));
    background = (ImageView) findViewById(R.id.no_items_bg);
    deviceList = deviceManager.getDevices();
    mGBDeviceAdapter = new GBDeviceAdapterv2(this, deviceList);
    deviceListView.setAdapter(this.mGBDeviceAdapter);
    ItemTouchHelper swipeToDismissTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT, ItemTouchHelper.RIGHT) {

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            if (dX > 50)
                dX = 50;
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            GB.toast(getBaseContext(), "onMove", Toast.LENGTH_LONG, GB.ERROR);
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            GB.toast(getBaseContext(), "onSwiped", Toast.LENGTH_LONG, GB.ERROR);
        }

        @Override
        public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        }
    });
    //uncomment to enable fixed-swipe to reveal more actions
    //swipeToDismissTouchHelper.attachToRecyclerView(deviceListView);
    registerForContextMenu(deviceListView);
    IntentFilter filterLocal = new IntentFilter();
    filterLocal.addAction(GBApplication.ACTION_QUIT);
    filterLocal.addAction(DeviceManager.ACTION_DEVICES_CHANGED);
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
    refreshPairedDevices();
    /*
         * Ask for permission to intercept notifications on first run.
         */
    Prefs prefs = GBApplication.getPrefs();
    if (prefs.getBoolean("firstrun", true)) {
        prefs.getPreferences().edit().putBoolean("firstrun", false).apply();
        Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
        startActivity(enableIntent);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkAndRequestPermissions();
    }
    ChangeLog cl = new ChangeLog(this);
    if (cl.isFirstRun()) {
        cl.getLogDialog().show();
    }
    GBApplication.deviceService().start();
    if (GB.isBluetoothEnabled() && deviceList.isEmpty() && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        startActivity(new Intent(this, DiscoveryActivity.class));
    } else {
        GBApplication.deviceService().requestDeviceInfo();
    }
}
Also used : IntentFilter(android.content.IntentFilter) NavigationView(android.support.design.widget.NavigationView) Canvas(android.graphics.Canvas) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) GBDeviceAdapterv2(nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapterv2) Intent(android.content.Intent) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) NavigationView(android.support.design.widget.NavigationView) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) FloatingActionButton(android.support.design.widget.FloatingActionButton) RecyclerView(android.support.v7.widget.RecyclerView) ChangeLog(de.cketti.library.changelog.ChangeLog) DrawerLayout(android.support.v4.widget.DrawerLayout) Toolbar(android.support.v7.widget.Toolbar)

Example 93 with ViewHolder

use of android.support.v7.widget.RecyclerView.ViewHolder 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)

Example 94 with ViewHolder

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

the class NewTabPageRecyclerView method dismissItemWithAnimation.

/**
     * Animates the card being swiped to the right as if the user had dismissed it. Any changes to
     * the animation here should be reflected also in
     * {@link #updateViewStateForDismiss(float, ViewHolder)} and reset in
     * {@link CardViewHolder#onBindViewHolder()}.
     * @param suggestion The item to be dismissed.
     */
public void dismissItemWithAnimation(SnippetArticle suggestion) {
    // We need to recompute the position, as it might have changed.
    final int position = getNewTabPageAdapter().getSuggestionPosition(suggestion);
    if (position == RecyclerView.NO_POSITION) {
        // The item does not exist anymore, so ignore.
        return;
    }
    final View itemView = mLayoutManager.findViewByPosition(position);
    if (itemView == null) {
        // The view is not visible anymore, skip the animation.
        getNewTabPageAdapter().dismissItem(position);
        return;
    }
    final ViewHolder viewHolder = getChildViewHolder(itemView);
    if (!((NewTabPageViewHolder) viewHolder).isDismissable()) {
        // The item is not dismissable (anymore), so ignore.
        return;
    }
    AnimatorSet animation = new AnimatorSet();
    animation.playTogether(ObjectAnimator.ofFloat(itemView, View.ALPHA, 0f), ObjectAnimator.ofFloat(itemView, View.TRANSLATION_X, (float) itemView.getWidth()));
    animation.setDuration(DISMISS_ANIMATION_TIME_MS);
    animation.setInterpolator(DISMISS_INTERPOLATOR);
    animation.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationStart(Animator animation) {
            NewTabPageRecyclerView.this.onItemDismissStarted(viewHolder);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            getNewTabPageAdapter().dismissItem(position);
            NewTabPageRecyclerView.this.onItemDismissFinished(viewHolder);
        }
    });
    animation.start();
}
Also used : Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) SectionHeaderViewHolder(org.chromium.chrome.browser.ntp.snippets.SectionHeaderViewHolder) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) AnimatorSet(android.animation.AnimatorSet) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 95 with ViewHolder

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

the class NewTabPageRecyclerView method onLayout.

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int numberViews = getChildCount();
    for (int i = 0; i < numberViews; ++i) {
        View view = getChildAt(i);
        NewTabPageViewHolder viewHolder = (NewTabPageViewHolder) getChildViewHolder(view);
        if (viewHolder == null)
            return;
        viewHolder.updateLayoutParams();
    }
    super.onLayout(changed, l, t, r, b);
}
Also used : View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)265 View (android.view.View)248 TextView (android.widget.TextView)129 ImageView (android.widget.ImageView)95 ViewHolder (android.support.v7.widget.RecyclerView.ViewHolder)47 LayoutInflater (android.view.LayoutInflater)34 ViewGroup (android.view.ViewGroup)32 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)28 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)27 Intent (android.content.Intent)26 ArrayList (java.util.ArrayList)25 Context (android.content.Context)21 ViewPropertyAnimatorCompat (android.support.v4.view.ViewPropertyAnimatorCompat)17 BindView (butterknife.BindView)17 CardView (android.support.v7.widget.CardView)15 Paint (android.graphics.Paint)11 ParallaxRecyclerAdapter (com.poliveira.parallaxrecycleradapter.ParallaxRecyclerAdapter)8 ExampleData (de.madcyph3r.example.data.ExampleData)8 Drawable (android.graphics.drawable.Drawable)7 Test (org.junit.Test)7