📜 ⬆️ ⬇️

ListView inside ScrollView

In the work on the project, it became necessary to draw a layout of this type: on top of the compact block are the details of the topic, and under them is a list of comments to this topic. At first it was implemented in a natural way: the layout of the details of the topic, and under it - the list. Later TK changed, and it turned out that you need to scroll the title along with the list.

The first impulse was to do what was in the headline: put the ListView inside the ScrollView . The resulting thing was displayed incorrectly, which made me dig deeper into Google.

In many places on the network (for example, here: https://groups.google.com/forum/#!topic/android-beginners/LRpLgGOy2Pc or here: http://stackoverflow.com/questions/1526831/android-scrollview- Layout-problem ) directly states that you can not do this. To implement such things, ListView provides for regular headers.

Headers are added by calling the ListView class method:
')
public void addHeaderView (View v)

where View v can be created in any way (either by explicitly calling the constructor, or through inflater).

View hv = ...;
listView.addHeaderView(hv);


There can be more than one header in the ListView . By the way, one more feature is connected with their number: by default, the headers are also clickable, as well as the usual list items, clicking on them causes practicing the liners specified by calls to setOnItemClickListener and setOnItemLongClickListener , so the position of the clicked item that is passed to the liners in the position parameter, will be shifted by the number of headers. This fact must be taken into account in the lisner when processing. For this, ListView has a method:

int getHeaderViewsCount()

which returns the number of list headers. By the value that it returns, you can, for example, reduce the position value obtained in the linsen before positioning in the internal data list.

protected OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(final AdapterView<?> l, final View v, final int position, final long id) {
MyItem myItem = myItems.get(position - listView.getHeaderViewsCount());
// do something to myItem
}
};


There is another method for adding headers, a bit more complicated:

public void addHeaderView (View v, Object data, boolean isSelectable)

It provides the ability to make the header non-clickable: if you pass false to the third parameter, then when you click on the header, the list will not work onItemClickListener . Lisner same, set for the views located inside such a title, will work out regularly. However, even in this case, despite the non-climability of the title, the numbering of elements for position will still take into account their presence.

View hv = ...;
listView.addHeaderView(hv, null, false);


For completeness, I’ll say that the second parameter ( Object data ), the meaning of which is not very clear from the documentation, actually sets the data that will be returned by the Adapter.getItem () method (taken here: http://stackoverflow.com/questions/ 4311693 / what-does-the-second-parameter-in-addheaderview-in-the-class-listview-do ).

The list of methods has a similar set for footers:

void addFooterView(View v)

void addFooterView(View v, Object data, boolean isSelectable)

int getFooterViewsCount()

Source: https://habr.com/ru/post/125107/


All Articles