Giter Club home page Giter Club logo

discretescrollview's Introduction

DiscreteScrollView

The library is a RecyclerView-based implementation of a scrollable list, where current item is centered and can be changed using swipes. It is similar to a ViewPager, but you can quickly and painlessly create layout, where views adjacent to the currently selected view are partially or fully visible on the screen.

GifSampleShop

Gradle

Add this into your dependencies block.

compile 'com.yarolegovich:discrete-scrollview:1.5.1'

Reporting an issue

If you are going to report an issue, I will greatly appreciate you including some code which I can run to see the issue. By doing so you maximize the chance that I will fix the problem.

By the way, before reporting a problem, try replacing DiscreteScrollView with a RecyclerView. If the problem is still present, it's likely somewhere in your code.

Sample

Get it on Google Play

Please see the sample app for examples of library usage.

GifSampleWeather

Wiki

General

The library uses a custom LayoutManager to adjust items' positions on the screen and handle scroll, however it is not exposed to the client code. All public API is accessible through DiscreteScrollView class, which is a simple descendant of RecyclerView.

If you have ever used RecyclerView - you already know how to use this library. One thing to note - you should NOT set LayoutManager.

Usage:

  1. Add DiscreteScrollView to your layout either using xml or code:
  2. Create your implementation of RecyclerView.Adapter. Refer to the sample for an example, if you don't know how to do it.
  3. Set the adapter.
  4. You are done!
<com.yarolegovich.discretescrollview.DiscreteScrollView
  android:id="@+id/picker"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:dsv_orientation="horizontal|vertical" />  <!-- orientation is optional, default is horizontal -->
DiscreteScrollView scrollView = findViewById(R.id.picker);
scrollView.setAdapter(new YourAdapterImplementation());

API

General

scrollView.setOrientation(DSVOrientation o); //Sets an orientation of the view
scrollView.setOffscreenItems(count); //Reserve extra space equal to (childSize * count) on each side of the view
scrollView.setOverScrollEnabled(enabled); //Can also be set using android:overScrollMode xml attribute

Related to the current item:

scrollView.getCurrentItem(); //returns adapter position of the currently selected item or -1 if adapter is empty.
scrollView.scrollToPosition(int position); //position becomes selected
scrollView.smoothScrollToPosition(int position); //position becomes selected with animated scroll
scrollView.setItemTransitionTimeMillis(int millis); //determines how much time it takes to change the item on fling, settle or smoothScroll

Transformations

One useful feature of ViewPager is page transformations. It allows you, for example, to create carousel effect. DiscreteScrollView also supports page transformations.

scrollView.setItemTransformer(transformer);

public interface DiscreteScrollItemTransformer {
    /**
     * In this method you apply any transform you can imagine (perfomance is not guaranteed).
     * @param position is a value inside the interval [-1f..1f]. In idle state:
     * |view1|  |currentlySelectedView|  |view2|
     * -view1 and everything to the left is on position -1;
     * -currentlySelectedView is on position 0;
     * -view2 and everything to the right is on position 1.
     */
    void transformItem(View item, float position); 
}

In the above example view1Position == (currentlySelectedViewPosition - n) and view2Position == (currentlySelectedViewPosition + n), where n defaults to 1 and can be changed using the following API:

scrollView.setClampTransformProgressAfter(n);

Because scale transformation is the most common, I included a helper class - ScaleTransformer, here is how to use it:

cityPicker.setItemTransformer(new ScaleTransformer.Builder()
  .setMaxScale(1.05f) 
  .setMinScale(0.8f) 
  .setPivotX(Pivot.X.CENTER) // CENTER is a default one
  .setPivotY(Pivot.Y.BOTTOM) // CENTER is a default one
  .build());

You may see how it works on GIFs.

Slide through multiple items

To allow slide through multiple items call:

scrollView.setSlideOnFling(true);

The default threshold is set to 2100. Lower the threshold, more fluid the animation. You can adjust the threshold by calling:

scrollView.setSlideOnFlingThreshold(value);

Infinite scroll

Infinite scroll is implemented on the adapter level:

InfiniteScrollAdapter wrapper = InfiniteScrollAdapter.wrap(yourAdapter);
scrollView.setAdapter(wrapper);

An instance of InfiniteScrollAdapter has the following useful methods:

int getRealItemCount();

int getRealCurrentPosition();

int getRealPosition(int position);

/*
 * You will probably want this method in the following use case:
 * int targetAdapterPosition = wrapper.getClosestPosition(targetPosition);
 * scrollView.smoothScrollTo(targetAdapterPosition);
 * To scroll the data set for the least required amount to reach targetPosition.
 */
int getClosestPosition(int position); 

Currently InfiniteScrollAdapter handles data set changes inefficiently, so your contributions are welcome.

Disabling scroll

It's possible to forbid user scroll in any or specific direction using:

scrollView.setScrollConfig(config);

Where config is an instance of DSVScrollConfig enum. The default value enables scroll in any direction.

Callbacks

  • Scroll state changes:
scrollView.addScrollStateChangeListener(listener);
scrollView.removeScrollStateChangeListener(listener);

public interface ScrollStateChangeListener<T extends ViewHolder> {

  void onScrollStart(T currentItemHolder, int adapterPosition); //called when scroll is started, including programatically initiated scroll
  
  void onScrollEnd(T currentItemHolder, int adapterPosition); //called when scroll ends
  /**
   * Called when scroll is in progress. 
   * @param scrollPosition is a value inside the interval [-1f..1f], it corresponds to the position of currentlySelectedView.
   * In idle state:
   * |view1|  |currentlySelectedView|  |view2|
   * -view1 is on position -1;
   * -currentlySelectedView is on position 0;
   * -view2 is on position 1.
   * @param currentIndex - index of current view
   * @param newIndex - index of a view which is becoming the new current
   * @param currentHolder - ViewHolder of a current view
   * @param newCurrent - ViewHolder of a view which is becoming the new current
   */
  void onScroll(float scrollPosition, int currentIndex, int newIndex, @Nullable T currentHolder, @Nullable T newCurrentHolder); 
}
  • Scroll:
scrollView.addScrollListener(listener);
scrollView.removeScrollListener(listener);

public interface ScrollListener<T extends ViewHolder> {
  //The same as ScrollStateChangeListener, but for the cases when you are interested only in onScroll()
  void onScroll(float scrollPosition, int currentIndex, int newIndex, @Nullable T currentHolder, @Nullable T newCurrentHolder);
}
  • Current selection changes:
scrollView.addOnItemChangedListener(listener);
scrollView.removeOnItemChangedListener(listener);

public interface OnItemChangedListener<T extends ViewHolder> {
  /**
   * Called when new item is selected. It is similar to the onScrollEnd of ScrollStateChangeListener, except that it is 
   * also called when currently selected item appears on the screen for the first time.
   * viewHolder will be null, if data set becomes empty 
   */
  void onCurrentItemChanged(@Nullable T viewHolder, int adapterPosition); 
}

Special thanks

Thanks to Tayisiya Yurkiv for sample app design and beautiful GIFs.

License

Copyright 2017 Yaroslav Shevchuk

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.

discretescrollview's People

Contributors

jonathan-caryl avatar srujanb avatar tcw165 avatar yarolegovich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

discretescrollview's Issues

Typo/Bug in ScaleTransformer.Builder

ScaleTransformer.Builder.setMaxScale changes ScaleTransformer.maxMinDiff field:

public Builder setMaxScale(@FloatRange(from = 0.01) float scale) {
       transformer.maxMinDiff = scale;
       return this;
}

but should change ScaleTransformer.maxScale, because ScaleTransformer.maxMinDiff would be calculated on the base of maxScale and minScale:

public ScaleTransformer build() {
        transformer.maxMinDiff = transformer.maxScale - transformer.minScale;
        return transformer;
}

Scrollview Doesnt Update Data

I Am using Firebase , Grabbing Data and Using the Scrollview in A Fragment. The Fragment needs to loaded again in the drawer to show data

How to dynamically control item width

I want to get the screen width -10dp as the width of item,
I tried doing it in onCreateViewHolder:
viewHolder= new viewHolder(_inflate);
ViewGroup.LayoutParams _layoutParams = viewHolder.mCardView.getLayoutParams();
layoutParams.width=_layoutParams.width-10;
viewHolder.mCardView.setLayoutParams(_layoutParams);
But it doesn't work.

Delete item error

When i want to delete recyclerview item,it cannot work correct.
i delete the last item,an call notifyDataSetChanged method,and the recyclerview show incorrectly;all items scroll to the right of the center.
but when i delete other position items,it work well.

Set non-selectable items?

Good day, thanks for your awesome library! Is there any way i can achieve this behavior where some items are not selectable? I mean what i'm trying to do is when i scroll to that particular position(that should not be selected), the DSV automatically scroll to other position. I already tried to do it with onScrollEnd and achieved what i wanted, but sometimes the position still get selected.

how to make the view of(current position -1)reset all the childview's state or position?

Because I have an animation in the itemView,I wanna reset the itemView
when I remove it from the screen to the left side then scroll to the middle of screen,the item view will reset,I don't wanna call notifyDataSetChanged each time.I have found that viewholder setIsRecyclable(false); It will reset the itemview when the itemview not in Screen,how do I can make the view of(current position -1)reset reset all the childview's state or position when it not in the middle of Screen?

Scrolling Behaviour a little bit disrupted , jerky inside Coordinator layout.

It doesn't influence the Collapse Modes of App Bar. Maybe touches are not being forwarded?

Implemented example as:

 List<Item> datsa = new ArrayList<>();
        datsa = Arrays.asList(
                new Item(1, "Everyday Candle", "$12.00 USD", "https://unsplash.it/200/200?image=23"),
                new Item(2, "Small Porcelain Bowl", "$50.00 USD", "https://unsplash.it/200/200?image=56"),
                new Item(3, "Favourite Board", "$265.00 USD", "https://unsplash.it/200/200?image=998"),
                new Item(3, "Favourite Board", "$265.00 USD", "https://unsplash.it/200/200?image=776"),
                new Item(3, "Favourite Board", "$265.00 USD", "https://unsplash.it/200/200?image=687"));

        discreteScrollView.setItemTransformer(new ScaleTransformer.Builder()
                .setMaxScale(1.05f)
                .setMinScale(0.8f)
                .setPivotX(Pivot.X.CENTER) // CENTER is a default one
                .setPivotY(Pivot.Y.BOTTOM) // CENTER is a default one
                .build());

        discreteScrollView.setAdapter(new ItemAdapter(datsa));
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:elevation="4dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            app:title="@null"
            app:titleTextAppearance="@style/Toolbar.TitleText">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:subtitleTextColor="@color/white"
                app:theme="@style/AppTheme.ToolbarStyle"
                app:title="@null"
                app:titleTextAppearance="@style/Toolbar.TitleText"
                app:titleTextColor="@color/white" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scrolling_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <TextView
                style="@style/TextStyle.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                android:textSize="32sp" />

            <TextView
                style="@style/TextStyle.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:fontFamily="sans-serif-light" />

            <TextView
              
                style="@style/TextStyle.Title.Sub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:fontFamily="sans-serif-light" />

                <com.yarolegovich.discretescrollview.DiscreteScrollView
                    android:id="@+id/item_picker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            <TextView
                style="@style/TextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|top"
                android:layout_marginTop="8dp"
                android:text=" DESCRIPTION" />

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/card_margin"
                app:cardBackgroundColor="@android:color/white"
                app:cardElevation="2dp"
                app:contentPadding="4dp">


                <TextView
                    style="@style/TextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:fontFamily="sans-serif-light"
                    android:lineSpacingMultiplier="1.2" />
            </android.support.v7.widget.CardView>




         
        </LinearLayout>


    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:layout_anchor="@id/scrolling_container"
        app:layout_anchorGravity="bottom|right|end"
        app:pressedTranslationZ="8dp"
        app:rippleColor="@android:color/white" />

</android.support.design.widget.CoordinatorLayout>

DiscreteScrollLayoutManager is package private

I spotted that if I want create my own LayoutManager, I have to extend DiscreteScrollLayoutManager and this is reasonable, but I saw that this class is package private, so I can't extend it. Is this intended?

Child Item loaded problem.

Hello, thanks for this great library.
It looks really nice 👍

But I found that will be laggy when we have more data on it.
I try to figure out, then I found that the DiscreteScrollView will load "all child item" when init.
So if all of my data(picture) are different, it will cause laggy and even out of memory.

My question is that why DiscreteScrollView need to loaded "all child item" and keep them?
Is it necessary or for some unknown reason?

Looking forward to your reply.
Thanks again : D

Fast Scroll

Hello.
I want to ask you if is possible to implement fast scroll over items? when user swipe to change position not only pre/next slide. Can you please guide me how is possible to implement for your ScrollView.

onScroll() method from ScrollStateChangeListener called with incorrect params when call smoothScrollToPosition()

When I call srollView.smoothScrollToPosition(pos) and difference between pos and old position, for example, is 2 I got this results in CatLog:

08-05 18:18:38.818 20927-20927/com.some D/CropFormAdapter: onScrollStart() 0
08-05 18:18:38.835 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.14285715
08-05 18:18:38.852 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.2857143
08-05 18:18:38.868 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.42380953
08-05 18:18:38.885 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.54761904
08-05 18:18:38.901 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.67142856
08-05 18:18:38.918 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.78571427
08-05 18:18:38.935 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -0.9047619
08-05 18:18:38.951 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:38.969 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:38.985 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.000 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.017 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.033 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.051 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.066 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.084 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.099 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.117 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.133 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.150 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.167 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.184 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.200 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.215 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.233 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.249 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0
08-05 18:18:39.266 20927-20927/com.some D/CropFormAdapter: onScroll() currentHolder = 0 newCurrentHolder = 1 scrollPosition = -1.0 

When scroll started between 0 and 1 positions - ok, but when the scroll goes between 1 and 2 positions I got the incorrect viewHolders objects in the callback parameters (they have 0 and 1 positions, when I expected to get viewHolders with 1 and 2 positions), and scrollPosition in this case is always -1.0 (08-05 18:18:38.969) in the logs.

my code

How to decrease the space between items?

Hey. I must say that this is a very interesting library. Good work guys. But i am having hard time to decrease the space between recycler items. I want to decrease the space please look at this below image.

capture

What i have tried so far is below.
This is the item decorator.

` public class RecyclerItemDecorator extends RecyclerView.ItemDecoration {

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.left = 0;
    outRect.right = 0;
    outRect.top = 0;
    outRect.bottom = 0;
    //super.getItemOffsets(outRect, view, parent, state);
}
}

`
Here is the java code from activity

binding.rcTest.addItemDecoration(new RecyclerItemDecorator());

Maybe there is mistake into fling algorithm

Sorry for my English. I use vertical scrollview, but i could not make it to work with fling. Then, i looked in the code and saw that:

public void onFling(int velocityX, int velocityY) {
int velocity = orientationHelper.getFlingVelocity(velocityX, velocityY);
int throttleValue = shouldSlideOnFling ? Math.abs(velocityX / flingThreshold) : 1;

I suggest that there should be
Math.abs(velocity/flingThreshold)
instead of
Math.abs(velocityX/flingThreshold)

Invalid target position when scrolling

Hi, Thanks for the great library. I encountered an app crash with the following stacktrace when using the latest (1.3.0) version. I simply fetch a list from an api and update the RV adapter with notifyDatasetChanged() after setting the new list. It works fine if there are more than 2 items in the list. But it crashes if there are 2 or fewer items in the list. The crash occurs soon after I scroll the first item.

java.lang.IllegalArgumentException: Invalid target position
    at android.support.v7.widget.RecyclerView$SmoothScroller.start(RecyclerView.java:11132)
    at android.support.v7.widget.RecyclerView$LayoutManager.startSmoothScroll(RecyclerView.java:7871)
    at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.startSmoothPendingScroll(DiscreteScrollLayoutManager.java:463)
    at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.onScrollEnd(DiscreteScrollLayoutManager.java:390)
    at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.onScrollStateChanged(DiscreteScrollLayoutManager.java:351)
    at android.support.v7.widget.RecyclerView.dispatchOnScrollStateChanged(RecyclerView.java:4746)
    at android.support.v7.widget.RecyclerView.setScrollState(RecyclerView.java:1434)
    at android.support.v7.widget.RecyclerView.cancelTouch(RecyclerView.java:3023)
    at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3001)
    at android.view.View.dispatchTouchEvent(View.java:11721)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2929)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2636)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2931)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2650)
    at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445)
    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828)
    at android.app.Activity.dispatchTouchEvent(Activity.java:3292)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
    at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:407)
    at android.view.View.dispatchPointerEvent(View.java:11960)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4776)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4590)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)

Dependent on size of first item in adapter

Problem : all list items take up the size of the first item in the adapter list - this is a terrible assumption for a generic library like this one!

My adapter has a header of toolbar height ?android:attr/actionBarSize and then a list variable length images - something like facebook's newsfeed. I ended up with a vertical stack of lots of items, all of the same toolbar height.

Is ther an easy fix to this? I haven't gone through the code yet, hence not aware of how much effort this would require.

Can not give dynamic height to items

Hello, I am in a great problem ..I am using your library for showing images. There are different types of images some are portrait some are landscape but I have tried every possibility but I am unable to give dynamic height. i tried in onCreateViewHolder and in onBindViewHolder .Please help

Not working with android Appcompat

I'm using android appcompat lib and when I try to add this lib in gradle file under dependencies on sync android studio give an error for support libraries like appcompat, recyclerview etc.
This is what I get on sync

Error:Failed to resolve: com.android.support:appcompat-v7:26.0.0
<a href="install.m2.repo">Install Repository and sync project</a><br><a href="openFile:E:/Android_Studio_and_SDK/Opencart_android/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>

And this

Error:Failed to resolve: com.android.support:recyclerview-v7:26.0.0
<a href="install.m2.repo">Install Repository and sync project</a><br><a href="openFile:E:/Android_Studio_and_SDK/Opencart_android/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>

But when I remove this lib (DiscreteScrollView) everything is work fine as usual. I tried more than once.
I'm using

  1. Android studio 2.3.3

  2. gradle-3.3

  3. Java 8

Single Scroll implementation.

Hi , how do I use just single scroll ?
I do not want to use the Infinite scroll. can I use single scroll adapter ?

IndexOutOfBoundsException

java.lang.IndexOutOfBoundsException: Invalid item position -1(-1). Item count:6
i dont understand. thank you

ScrollView isn't working properly with height of item = wrap_content

I am trying to use this library with a CardView as a viewholder. The card view height is set to wrap_content. The card_view has a textview inside it which makes it bigger or smaller according to the size of this text_view.

The ScrollView always show items with height equal to the height of the first item. Is there a way to make such behaviour work with DiscreteScrollView ?

Horizontal scrolling within ViewPager fragment not working correctly

Hi,

I have a ViewPager set up with 3 fragments. The 3rd fragment, contains a relativeLayout with the DiscreteScrollView located in the lower half.

The general implementation works fine, displays correctly, transforms etc. However, when it comes to scrolling, if you try a quick "fling" type scroll, it does not register on the DiscreteScrollView, instead, the ViewPager takes the touch event. If you however, hold your finger over an item in the DiscreteScrollView for a split second or more, and then attempt to scroll, it works fine. This also occurs if you very casually or "slowly" attempt to scroll (possibly due to the split-second extra contact over the DiscreteScrollView,allowing the touch to be registered for the DiscreteScrollView instead of the ViewPager). Furthermore, if you initially scroll up or down to initial the contact - without lifting your finger - and then scrolling horizontally, it also works every time.

I have tested this on various devices with the same result. I believe there's a problem with the initial touch registration for the DiscreteScrollView. Could this be a problem with the internal LayoutManager and how it handles scrolls?

As previously, we had a standard recyclerView in the exact same location and setup and it handled the scrolling flawlessly as would be expected for such a trivial aspect.

We have tried various properties, enabling and disabling NestedScrolling, various layout formats (such as using NestedScrollViews), nothing made any difference at all.

Any help or insight into this at all would be greatly appreciated!

requiresFadingEdge doesn't work

I tried adding android:requiresFadingEdge="vertical|horizontal" to DiscreteScrollView in xml, but it had no effect. How can I add fading edge to it?

Help: Gradual zoom decrease

Hi, is there a way to decrease zoom gradualy among items for example the current item in list is the most zoomed now what I mean is to zoom also adiacent items for example by 50% then others by 25% ecc...

notifyDataSetChanged issue

Calling on notifyDataSetChanged() list scroll to the first position, so how to notify particular item or adapter?

ViewHolder cannot show correct width

1111

I am trying to implement a Google play store like screenshot scroll view with different image width.
As you can see from the image above the last image can not show the full width (the black border is the background of the viewholder), I have tried to set the LayoutParams in runtime but still nothing change

 mViewImage.setLayoutParams(new RelativeLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight()));
 mViewImage.setImageBitmap(bitmap);

and the holder xml like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/wrapper"
    android:background="@android:color/black"
    android:padding="4dp">

    <ImageView
        android:id="@+id/image"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

is there any hints to solve this problem ?

java.lang.IndexOutOfBoundsException: Invalid item position 20(20). Item count:20

I've got the following exception:

java.lang.IndexOutOfBoundsException: Invalid item position 20(20). Item count:20
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5466)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
       at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
       at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.layoutView(DiscreteScrollLayoutManager.java:160)
       at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.fill(DiscreteScrollLayoutManager.java:133)
       at com.yarolegovich.discretescrollview.DiscreteScrollLayoutManager.onLayoutChildren(DiscreteScrollLayoutManager.java:93)
       at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
       at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
       at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
...

Repro:

  1. Add N items
  2. Remove N items - in DiscreteScrollLayoutManager.onItemsRemoved() currentPosition becomes 0 but I think it is more correct to become -1 (NO_POSITION) as an index range of an empty adapter is also empty. So currentPosition is better to be reverted to the initial NO_POSITION.
  3. Add some 20 items - in DiscreteScrollLayoutManager.onItemsAdded() currentPosition becomes 0 + 20 = 20 but it should be 19.
public void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount) {
        if (getItemCount() == 0) {
            currentPosition = NO_POSITION;
        } else if (currentPosition >= positionStart) {

            // What about this correction?
            // currentPosition = Math.max(0, currentPosition - itemCount);
            currentPosition = Math.max(NO_POSITION, currentPosition - itemCount);
        }
    }

Scrolling problems with async remote images

Hi,
im facing issues with scrolling its not smooth and when scrolling feels like "it's trying hard" to scroll to the next element.
I feed my recycle adapter with remote data and all images are loading remotely with glide.
The orientation of the descreteScrollview is horizontal.

Can someone please recommend, some fixes for smooth scrolling.

Thanks in advance.

Sliding direction

Hi, Your project is sliding from the left to the right, I want to change from the right to the left slide, can you tell me how to do it? And I do not want to use DiscreteScrollView.scrollToPostion () this method to directly display the last one of the data, and then slide from the right to achieve this. Can you help me?

How to scroll through more than one item?

Now I can only scroll through one item at a time, then it stops scrolling. Is there any way to scroll through multiple items at one scrolling? I call this method smoothScrollToPosition() when users tap on a item to achieve this. But when users slide left or right, only one item and then stops. Thanks!

Pre-binding adjacent items

Hi, is it possible to tweak the condition when an item view is laid out? My item view is fullscreen and that causes a little problem:

https://gifs.com/gif/58zJlZ

I need to bind the next item in background, that is - not just when a small edge of it becomes visible but earlier. In other words when item N becomes current item then at same time N+1 to be bound. As you can see from the video, my images are requested from network when the view appears, but that's too late. That should be done when the view is still off-screen. Is it possible to implement an "offscreen limit" like 1-2 items on both sides or to maintain an extra layout space like LinearLayoutManager does with https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#getExtraLayoutSpace(android.support.v7.widget.RecyclerView.State)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.