Created
September 27, 2017 00:09
-
-
Save luisrenemas/fb3c42f2ff8dfab6aba8bec300516f02 to your computer and use it in GitHub Desktop.
Adapter personalizado para Spinner - Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<shape android:shape="rectangle"> | |
<stroke | |
android:width="1dp" | |
android:color="@color/color_verde" /> | |
<size android:height="40dp"/> | |
<padding android:left="4dp" android:right="4dp" android:top="10dp" android:bottom="10dp"/> | |
</shape> | |
</item> | |
</selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<shape android:shape="rectangle"> | |
<solid android:color="@color/accent"/> | |
<stroke | |
android:width="1dp" | |
android:color="@color/accent" /> | |
<size android:height="40dp"/> | |
<padding android:left="4dp" android:right="4dp" android:top="9dp" android:bottom="9dp"/> | |
</shape> | |
</item> | |
</selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<shape android:shape="rectangle"> | |
<size android:height="40dp"/> | |
<padding android:left="4dp" android:right="4dp" android:top="10dp" android:bottom="10dp"/> | |
</shape> | |
</item> | |
</selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[Combo categoria recordatorios] | |
Spinner mSpinnerListaCategotegorias; | |
//Variables para el combo | |
List<String> comboListaCategorias; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
//Spinner | |
mSpinnerListaCategotegorias = (Spinner) v.findViewById(R.id.sp_categorias_recordatorios); | |
//[INICIO COMBO] | |
poblarSpinner(); | |
//[FIN COMBO] | |
} | |
public void poblarSpinner(){ | |
comboListaCategorias = new ArrayList<>(); | |
int sizeListaCat = mManagerRecordatorios.getListaCategorias().size(); | |
comboListaCategorias.add(getString(R.string.selecciona_categoria_spinner)); | |
for(int i = 0; i < sizeListaCat; i++){ | |
comboListaCategorias.add(mManagerRecordatorios | |
.getListaCategorias().get(i).getCategorioRecordatorio()); | |
} | |
mSpinnerListaCategotegorias.setAdapter(new SpinnerCategoriasAdaptador( | |
getActivity().getApplicationContext(), | |
comboListaCategorias)); | |
mSpinnerListaCategotegorias.setOnItemSelectedListener(this); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.a01luisrene.multirecordatorio.ui.adaptadores; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.support.v4.content.ContextCompat; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import com.a01luisrene.multirecordatorio.R; | |
import com.a01luisrene.multirecordatorio.utilidades.Utilidades; | |
import java.util.List; | |
public class SpinnerCategoriasAdaptador extends BaseAdapter { | |
private List<String> mListaCategorias; | |
private Context mContext; | |
public SpinnerCategoriasAdaptador(Context context, List<String> listaCategorias){ | |
this.mContext = context; | |
this.mListaCategorias = listaCategorias; | |
} | |
@Override | |
public int getCount() { | |
return mListaCategorias.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return mListaCategorias.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@SuppressLint("ViewHolder") | |
@Override | |
public View getView(int position, View view, ViewGroup parent) { | |
Drawable drawable_titulo_main, drawable_titulos; | |
drawable_titulo_main = ContextCompat.getDrawable(mContext, R.drawable.item_main_categoria_spinner); | |
drawable_titulos = ContextCompat.getDrawable(mContext, R.drawable.item_titulos_categorias_spinner); | |
LayoutInflater layoutInflater = LayoutInflater.from(mContext); | |
view = layoutInflater.inflate(android.R.layout.simple_spinner_item, null); | |
TextView tituloCategoria =(TextView)view.findViewById(android.R.id.text1); | |
String titulo = mListaCategorias.get(position); | |
String tituloFormateado; | |
if(titulo.length() >= 40){ | |
tituloFormateado = titulo.substring(0, 40) + "..."; | |
}else{ | |
tituloFormateado = titulo; | |
} | |
if(position==0) { //Primer elemento | |
tituloCategoria.setTextColor(Color.parseColor("#FFFFFF")); | |
//Condicionar para smartphone & tablets | |
if(Utilidades.smartphone){ | |
tituloCategoria.setTextSize(16); | |
}else{ | |
tituloCategoria.setTextSize(20); | |
} | |
tituloCategoria.setBackground(drawable_titulo_main); | |
}else {//N items | |
tituloCategoria.setTextColor(Color.parseColor("#212121")); | |
//Condicionar para smartphone & tablets | |
if(Utilidades.smartphone){ | |
tituloCategoria.setTextSize(14); | |
}else{ | |
tituloCategoria.setTextSize(18); | |
} | |
tituloCategoria.setBackground(drawable_titulos); | |
} | |
tituloCategoria.setText(tituloFormateado); | |
return view; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public Cursor cargarCursorCategorias() { | |
String[] columnas = new String[]{ID_CATEGORIA, | |
RUTA_IMAGEN_CATEGORIA, | |
TITULO_CATEGORIA}; | |
String order_by = ID_CATEGORIA + " DESC"; | |
return super.getDb().query(TABLA_CATEGORIAS_RECORDATORIOS, columnas, null, null, null, null, order_by); | |
} | |
@Override | |
public List<Categorias> getListaCategorias(){ | |
List<Categorias> lista = new ArrayList<>(); | |
Cursor cursor = cargarCursorCategorias(); | |
while (cursor.moveToNext()){ | |
Categorias catRecordatorio = new Categorias(); | |
catRecordatorio.setId(cursor.getString(0)); | |
catRecordatorio.setImagen(cursor.getString(1)); | |
catRecordatorio.setCategorioRecordatorio(cursor.getString(2)); | |
lista.add(catRecordatorio); | |
} | |
cursor.close(); | |
return lista; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment