HorizontalPicker icon indicating copy to clipboard operation
HorizontalPicker copied to clipboard

Set initial selected item or view

Open jsonfellin opened this issue 7 years ago • 12 comments

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?

jsonfellin avatar Aug 08 '17 16:08 jsonfellin

try to see --> #3

chemickypes avatar Sep 01 '17 13:09 chemickypes

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

jsonfellin avatar Sep 01 '17 13:09 jsonfellin

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

chemickypes avatar Sep 01 '17 13:09 chemickypes

@chemickypes Yeah, see my first comment above.

jsonfellin avatar Sep 01 '17 15:09 jsonfellin

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

adityagohad avatar Sep 01 '17 15:09 adityagohad

@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 avatar Sep 04 '17 09:09 chemickypes

@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

chemickypes avatar Sep 04 '17 09:09 chemickypes

@adityagohad Any fix for this issue yet?

paramarthasaha96 avatar Oct 02 '17 15:10 paramarthasaha96

Anyone can solve this issue ? Please give me solution

nayankukadiya avatar Jan 24 '18 06:01 nayankukadiya

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

ReeMiXeR avatar Jan 29 '18 15:01 ReeMiXeR

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

jsonfellin avatar Apr 10 '18 21:04 jsonfellin

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

Gradergage avatar Jul 08 '19 01:07 Gradergage