问题是布置两个ListView

时间:2011-02-11 04:56:10

标签: android listview

我一直在努力在垂直的LinearLayout中布置两个ListView。我终于在这里看到了关于将每个包装在他们自己的LinearLayout中的答案,并将这些LinearLayouts中的每一个添加到权重为1的原始LinearLayout。这就是我尝试过的......但我似乎无法让它工作。

我在代码中这样做:

public class MyDualList extends LinearLayout
{
    private LinearLayout    _layout1;
    private LinearLayout    _layout2;
    private ListView        _list1;
    private ListView        _list2;

    public MyDualList(Context context, ListView list1, ListView list2)
    {
        super(context);

        _list1 = list1;
        _list2 = list2;

        _layout1 = new LinearLayout(context);
        _layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 0, 1));
        _layout1.addView(_list1, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        _layout2 = new LinearLayout(context);
        _layout2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 0, 1));
        _layout2.addView(_list2, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        addView(_layout1);
        addView(_layout2);
    }
}

这是我最近的尝试,但我发誓我已经尝试了LayoutParams高度的每个组合/设置(FILL_PARENT,WRAP_CONTENT,0)以及各种权重。

结果永远不会很好;如果我的第二个列表很长,它不可避免地占据了原始垂直布局的绝大部分(当然远远超过50%)。

我不会放弃!也许有人可以帮我一点。

谢谢, 肯

3 个答案:

答案 0 :(得分:2)

我想请你将视图逻辑移到XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <ListView
            android:id="@+id/list1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
    <ListView
            android:id="@+id/list2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
</LinearLayout>

Java代码可以是:

final String[] data1 = new String[] { "1a", "1b", "1c", "1d", "1e", "1f", "1g", "1h", "1i" };
final String[] data2 = new String[] { "2a", "2b", "2c", "2d", "2e", "2f", "2g", "2h", "2i" };

final ListView l1 = (ListView) findViewById(R.id.list1);
final ListView l2 = (ListView) findViewById(R.id.list2);

l1.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data1));
l2.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data2));

此外,如果您坚持使用Java编码,我会尝试在您LayoutParams - &gt;的调用中设置addView(_layout1)addView(_layout1, new LayoutParams(LayoutParams.FILL_PARENT, 0, 1))

BTW :您在哪里设置此布局的方向?默认为水平

答案 1 :(得分:1)

您正在使用不正确的布局参数(在addView中传递的那个)替换正确的布局参数(使用setLayoutParams设置的参数)。只需调用addView(_list1),而无需指定布局参数。 编辑抱歉,我误读了代码,忽略了这一点:)

答案 2 :(得分:0)

所以这是解决方案2:

public class Q4965745 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setContentView(R.layout.main);
        setContentView(new Q4965745View(this));

        final String[] data1 = new String[]{"1a", "1b", "1c", "1d", "1e", "1f", "1g", "1h", "1i"};
        final String[] data2 = new String[]{"2a", "2b", "2c", "2d", "2e", "2f", "2g", "2h", "2i"};

        final ListView l1 = (ListView) findViewById(11);
        final ListView l2 = (ListView) findViewById(22);

        l1.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data1));
        l2.setAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.text, data2));
    }

    class Q4965745View extends LinearLayout {
        public Q4965745View(Context context) {
            super(context);
            Q4965745View.this.setOrientation(LinearLayout.VERTICAL);

            ListView lv1 = new ListView(context);
            lv1.setId(11);

            ListView lv2 = new ListView(context);
            lv2.setId(22);

            Q4965745View.this.addView(lv1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
            Q4965745View.this.addView(lv2, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
        }
    }
}

产生:

Screenshot

修改

使用1000个条目填写 data2 时:

final String[] data2 = new String[1000];
for(int i = 0; i < 1000; i++) {
    data2[i] = "test" + i;
}

我仍然得到相同的布局:

Screenshot 50/50 * 1000 entries

编辑2:

至少使用TableLayout

,我能够获得“更好的行为”
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TableRow>
        <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />
    </TableRow>
    <TableRow>
        <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />
    </TableRow>
</TableLayout>

也许罗曼有一个关于如何使其真正50/50独立于ListView中的项目数量的想法..

编辑3

好的,最终解决方案(使用内部LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            >
        <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
    </LinearLayout>
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            >
        <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
    </LinearLayout>
</LinearLayout>

也许够好吗? ; - )