クリッピングの問題の解決策が見つからなかったため、ListView にヘッダーとフッターを追加しました。ListView の高さを match_parent に戻し、ListView の親ビューで post メソッドを使用して、ヘッダーとフッターの高さを次のように計算できました。
// this call should be included in the onCreate method
// the post method is called after parentView is ready to be added
// so the parentView's height and width can be used
parentView.post(addSpace(parentView, myListView));
private Runnable addSpace(final RelativeLayout parent, final ListView list) {
return new Runnable(){
public void run() {
// create list adapter here or in the onCreate method
// R.layout.list_item refers to my custom xml layout file
adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, values);
// calculate size and add header and footer BEFORE applying adapter
// listItemHeight was calculated previously
spacerSize = (parent.getHeight()/2) - (listItemHeight/2);
list.addHeaderView(getSpacer(), null, false);
list.addFooterView(getSpacer(), null, false);
list.setAdapter(adapter);
}
}
}
// I used a function to create my spacers
private LinearLayout getSpacer(){
// I used padding instead of LayoutParams thinking it would be easier to
// change in the future. It wasn't, but still made resizing my spacer easy
LinearLayout spacer = new LinearLayout(MainActivity.this);
spacer.setOrientation(LinearLayout.HORIZONTAL);
spacer.setPadding(parentView.getWidth(), spacerSize, 0, 0);
return spacer;
}
このメソッドを使用して、ListView の上部と下部に適切な間隔を追加することができたので、一番上までスクロールすると、一番上のアイテムがビューの中央に表示されました。
問題: 私のアプリでは、ユーザーが一番下のインターフェイス要素を非表示にすることができます。ヘッダーとフッターを動的に高くする方法を考えていますが、単純に ListView を削除して、より大きなヘッダーとフッターで再作成する必要があるかもしれません。
これが何時間もの欲求不満から誰かを助けることを願っています:)