I should want a non scrollable ListView, and show the entire ListView. It\'s because my entire screen is a ScrollView, and I dispatch widgets with a RelativeLayout, so I don
Depending on what exactly you are trying to do, you may be able to solve your problem by ditching the ScrollView
and instead using ListView
's addHeaderView(View) and addFooterView(View).
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);
}
I found a very simple solution for this. Just get the adapter of the listview and calculate its size when all items are shown. The advantage is that this solution also works inside a ScrollView.
Example:
public void justifyListViewHeightBasedOnChildren (ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
Call this function passing over your ListView object:
justifyListViewHeightBasedOnChildren(myListview);
The function shown above is a modification of a post in: Disable scrolling in listview
Please note to call this function after you have set the adapter to the listview. If the size of entries in the adapter has changed, you need to call this function as well.
Don't put a listview in a scrollview, it doesn't work. If you want a list of items that doesn't scroll, it's called a linearlayout.
to disable scrolling you can use listview.setEnabled(false)
This also disables row selections.
The correct answer is here.
Just assign this listener to your ListView
:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you and will not be forwarded further.
}
return false;
}
});