I came up with BaseAdapterUnscrollable. Basically it just adds views to ViewGroup container. Implementation is a bit like BaseAdapter. It’s pretty convenient if you use a few non-scrollable lists like that in you project.
In onCreate:
PeopleAdapter peopleAdapter = new PeopleAdapter(this, personList, containerPeopleLinearLayout);
peopleAdapter.setOnItemClickListener(this);
peopleAdapter.drawItems();
Your specific adapter:
public class PeopleAdapter extends BaseAdapterNonScrollable<Person> {
public PeopleAdapter(Context context, List<Person> items, LinearLayout container) {
super(context, items, container);
}
@Override
public View getView(View container, Person person) {
TextView textView = (TextView) LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, null);
textView.setText(person.getName());
return textView;
}
}
BaseAdapterNonScrollable (just copy):
public abstract class BaseAdapterNonScrollable<T> implements NonScrollable, OnItemClick {
public Context context;
private ViewGroup containerViewGroup;
private List<T> itemObjectList;
private OnItemClick itemClickListener;
public BaseAdapterNonScrollable(Context context, List<T> items, ViewGroup containerViewGroup) {
this.context = context;
this.itemObjectList = items;
this.containerViewGroup = containerViewGroup;
}
@Override
public void drawItems() {
if (containerViewGroup == null || itemObjectList.size() == 0) {
return;
}
if (containerViewGroup.getChildCount() > 0) {
containerViewGroup.removeAllViews();
}
//draw all items
for (int i = 0; i < itemObjectList.size(); i++) {
final int position = i;
final View itemView = getView(containerViewGroup, itemObjectList.get(i));
if (itemView != null) {
containerViewGroup.addView(itemView);
//handle item click event
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemClick(itemObjectList, position);
}
}
});
}
}
}
public void setOnItemClickListener(OnItemClick onItemClick) {
this.itemClickListener = onItemClick;
}
public abstract View getView(View container, T itemObject);
@Override
public void onItemClick(List<?> itemList, int position) {
}
}
Interfaces
public interface NonScrollable {
void drawItems();
}
public interface OnItemClick {
void onItemClick(List<?> itemList, int position);
}