1

2 つのリストビューが上下にあるシナリオがあります。一番上のリストをそのリストの一番下までスクロールしてから、2 番目のリストに進むようにしようとしています。

ご覧のとおり、どちらもコンテンツをラップするように設定されています。スクロールはしますが、すべてではなく、各リストビューから 1 つのオプションのみを表示します。

<ScrollView
    android:id="@+id/scrllArea"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/bottombar" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/topbar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="{0} Issues"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/lvIssues"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="false" >
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/topbar2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvTitle2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="{0} Issues"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/lvNewIssues"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:drawSelectorOnTop="false" >
            </ListView>
        </LinearLayout>
    </LinearLayout>
</ScrollView>
4

1 に答える 1

4

あるスクロール コンポーネントを別のコンポーネントに配置しないことをお勧めします。ListViewただし、このレイアウトでが必要な場合は回避策があります。スクロールしません。すべてのアイテムが表示されるように、高さいっぱいまで展開されます。

UI という名前の新しいパッケージ内に新しいクラスを作成し (または任意の他の名前)、以下を貼り付けます。

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ListView;

public class ExpandedListView extends ListView 
{
    private android.view.ViewGroup.LayoutParams params;
    private int oldCount = 0;

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

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (getCount() != oldCount) 
        {
            int height = getChildAt(0).getHeight() + 1 ;
            oldCount = getCount();
            params = getLayoutParams();
            params.height = getCount() * height;
            setLayoutParams(params);
        }

        super.onDraw(canvas);
    }
}

ListViews次のように定義します。

        <com.example.UI.ExpandedListView <!--or whatever your package path name is -->
            android:id="@+id/lvIssues"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:drawSelectorOnTop="false" >
        </ListView>


        <com.example.UI.ExpandedListView
            android:id="@+id/lvNewIssues"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:drawSelectorOnTop="false" >
        </ListView> 

height 属性が fill_parent に設定されていることに注意してください。onDraw()のメソッドで実施した制限により、親コンポーネントを視覚的に塗りつぶしませんExpandedListView

ただし、もう一度繰り返します。他のレイアウトにリファクタリングできる場合は、そうしてください。提供したレイアウトはお勧めできません。

幸運を。

于 2013-09-12T12:57:21.687 に答える