Search code examples
androidxmllistviewnumberpicker

Modify selected item in Numberpicker Android


I'm trying to figure some way to achieve the next kind of view. At the moment I have tried to create a Listview and just make bigger the selected item. But I cannot make the selected item always be in the middle of my view. So now I'm trying to get this with a numberpicker.

But I didn't find any way to hide the divider bar, and make different the selected item and the rest of the view. The idea is get something like in the bottom image.

enter image description here


Solution

  • I wanted to achieve a pretty similar effect on one of my project, where I wanted the middle item of my recycler view to be more prominent. In my case, that said item is only z-translated to give an impression of focus, but the result is pretty similar to what you're describing. I'll post my code here, in case it could help you go in the right direction :

    //We're on the onCreateView in a fragment here
    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
    
                //First I find the first visible element
                int firstVisiblePosition = mLayoutManager.findFirstVisibleItemPosition();
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (firstVisiblePosition != -1) {
                        int lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
    
                        int itemHeight = mLayoutManager.getChildAt(0).getMeasuredHeight();
                        int itemTop = mLayoutManager.getChildAt(0).getTop();
    
                        //We use a '+' as itemTop will be negative
                        int delta = itemHeight + itemTop;
    
                        int currentItemToBeFocused = (delta < (itemHeight / 2)) ? 1 : 0;
    
                        //Reset the z-translation of other items to 0
                        for (int i = 0, last = (lastVisiblePosition - firstVisiblePosition); i <= last; ++i) {
                            if (mLayoutManager.getChildAt(i) != null) {
                                mLayoutManager.getChildAt(i).setTranslationZ(0);
                            }
                        }
                        //And set the z-translation of the current "centered" item
                        if (mLayoutManager.getChildAt(currentItemToBeFocused) != null) {
                            mLayoutManager.getChildAt(currentItemToBeFocused).setTranslationZ(10);
                        }
                    }
                }
            }
    
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });