1

ListViewには独自のスクロールがあるため、「ListViewをScrollViewに入れない」というのは非常に悪い習慣です。ただし、ListView に追加する項目 (ボタンとテキストビュー) がいくつかあります。それらについては、間違いなく ScrollView を使用する必要があります。リストビューの崩壊を防ぐための解決策(http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html )を見つけました。しかし、私が言ったように、XML フォームのすべての項目をカプセル化する ScrollView が必要です。xml コードを追加しました。これを達成する方法を教えてください。

   <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >  
<LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice">
    </ListView>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
     <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1" />
      </LinearLayout>

       <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/longtext" />

</LinearLayout>
</ScrollView>
4

3 に答える 3

0

スクロールビュー内でリストビューを使用するために Expandable Listview を使用できます。コンテンツの長さに応じてリストビューが拡張され、ExpandableHeightListView という名前のカスタム クラスが作成されます。

パッケージ com.knight.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;

public class ExpandableHeightListView extends ListView
{

boolean expanded = false;

public ExpandableHeightListView(Context context)
{
    super(context);
}

public ExpandableHeightListView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ExpandableHeightListView(Context context, AttributeSet attrs,int defStyle)
{
    super(context, attrs, defStyle);
}

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // But do not use the highest 2 bits of this integer; those are
        // reserved for the MeasureSpec mode.
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
}
} 

その後、プログラムでこの行をコードに追加すると、リストビューがスクロールビューの横に拡張されます。

Listview listview=(Listview)findviewbyid(R.id.listview1);
((ExpandableHeightListView)listview).setExpandable(true);
于 2014-05-08T13:18:16.417 に答える