layout.xmlファイルで特別な処理を行う必要はなく、親のScrollViewで何も処理する必要はありません。子ListViewを処理するだけです。また、このコードを使用して、ScrollView &のTouch操作内の任意のタイプの子ビューを使用することもできます。
ちょうどあなたのJavaクラスのコードの次の行を追加します。
ListView lv = (ListView) findViewById(R.id.layout_lv);
lv.setOnTouchListener(new OnTouchListener() {
// Setting on Touch Listener for handling the touch inside ScrollView
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
あなたはScrollView内のListViewを置く場合は、すべてのListViewがその全高さまで伸びていません。以下はこの問題を解決する方法です。
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
ちょうどこのメソッド内のListViewを渡し、このメソッドを使用するには:
リストビューコントロールに対して
ListView list = (ListView) view.findViewById(R.id.ls);
setListViewHeightBasedOnChildren(list);
For using with ExpandableListView - credit Benny
ExpandableListView: view = listAdapter.getView(0, view, listView);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);
変数の項目の高さは、以下のリンクを使用します。ScrollView内部
ListViewコントロールは、Android
にスクロールされていません
あなたのコードにライブラリを直接実装する - クレジットPaolo Rotolo
https://github.com/PaoloRotolo/ExpandableHeightListView
https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-viewこれはあなたのような同じ問題を抱えています。 –
これらのハッキングではなく、複数のビューでListViewとRecyclerViewを使用できます。 – Rahul
https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view –