Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set initial selected item or view #5

Open
jsonfellin opened this issue Aug 8, 2017 · 12 comments
Open

Set initial selected item or view #5

jsonfellin opened this issue Aug 8, 2017 · 12 comments

Comments

@jsonfellin
Copy link

I don't see a way to set the initial selected view or item. RecyclerView.smoothScrollToPosition() doesn't seem to scroll to the correct position. Any ideas?

@chemickypes
Copy link

try to see --> #3

@jsonfellin
Copy link
Author

@chemickypes Thanks, yeah I did and it doesn't work reliably. The library should be able to set a default position (something like selectedIndex), not scrolling by pixels.

@chemickypes
Copy link

@jsonfellin mh, sure, you're right! Did you try to use smoothScrollToPosition() method?

@jsonfellin
Copy link
Author

@chemickypes Yeah, see my first comment above.

@adityagohad
Copy link
Owner

@jsonfellin Hey thanx for pointing that out will try to fix it ASAP.
And even I have experienced RecyclerView.smoothScrollToPosition() missing the given index.

@chemickypes hey man I am open for suggestion 😅

@chemickypes
Copy link

@jsonfellin Oh sorry, I was confused! I have another question: when do you call this method? I had a similar problem: I put smoothScrollBy() method inside a OnGlobalLayoutListener when data is not already loaded and it's not working. So, I had to wait for the finish of notifyDataSetChanched().

try something like this -->

      slimAdapter.notifyDataSetChanged();
        post(new Runnable() {
            @Override
            public void run() {
                //make your selection
                //RecyclerView.smoothScrollToPosition() 
            }
        });

@adityagohad we have to think about it!

@chemickypes
Copy link

@adityagohad In my project of HorizontalPicker I put a method in the custom LinerLayout manager :

public void select(int position) {
        scrollToPositionWithOffset(position,10);
        rv.smoothScrollBy(1,0);
    }

The first method is inside LinearLayoutManager to scroll to the wanted position and the latter let RecyclerView to scroll (very tiny, 1 px) and select the element.

This method is called when you add data in the Horizontal Picker

public void addData(List<?> data, final int selectedItem){
        slimAdapter.updateData(data);

        post(new Runnable() {
            @Override
            public void run() {
                selectItem(selectedItem);
            }
        });
    }
...
 public void selectItem(int position) {

        try {
            if (position < slimAdapter.getItemCount()) {
                pickerLayoutManager.select(position);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

I don't know if this idea can help @jsonfellin

@paramarthasaha96
Copy link

@adityagohad Any fix for this issue yet?

@nayankukadiya
Copy link

Anyone can solve this issue ? Please give me solution

@ReeMiXeR
Copy link

ReeMiXeR commented Jan 29, 2018

@nayankukadiya @paramarthasaha96 @jsonfellin
I tried to figure out how to solve this issue for a while and finally... i think, i found solution.
DONT HARDCODE RecyclerView padding, use dynamic padding adding
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int padding = (displayMetrics.widthPixels - VIEW_HOLDER_WIDTH_IN_PIXELS) / 2;
mRecyclerView.setPadding(padding, 0, padding, 0);
now, I can use smoothScroll on recycler and init position is ok. so, just play with paddings, guys.
p. s. thanks for amazing library!

@jsonfellin
Copy link
Author

@ReeMiXeR Doesn't that assume each view holder has equal width?

@Gradergage
Copy link

@adityagohad
smoothScrollToPosition(*) launches animation, so it causes dragging when i launch my fragment (maybe it caused by centering of picked element). If i use scrollToPosition, i have static RV but without scaled items. How can i fix that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants