Вы можете использовать инверсию порядка элементов в HTML. Затем, используя order
, как в ответе Michael_B , вы можете использовать flex-direction: row-reverse;
или flex-direction: column-reverse;
в зависимости от вашего макета.
Рабочий пример:
.flex {
display: flex;
flex-direction: row-reverse;
/* Align content at the "reversed" end i.e. beginning */
justify-content: flex-end;
}
/* On hover target its "previous" elements */
.flex-item:hover ~ .flex-item {
background-color: lime;
}
/* styles just for demo */
.flex-item {
background-color: orange;
color: white;
padding: 20px;
font-size: 3rem;
border-radius: 50%;
}
5
4
3
2
1
You can look at this answer. You can also go with a custom adapter, but the solution below is fine for simple cases.
Here's a re-post:
So if you came here because you want to have both labels and values in the Spinner - here's how I did it:
Spinner
the usual wayarray.xml
file -- one array for labels, one array for valuesSpinner
with android:entries="@array/labels"
When you need a value, do something like this (no, you don't have to chain it):
String selectedVal = getResources().getStringArray(R.array.values)[spinner.getSelectedItemPosition()];
Если Вам не нужен разделенный класс, я имею в виду просто простой адаптер, отображенный на Вашем объекте. Вот мой код на основе обеспеченных функций ArrayAdapter.
И потому что Вы, возможно, должны были бы добавить объект после создания адаптера (например, объект базы данных асинхронная загрузка).
Простой, но эффективный.
editCategorySpinner = view.findViewById(R.id.discovery_edit_category_spinner);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner, as you can see i have no data at this moment
editCategorySpinner.setAdapter(dataAdapter);
final ArrayAdapter<Category> dataAdapter = new ArrayAdapter<Category>
(getActivity(), android.R.layout.simple_spinner_item, new ArrayList<Category>(0)) {
// And the "magic" goes here
// This is for the "passive" state of the spinner
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// I created a dynamic TextView here, but you can reference your own custom layout for each spinner item
TextView label = (TextView) super.getView(position, convertView, parent);
label.setTextColor(Color.BLACK);
// Then you can get the current item using the values array (Users array) and the current position
// You can NOW reference each method you has created in your bean object (User class)
Category item = getItem(position);
label.setText(item.getName());
// And finally return your dynamic (or custom) view for each spinner item
return label;
}
// And here is when the "chooser" is popped up
// Normally is the same view, but you can customize it if you want
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = (TextView) super.getDropDownView(position, convertView, parent);
label.setTextColor(Color.BLACK);
Category item = getItem(position);
label.setText(item.getName());
return label;
}
};
И затем можно использовать этот код (я не мог поместить Категорию [] в конструкторе адаптера, потому что данные загружаются separatly).
Примечание, которое adapter.addAll (объекты) обновляют счетчик путем вызова notifyDataSetChanged () во внутреннем.
categoryRepository.getAll().observe(this, new Observer<List<Category>>() {
@Override
public void onChanged(@Nullable final List<Category> items) {
dataAdapter.addAll(items);
}
});
У меня отлично работает, код, необходимый для функции getResource (), выглядит следующим образом:
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> spinner, View v,
int arg2, long arg3) {
String selectedVal = getResources().getStringArray(R.array.compass_rate_values)[spinner.getSelectedItemPosition()];
//Do something with the value
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Просто нужно убедиться (самостоятельно) в значениях в двух массивах выровнены правильно!