1

我有一个带有更多控件的活动:一个 Spinner、2 个编辑框、一个按钮一些文本视图和一个列表视图,然后是另一个按钮。全部采用垂直布局。

OnClick 第一个按钮,我将一个新项目添加到列表视图(基于其他编辑框值和微调器值)。这样,列表视图不断增长。我想知道是否有任何方法可以使列表视图的高度在每次添加后自动扩展。扩展后底部按钮也应保留在底部。

我想要这个,因为我的列表视图非常小,因为布局上有很多控件。它几乎不会同时显示 2 个项目。所以用户不能一次看到所有添加的项目,他必须在这个2 项目列表视图中滚动才能看到所有项目。这看起来既糟糕又愚蠢。而且用户还必须知道列表中有超过 2 个项目,因为没有直接看到它们,他可以假设只有 2 个项目并且应用程序无法运行,因为没有新项目出现。我希望我说清楚了。

请告诉我是否有可能或在这种情况下最好的方法是什么?

我的布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".NewStuff" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Back" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Page title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Choose item" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_below="@+id/spinner1"
        android:text="Property 1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignRight="@+id/spinner1"
        android:layout_below="@+id/textView3"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:text="Property 2" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/textView4"
        android:ems="10"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignRight="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:text="Add to listview" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toLeftOf="@+id/button1"
        android:text="Save listview contents" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2" >

    </ListView>

</RelativeLayout>
4

3 回答 3

3

你需要一个自定义列表视图。

public class ExpandedListView extends ListView {

private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;

public ExpandedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
    if (getCount() != old_count) {
        old_count = getCount();
        params = getLayoutParams();
        params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() + 2 : 0);
        setLayoutParams(params);
    }
    super.onDraw(canvas);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev){
   if(ev.getAction()== MotionEvent.ACTION_MOVE)
      return true;
   return super.dispatchTouchEvent(ev);
}

}

此列表视图将随着其子项的数量而增长。在你的xml中你必须使用

<your.path.ExpandedListView
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1" />

希望这会有所帮助

于 2013-11-26T15:11:58.633 回答
2

使用 LinearLayout 来保存所有内容,并在 ListView 上使用 layout_weight 来完成此操作:

<ListView android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:layout_above="@+id/button1"
          android:layout_alignLeft="@+id/button2"
          android:layout_below="@+id/button2"/>
于 2013-08-06T01:47:33.357 回答
1

您的屏幕上有太多项目,请记住 Android 在大量屏幕配置上运行。因此,在某些设备上,空间可以容纳两个以上的物品,但在其他设备上,空间可能少于两个。所以我建议以下解决方案:

  1. 如果项目不多且不涉及显示大图形,则可以将 ListView 更改为 LinearLayout,并动态插入所有项目。然后将整个视图封装在一个 ScrollerView 中,这样用户就可以在任何地方滚动以查看完整列表。
  2. 在不同的活动/片段中显示结果,这具有强调用户正在搜索的数据的额外效果

[编辑]

其实很简单,基本思路是:

  1. 对于每个项目,膨胀所需的视图并设置其布局参数
  2. 将其添加到父视图
  3. 重复步骤 1 和 2

例如:

//the base LinearLayout that you want to insert items
LinearLayout content = (LinearLayout) fragView.findViewById(R.id.booking_content);

//for each item in the LinearLayout
for(int i=0; i<itemsList.size(); i++) {
    //inflate layout as you normally would for a ListView
    ViewGroup base = (ViewGroup) inflater.inflate(
            R.layout.include_no_movie, null);
    //sets the layout parameter of the child view
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    //adds the view
    content.addView(base, param);
}
于 2013-08-06T03:20:55.220 回答