ListView的内容适配器更新后得到的截止 [英] ListView contents get cutoff after Adapter update

查看:155
本文介绍了ListView的内容适配器更新后得到的截止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UI中的一些垂直堆叠板。每个板都包含一个ListView。基于用户交互,项目在ListView数量得到更新。

I have a number of vertically stacked panels in my UI. Each panel contains a ListView. Based on user interaction, the number of items in the ListView gets updated.

我的问题是,如果我增加被显示在ListView的项目数,含面板不会扩大向他们展示。相反,我的ListView只需获取与黑渐变切断。我以编程方式创建这些叠板 - 这是创建函数体:

My problem is that if I increase the number of items being shown in the ListView, the containing panel will not expand to show them. Instead, my ListView just gets cut off with a fade to black. I am programmatically creating each of these stacked panels - this is the body of the creating function:

LinearLayout containingPanel = new LinearLayout(TestActivity.this);
containingPanel.setOrientation(LinearLayout.VERTICAL);

// create title
TextView titleText = new TextView(TestActivity.this);
titleText.setText("a title");
titleText.setGravity(Gravity.CENTER);
titleText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


// create dynamic list view of costs                
ListView dynamicContentListView = new ListView(TestActivity.this);
dynamicContentListView.setAdapter(new MyDynamicAdapter());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.weight = 1;
dynamicContentListView.setLayoutParams(params);

// build up GUI

containingPanel.addView(titleText); 

containingPanel.addView(dynamicContentListView);                        
return containingPanel;

我想在ListView中设置的权重应该是足够了,但事实并非如此。所有的GUI更新工作正常 - 新的项目被自动添加到数据备份的适配器,ListView控件更新本身正确。但经过我添加三个或四个新项目,该containingPanel拒绝自我更新和新的项目将混合了。

I thought that setting the weight on the ListView should be enough, but it is not. All GUI updating is working fine - new items are automatically added to data backing the Adapter, and the ListView updates itself properly. But after I add three or four new items, the containingPanel refuses to update itself and the new items get blended out.

推荐答案

(您可以添加的ListView 滚动型但并非没有一点工作。(因为他们都是滚动元件 - 如何将操作系统知道哪一个你想滚动)你需要添加 isScrollContainer =假在你的ListView。)

(You can add a ListView inside a ScrollView but not without a little work (as they are both Scrollable components - how would the OS know which one you're trying to scroll?). You would need to add isScrollContainer="false" on your ListView.)

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_contents"
    android:isScrollContainer="false"/>

的ListView 的整点是它具有除非你的页面布局设定高度。当它的孩子们的总高度超过显示它所需的面积,只变成滚动。

The entire point of a ListView is it has a set height as dictated by the layout of your page. It only becomes scrollable when it's children's combined height exceed the area required to display it.

这听起来像你真正想要的东西更像是通过适配器支持的LinearLayout中,有几个实现了有在网络上,也可以创建自己的。

It sounds like what you actually want is something more akin to a LinearLayout which is backed by an Adapter, there are several implementations out there on the web or you can create your own.

不过,你可以破解一个ListView到这种行为通过动态地调整你的的ListView 编程设置它的高度为: listCount * itemHeight 。这将有持续扩大你的ListView效果。

However, you can hack a ListView into this behaviour by dynamically resizing your ListView programatically by setting it's Height to: listCount * itemHeight. This would have the effect of consistently expanding your ListView.

您可能会发现,当你发展你的UI设计,你将不再需要这样的一个组成部分。

You will likely find that as you develop your UI design you will no longer require such a component.

这篇关于ListView的内容适配器更新后得到的截止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆