Android ListView不展开

Android ListView不展开,android,android-layout,android-listview,Android,Android Layout,Android Listview,我有一个包含更多控件的活动:一个微调器、两个编辑框、一个按钮、一些文本视图和一个列表视图,然后是另一个按钮。都是垂直布局 单击第一个按钮后,我将向listview添加一个新项(基于其他EditBox值和微调器值)。这样,listview不断增长。 我想知道是否有任何方法可以使listview的高度在每次添加后自动扩展。 此外,底部按钮在展开后应保持在底部 我之所以想要这个,是因为我的listview非常小,因为布局上有很多控件。它几乎一次显示两个项目。因此,用户不能一次看到所有添加的项目,他必须

我有一个包含更多控件的活动:一个微调器、两个编辑框、一个按钮、一些文本视图和一个列表视图,然后是另一个按钮。都是垂直布局

单击第一个按钮后,我将向listview添加一个新项(基于其他EditBox值和微调器值)。这样,listview不断增长。 我想知道是否有任何方法可以使listview的高度在每次添加后自动扩展。 此外,底部按钮在展开后应保持在底部

我之所以想要这个,是因为我的listview非常小,因为布局上有很多控件。它几乎一次显示两个项目。因此,用户不能一次看到所有添加的项目,他必须在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>

使用线性布局保存所有内容,并使用ListView上的布局权重来完成此操作:

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

使用线性布局保存所有内容,并使用ListView上的布局权重来完成此操作:

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

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

  • 如果项目不太多且不需要显示大型图形,则可以将ListView更改为LinearLayout,并动态插入所有项目。然后将整个视图封装在ScrollView中,以便用户可以滚动到任何地方查看完整列表
  • 在不同的活动/片段中显示结果,这具有突出用户正在搜索的数据的额外效果
  • [编辑]

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

  • 对于每个项目,为所需视图充气并设置其布局参数
  • 将其添加到父视图
  • 重复步骤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);
    }
    
    //要插入项的基本线性布局
    LinearLayout content=(LinearLayout)fragView.findViewById(R.id.booking\u content);
    //对于线性布局中的每个项目
    
    对于(int i=0;i您的屏幕上有太多的项目,请记住Android运行在大量的屏幕配置上。因此,在某些设备上可以容纳两个以上的项目,但在其他设备上可能少于两个。因此,我建议以下解决方案:

  • 如果项目不太多,并且不需要显示大的图形,您可以将ListView更改为LinearLayout,并动态插入所有项目。然后将整个视图封装在ScrollView中,以便用户可以在任何地方滚动查看完整的列表
  • 在不同的活动/片段中显示结果,这具有突出用户正在搜索的数据的额外效果
  • [编辑]

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

  • 对于每个项目,为所需视图充气并设置其布局参数
  • 将其添加到父视图
  • 重复步骤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);
    }
    
    //要插入项的基本线性布局
    LinearLayout content=(LinearLayout)fragView.findViewById(R.id.booking\u content);
    //对于线性布局中的每个项目
    
    对于(inti=0;i您需要一个自定义的listview

    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);
    }
    
    }

    此listview将随着其子项的数量而增长。 在xml中,必须使用

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

    希望这将有助于您需要自定义listview

    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);
    }
    
    }

    此listview将随着其子项的数量而增长。 在xml中,必须使用

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

    希望这会有所帮助

    您是否试图让listview占据未分配给其他按钮和微调器的剩余高度?您是否试图让listview占据未分配给其他按钮和微调器的剩余高度?我喜欢您在滚动条中使用线性布局的建议,但我不知道如何创建模拟listview的te项,最重要的是,如何解析这些动态“项”最后,当用户想通过按下底部按钮来保存其工作时?我喜欢你的建议,即滚动条中的LinearLayout,但我不知道如何创建模仿listview的项目,最重要的是,我如何解析这些动态“项目”最后,当用户想通过按下底部按钮来保存其工作时?非常感谢。这是一个更好的解决方案。@Azizi i更新了高度计算,以便正确显示包含许多项的列表。请使用params.height=getCount()*(old_count>0?getChildAt(0)。getHeight()+2:0);谢谢,这帮了我很大的忙。非常感谢。这是一个更好的解决方案。@Azizi我更新了高度的计算,以便正确显示包含许多项目的列表。使用params.height=getCount()*(old_count>0?getChildAt(0)。getHeight()+2:0);谢谢,这帮了我很大的忙。