Change selected item in Numpicker Android

I'm trying to somehow figure out how to achieve the next look. At the moment, I tried to create a Listview and simply increased the selected item. But I can not make the selected element always in the middle of my view. So now I'm trying to get this with a numpicker.

But I did not find a way to hide the separator panel and make another selected element and the rest of the view. The idea is to get something like a bottom image.

Hope someone can help me! Thank!

enter image description here

+4
source share
4 answers

, , . z- , , . , :

//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);
        }
    });
+2

, ListView , NumberPicker.

, , , getView (...) :

public View getView(int position, View convertView, ViewGroup parent) {
    if (position == 1) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.focused_layout, parent, false);
        // Do whatever with this view

    } else {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.not_focused_layout, parent, false);
        // Do whatever with this view

    }

    return convertView;
}

XML, . Yo , "" .

+3

:

 final NumberPicker aNumberPicker = new NumberPicker(context);
    List<Integer> ids = getIds();
    aNumberPicker.setMaxValue(ids.size()-1);
    aNumberPicker.setMinValue(0);
    mDisplayedIds = new String[ids.size()];
    for (int i = 0; i < ids.size(); i++) {
        mDisplayedIds[i] = "Nombre"+String.valueOf(ids.get(i)) ;
    }
    aNumberPicker.setDisplayedValues(mDisplayedIds);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
    RelativeLayout.LayoutParams numPickerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    numPickerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    relativeLayout.setLayoutParams(params);
    relativeLayout.addView(aNumberPicker, numPickerParams);

, , ​​ AndroidPicker

+3
source

You can implement this using RecyclerView with one holder for a regular item and one holder for a selected item.

Inside the RecyclerView Adapter

private static int SELECTED_ITEM_POSITION = 2;
private static int NORMAL_ITEM = 1;
private static int SELECTED_ITEM = 2;

@Override
public int getItemViewType(int position)
{
    if(position == SELECTED_ITEM_POSITION)
        return SELECTED_ITEM;
    else
        return NORMAL_ITEM;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    if(viewType == SELECTED_ITEM)
    {
        YourSelectedViewHolder selectedViewHolder = (YourSelectedViewHolder)layoutInflater.inflate(R.layout.selected_item_layout, parent, false);
        return selectedViewHolder;
    }
    else //viewType == NORMAL_ITEM
    {
        YourNormalViewHolder normalViewHolder = (YourNormalViewHolder)layoutInflater.inflate(R.layout.normal_item_layout, parent, false);
        return normalViewHolder;
    }
}
+3
source

All Articles