【问题标题】:Android, How to add ScrollView into screen which has some list items?Android,如何将 ScrollView 添加到具有一些列表项的屏幕中?
【发布时间】:2012-05-08 10:25:22
【问题描述】:

在我的活动中,我有三个列表。运行应用程序后,屏幕无法滚动。我可以滚动位于其他列表之上的列表,但我无法滚动整个页面。我试图将ScrollView 添加到我的 xml 布局中,但 lint 说“垂直滚动的 ScrollView 不应包含另一个垂直滚动的小部件 (ListView)”。

如何让我的屏幕可滚动?

【问题讨论】:

    标签: android scrollview


    【解决方案1】:

    试试这个方法

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
        <LinearLayout android:id="@+id/GlobalLayout" 
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:orientation="vertical" >
    
            <ListView android:id="@+id/ListView1"
                      android:choiceMode="multipleChoice"
                      android:layout_height="100dip"
                      android:layout_width="fill_parent" />
           <ListView android:id="@+id/ListView2"
                      android:choiceMode="multipleChoice"
                      android:layout_height="100dip"
                      android:layout_width="fill_parent" />
           <ListView android:id="@+id/ListView3"
                      android:choiceMode="multipleChoice"
                      android:layout_height="100dip"
                      android:layout_width="fill_parent" />
    
        </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

    • 谢谢,我这样做了,但结果是一样的,我无法滚动屏幕。
    【解决方案2】:

    <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
    
                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
                </ListView>
    
            </LinearLayout>
    
        </ScrollView>
    

    Scrollview 支持一种子布局,所以将 listview 放在滚动里面,这样你就可以得到你想要的。

    【讨论】:

    • 谢谢,我这样做了,但结果是一样的,我无法滚动屏幕。
    【解决方案3】:

    它不起作用,因为 android 不知道他应该滚动哪个视图,所以将 listview 放在 scrollview 中并不是一个好主意。 尝试使用visibilty 属性,使用HIDE 您完成使用的视图并设置VISIBLE 新的。 或使用ListView 并在完成和开始指令后尝试填充和删除项目。 希望对你有所帮助

    【讨论】:

      【解决方案4】:

      您可以做的是,您可以通过膨胀将页眉和页脚添加到列表视图中。创建三个 xml 布局并使用页眉和页脚将它们添加到列表视图中。

          View headerView = View.inflate(this, R.layout.layout_name, null);
          lv.addHeaderView(headerView);
      

      在设置适配器之前使用它。 lv 是列表视图。

      【讨论】:

        【解决方案5】:

        使用此方法,您的列表视图将在滚动视图中滚动:-

           ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
                                    ViewNewsDetail.this, viewOfferList));
                            getListViewSize(lstNewsOffer);
        
        void getListViewSize(ListView myListView) {
            ListAdapter myListAdapter = myListView.getAdapter();
            if (myListAdapter == null) {
                // do nothing return null
                return;
            }
            // set listAdapter in loop for getting final size
            int totalHeight = 0;
            for (int size = 0; size < myListAdapter.getCount(); size++) {
                View listItem = myListAdapter.getView(size, null, myListView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            // setting listview item in adapter
            ViewGroup.LayoutParams params = myListView.getLayoutParams();
            params.height = totalHeight
                    + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
            myListView.setLayoutParams(params);
            // print height of adapter on log
            Log.i("height of listItem:", String.valueOf(totalHeight));
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-16
          • 1970-01-01
          • 2020-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-14
          • 1970-01-01
          相关资源
          最近更新 更多