2017-07-21 6 views
0

私はアンドロイドレイアウトを持っていて、そこには多数の要素を持つscrollViewがあります。 scrollViewの一番下には、アダプタによって設定されたlistViewがあります。ListView inside scrollView

私が経験している問題は、scrollViewが既にスクロール可能な機能を持っているため、アンドロイドがscrollViewからlistViewを除外していることです。 listViewをコンテンツの長さにし、マスタースクロールビューをスクロール可能にします。

私はリストビューは、代わりにListViewScrollViewの内側を埋め込む彼女scroling

@Gauravロイ

+0

https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-viewこれはあなたのような同じ問題を抱えています。 –

+0

これらのハッキングではなく、複数のビューでListViewとRecyclerViewを使用できます。 – Rahul

+0

https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view –

答えて

0

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

+0

なぜhttps://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-viewから貼り付けをコピーしますか? –

+0

コメントが表示されない場合があります –

+0

ソリーの男は、私を参照して忘れて @ Gauravロイ –

0

を完了し、単一のViewGroupにscrollviewの内容を追加している場合scroling scrollViewを設定したいです、プログラムでそれらをとListViewの先頭に追加します。

たとえば、レイアウトのルート要素をListViewにし、残りのビューと別のレイアウトをLayoutInflater経由で展開します。次に、その参照を膨張したビューにaddHeaderViewメソッドに渡します。

関連する問題