[英]Making the ListView Scrollable
I have two xml files. 我有两个xml文件。 One of them contains the ListView and the one who populates the listView.
其中一个包含ListView,另一个填充ListView。 I was able to display the items but the thing is, it can't be scrolled.
我能够显示项目,但事实是,它不能滚动。 For example, I have list of items to display but it will not automatically enables scroll.
例如,我有要显示的项目列表,但不会自动启用滚动。 Which part of the code should be enhanced so that it will allow scrolling.
应该增强代码的哪一部分,以便允许滚动。 I know that listView enables scrolling by default.
我知道listView默认启用滚动。 Please help me with this one.
请帮我这个。 Thanks in advance.
提前致谢。
public class ActivityViewCategory extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Get listview
ListView lv = getListView();
.
.
.
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
ArrayList<HashMap<String, String>> com.example.restotrial.ActivityViewCategory.productsList * */
ListAdapter adapter = new SimpleAdapter(
ActivityViewCategory.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
activity_view_products.xml activity_view_products.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="456dp" >
</ListView>
</LinearLayout>
list_item.xml list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
</LinearLayout>
I think it would work better if you used the constraints of a RelativeLayout to contain your ListView
. 我认为,如果您使用RelativeLayout的约束来包含
ListView
效果会更好。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
The visual top of your ListView
will be at the top of the parent view, the visual bottom will be at the bottom of the parent view, and your content will be as long as your list happens to be, and scroll where it needs to. ListView
的视觉顶部将在父视图的顶部,视觉底部将在父视图的底部,您的内容将与列表恰好一样长,然后滚动到需要的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.