public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
LayoutInflater inflater;
ArrayList<String> list;
private static final int TYPE_HEADER = 0;
private static final int TYPE_LIST = 1;
public RecyclerViewAdapter(Context context) {
inflater = LayoutInflater.from(context);
list = new ArrayList<String>();
list.add("İstanbul");
list.add("Ankara");
list.add("Erzincan");
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == TYPE_HEADER) {
view = inflater.inflate(R.layout.header, parent, false);
return new ViewHolder(view, viewType);
} else if (viewType == TYPE_LIST) {
view = inflater.inflate(R.layout.item, parent, false);
return new ViewHolder(view, viewType);
} else {
return null;
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(holder.view_type == TYPE_LIST) {
holder.txtItem.setText(list.get(--position));
}
else if(holder.view_type == TYPE_HEADER) {
}
}
@Override
public int getItemCount() {
return list.size() + 1;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView txtItem;
int view_type;
public ViewHolder(View itemView, int viewType) {
super(itemView);
if (viewType == TYPE_LIST) {
txtItem = (TextView) itemView.findViewById(R.id.textView);
view_type = 1;
} else if (viewType == TYPE_HEADER) {
view_type = 0;
}
}
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEADER;
return TYPE_LIST;
}
}