adityagohad / HorizontalPicker

A simple, customizable and easy to use picker where centre view is scaled up
Apache License 2.0
364 stars 45 forks source link

Set initial selected item or view #5

Open jsonfellin opened 7 years ago

jsonfellin commented 7 years ago

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 commented 7 years ago

try to see --> #3

jsonfellin commented 7 years ago

@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 commented 7 years ago

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

jsonfellin commented 7 years ago

@chemickypes Yeah, see my first comment above.

adityagohad commented 7 years ago

@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 :sweat_smile:

chemickypes commented 7 years ago

@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 commented 7 years ago

@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 commented 7 years ago

@adityagohad Any fix for this issue yet?

nayankukadiya commented 6 years ago

Anyone can solve this issue ? Please give me solution

ReeMiXeR commented 6 years ago

@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 commented 6 years ago

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

Gradergage commented 5 years ago

@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?