【问题标题】:Android ListView does not expandAndroid ListView 不展开
【发布时间】:2013-08-06 01:35:29
【问题描述】:

我有一个带有更多控件的活动:一个微调器、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>

【问题讨论】:

  • 您是否想让您的列表视图占据未分配给其他按钮和微调器的剩余高度?

标签: android android-layout android-listview


【解决方案1】:

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

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" />

希望这会有所帮助

【讨论】:

  • 非常感谢。这是迄今为止更好的解决方案。
  • @Azizi 我更新了高度的计算,因此可以正确显示包含许多项目的列表。使用 params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() + 2 : 0);
  • 谢谢,这对我帮助很大。
【解决方案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"/>

【讨论】:

    【解决方案3】:

    您的屏幕上有太多项目,请记住 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);
    }
    

    【讨论】:

    • 我喜欢你在 Scroller 中使用 LinearLayout 的建议,但我不知道如何创建模仿列表视图的项目,最重要的是,我将如何在最后解析这些动态“项目”当用户想要通过按下底部按钮来保存他的工作时?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多