【问题标题】:Making the ListView Scrollable使 ListView 可滚动
【发布时间】:2013-06-05 12:44:55
【问题描述】:

我有两个 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>

【问题讨论】:

标签: java android xml


【解决方案1】:

我认为如果您使用 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 的可视化顶部将位于父视图的顶部,可视化底部将位于父视图的底部,并且您的内容将与您的列表一样,滚动需要的地方。

【讨论】:

  • listView 现在正在滚动,感谢 HalR。感谢您直接而明确的回答。干杯!~
猜你喜欢
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 2012-11-20
  • 2018-05-23
  • 2011-05-17
  • 1970-01-01
  • 2019-10-18
  • 2019-05-19
相关资源
最近更新 更多