Android的 - 不能得到滚动视图滚动列表正确
问题描述:
我有这个漂亮的基本观点:Android的 - 不能得到滚动视图滚动列表正确
<?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"
>
<TextView
android:id="@+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your question: "
/>
<TextView
android:id="@+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<EditText
android:id="@+id/question_text"
android:layout_height="wrap_content"
android:hint="@string/question_comment_hint"
android:inputType="textMultiLine"
android:lines="4"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="@+id/submit"
android:layout_height="wrap_content"
android:text="@string/submit"
android:onClick="sendFeedback"
android:layout_width="fill_parent">
</Button>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/please_wait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait while the discussion loads..."
/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
我希望用它做是为了让用户通过整个滚动列表注释。到目前为止,当屏幕上出现更多评论时,屏幕不会向下滚动。
任何想法我应该改变,以启用滚动的评论?
谢谢!
答
我会完全取消滚动视图 - 基于我认为你试图完成的事情,因为listview将自己处理滚动。喜欢这样:
<?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"
>
<TextView
android:id="@+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your question: "
/>
<TextView
android:id="@+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<EditText
android:id="@+id/question_text"
android:layout_height="wrap_content"
android:hint="@string/question_comment_hint"
android:inputType="textMultiLine"
android:lines="4"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="@+id/submit"
android:layout_height="wrap_content"
android:text="@string/submit"
android:onClick="sendFeedback"
android:layout_width="fill_parent">
</Button>
<TextView
android:id="@+id/please_wait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait while the discussion loads..."
/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</ListView>
现在的差别是,与ID TextView的 “PLEASE_WAIT” 将不会在列表中滚动。如果你需要它滚动列表,我会建议使用Java中的动态构建的TableLayout作为列表,然后将它和你的textview添加到ScrollView中。 TableLayout不会自行滚动,这就是为什么它比在ScrollView中使用ListView更好的解决方案。如果你想这样做,让我知道,我会发布一些示例代码。
答
好像你想在ListView中的注释上面添加一个标题。正如编码员指出的那样,您不应该在ScrollView中放置ListView。你应该看看直接添加一个标题视图到你的ListView(见addHeaderView
)。
答
基本上你可以这样做: 摆脱scrollview和加载textview。 设置一个简单的适配器将列表视图这样的:
fList = (ListView)findViewById(R.id.lstview);
fList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"Please wait while the discussion loads..."}));
比得到您的意见,并设置为你的列表视图。
list.setAdapter(adapter);
答
你不应该在ScrollView中有一个ListView。 哄抬的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" >
<TextView
android:id="@+id/question_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your question: " />
<TextView
android:id="@+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<EditText
android:id="@+id/question_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/question_comment_hint"
android:inputType="textMultiLine"
android:lines="4" >
</EditText>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendFeedback"
android:text="@string/submit" >
</Button>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/please_wait"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please wait while the discussion loads..." />
<LinearLayout
android:id="@+id/comments_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="invisible" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
在您的代码:
LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);
要填充的LinearLayout:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i < comments.size(); i++){
View childView = inflater.inflate(R.layout.comment_list_item, commentsLayout, false);
TextView comment = (TextView) childView.findViewById(R.id.comment);
comment.setText("Something");
categoriesLayout.addView(childView, i);
}
类似的东西,希望这有助于。
不确定这是问题所在,但您绝对不应该在Android的scrollview中拥有listview。 – coder 2012-04-12 20:51:09
@coder怎么回事?你是对的。无论我尝试什么,这都会给我带来麻烦。你将如何组织这种页面? – GeekedOut 2012-04-12 20:54:12
什么是listview的?所有的评论? – coder 2012-04-12 20:56:19