java - 使 ListView 可滚动

标签 java android xml

我有两个 xml 文件。其中之一包含 ListView 和填充 listView 的一个。我曾是 能够显示项目,但问题是它无法滚动。例如,我有要显示的项目列表,但它不会自动启用滚动。应该增强代码的哪一部分,以便允许滚动。我知道 listView 默认情况下启用滚动。请帮我解决这个问题。提前致谢。

    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

<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

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

最佳答案

我认为如果您使用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>

ListView 的视觉顶部将位于父 View 的顶部,视觉底部将位于父 View 的底部,并且只要列表出现,您的内容就会出现并滚动到需要的位置。

关于java - 使 ListView 可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16930753/

相关文章:

java - Google Cloud Endpoints - 在资源 getter 中返回另一个资源

java - 如何创建 JTree ExpansionListener

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

java - 奇怪的 Math.sin 行为

android - 可扩展 ListView 项目高度,其中包含另一个 ListView

android - QSettings 在 Android 下的 Qt 上无法正常工作

java - 如何将 List<Object> 转换为 List<T>?

java - 如何在java中处理嵌套的xml文件?

php - 从 MySQL 表创建 xml rss 提要时出错

c - 如何使用 libxml2 深入解析 xml 文件