我試圖想出一些方法來實現下一種視圖。目前我已經嘗試創建一個列表視圖,並使選定的項目更大。但是我不能讓選定的項目始終處於我的視野中。所以現在我試圖用一個數字選擇器來獲得這個。修改Numberpicker中的選定項目Android
但我沒有找到任何方法來隱藏分隔欄,並使選定的項目和視圖的其餘部分不同。這個想法是在底部的圖像中得到的東西。
希望有人能幫助我!謝謝!
我試圖想出一些方法來實現下一種視圖。目前我已經嘗試創建一個列表視圖,並使選定的項目更大。但是我不能讓選定的項目始終處於我的視野中。所以現在我試圖用一個數字選擇器來獲得這個。修改Numberpicker中的選定項目Android
但我沒有找到任何方法來隱藏分隔欄,並使選定的項目和視圖的其餘部分不同。這個想法是在底部的圖像中得到的東西。
希望有人能幫助我!謝謝!
我想實現我的項目中,我希望我的回收站取景當中的項目較爲突出的一個非常類似的效果。 就我而言,這個項目只是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);
}
});
我認爲ListView控件可能比NumberPicker更多的配置。
你可以做的是使用不同的行佈局dependind如果是中間的一個或其他的,所以你的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和代碼佈局。喲可以改變條件,如果你想要的「特殊」項目任何其他方式。
以下是一些選擇器自定義顯示值:
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
你可以用一個持有人的正常實施這一使用RecyclerView項目和一個選定項目的持有人。
裏面你RecyclerView適配器
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;
}
}