Merhaba arkadaşlar,
Custom Spinner Adapter Örnek kodlarını sizlerle paylaşmak istiyorum.
Java kısmı kod olarak paylaştım xml kısmına proje dosyasından bakabilirsiniz.
URL: https://disk.yandex.com.tr/d/RzOykwqh3lIZ4w
MainActivity.java
ListItem.java
CustomSpinnerAdapter.java
Custom Spinner Adapter Örnek kodlarını sizlerle paylaşmak istiyorum.
Java kısmı kod olarak paylaştım xml kısmına proje dosyasından bakabilirsiniz.
URL: https://disk.yandex.com.tr/d/RzOykwqh3lIZ4w
MainActivity.java
Kod:
package com.aturan.customspinnerexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private TextView textView;
private CustomSpinnerAdapter adapter;
private ListItem listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.main_aktivity_spinner);
textView = findViewById(R.id.main_aktivity_textview);
adapter = new CustomSpinnerAdapter(this, ListItem.getData(this));
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
listItem = (ListItem) parent.getItemAtPosition(position);
textView.setText(listItem.getStrBayrak());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
listItem = (ListItem) parent.getItemAtPosition(0);
textView.setText(listItem.getStrBayrak());
}
});
}
}
ListItem.java
Kod:
package com.aturan.customspinnerexample;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.util.ArrayList;
public class ListItem {
private Bitmap imgBayrak;
private String strBayrak;
public ListItem(Bitmap imgBayrak, String strBayrak) {
this.imgBayrak = imgBayrak;
this.strBayrak = strBayrak;
}
public Bitmap getImgBayrak() {
return imgBayrak;
}
public String getStrBayrak() {
return strBayrak;
}
public static ArrayList<ListItem> getData(Context context) {
ArrayList<ListItem> listItems = new ArrayList<>();
listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.turkiye), "Türkiye"));
listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.almanya), "Almanya"));
listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.ingiltere), "İngiltere"));
listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.fransa), "Fransa"));
listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.cin), "Çin"));
listItems.add(new ListItem(BitmapFactory.decodeResource(context.getResources(), R.drawable.abd), "ABD"));
return listItems;
}
}
CustomSpinnerAdapter.java
Kod:
package com.aturan.customspinnerexample;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomSpinnerAdapter implements SpinnerAdapter {
private Context context;
private ArrayList<ListItem> listItems;
private ImageView imgBayrak;
private TextView textBayrak;
public CustomSpinnerAdapter(Context context, ArrayList<ListItem> listItems) {
this.context = context;
this.listItems = listItems;
}
@Override
public View getDropDownView(int position, View view, ViewGroup parent) {
view = LayoutInflater.from(context).inflate(R.layout.spinner_item_dropdown, null);
imgBayrak = view.findViewById(R.id.spinner_item_dropDown_imgBayrak);
textBayrak = view.findViewById(R.id.spinner_item_dropDown_textBayrak);
imgBayrak.setImageBitmap(listItems.get(position).getImgBayrak());
textBayrak.setText(listItems.get(position).getStrBayrak());
return view;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem(int position) {
return listItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view = LayoutInflater.from(context).inflate(R.layout.spinner_item, null);
imgBayrak = view.findViewById(R.id.spinner_item_imgBayrak);
textBayrak = view.findViewById(R.id.spinner_item_textBayrak);
imgBayrak.setImageBitmap(listItems.get(position).getImgBayrak());
textBayrak.setText(listItems.get(position).getStrBayrak());
return view;
}
@Override
public int getItemViewType(int position) {
return 1;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean isEmpty() {
if (listItems.size() > 0)
return false;
else
return true;
}
}