【问题标题】:Issues with ScrollView and setting layoutsScrollView 和设置布局的问题
【发布时间】:2017-11-30 08:48:22
【问题描述】:

在这里,我是 android 开发的新手。如果有不清楚的地方,请不要介意。

我对此问题进行了足够的研究,在将其标记为重复之前,请更新解决方案。

在这里,我添加了一个图像,其中包含我想要在此处实现的布局安排。

这里listView 仅显示为一项。如果我滚动,所有项目都将滚动到这个高度。

这是我的xml 结构

<LinearLayout>(parent)
     <ScrollView>
          <LinearLayout>
               <LinearLayout>
                weight: __
                -----------
               </LinearLayout>
               <ListView>
                   h:match_parent
                   w:wrap_content
                   weight:__
               </ListView>
               <LinearLayout>
                   weight:__
                --------------
               </LinearLayout>
          </LinearLayout>
     </ScrollView>
</LinearLayout>

我从 DB 中获取了许多数据集,并且必须在 LinearLayouts 的中间显示这些数据集。目前我正在使用ListView,但似乎不可能像我期望的那样在这里使用。

请建议我是否可以在这里执行ListView 或如何为每个数据集使用LinearLayout 块的数量?

更新 这是我所期望的,如图所示。

【问题讨论】:

标签: android listview android-linearlayout android-scrollview


【解决方案1】:

试试这个

<LinearLayout>(parent)
     <NestedScrollView>
          <LinearLayout>
               <LinearLayout>                    
                w:wrap_content
                h:wrap_content
               </LinearLayout>
               <ListView>
                   w:match_p__arent
                   h:match_p__arent
                   weight:1
                   NestedScroll:true
               </ListView>
               <LinearLayout>
                   w:match_p__arent
                   h:match_p__arent
                   weight:1 
               </LinearLayout>
          </LinearLayout>
     </NestedScrollView>
</LinearLayout>

【讨论】:

  • 在这种情况下,滚动 listView 可以正常工作,但不会滚动 scrollView
  • @SNSingh 使用 NestedScrollView 而不是 ScrollView
  • 谢谢,现在两者都工作正常,但现在 listViewheight 与 1 项一样。如果我想增加身高怎么办?
【解决方案2】:

你的 ListView 需要这个方法

    public static void setListViewHeightBasedOnItems(ListView target_Listview, int limit) // LIMIT 0 FOR SHOWING ALLL CONTENTS
{
    if (limit == 0) {
        ListAdapter listAdapter = target_Listview.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                //                if(itemPos < 4)
                //                {
                View item = listAdapter.getView(itemPos, null, target_Listview);
                item.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                Log.e("" + itemPos, "" + item.getMeasuredHeight());
                totalItemsHeight += item.getMeasuredHeight();
                //                }
            }

            // Get total height of all item dividers.
            int totalDividersHeight = target_Listview.getDividerHeight() * (numberOfItems - 1);

            // Set list height.
            ViewGroup.LayoutParams params = target_Listview.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight;
            target_Listview.setLayoutParams(params);
            target_Listview.requestLayout();

            //  return true;

        }
        else {

        }
    }
    else {
        ListAdapter listAdapter = target_Listview.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                if (itemPos < limit) {
                    View item = listAdapter.getView(itemPos, null, target_Listview);
                    item.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                    Log.e("" + itemPos, "" + item.getMeasuredHeight());
                    totalItemsHeight += item.getMeasuredHeight();
                }
            }

            // Get total height of all item dividers.
            int totalDividersHeight = target_Listview.getDividerHeight() * (numberOfItems - 1);

            // Set list height.
            ViewGroup.LayoutParams params = target_Listview.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight;
            target_Listview.setLayoutParams(params);
            target_Listview.requestLayout();
        }


    }
}

ListView 上设置适配器后,只需调用该方法并在参数中传递您的ListView,并将您的列表大小作为第二个参数。

One Suggestion from my side :您应该使用 RecyclerView 而不是 listview 。只需在设置完所有内容(适配器等)后为此写一行

recyclerView.HasFixedSize();

希望我的回答会有所帮助。

【讨论】:

  • 感谢@parambir,现在尺寸调整好了。
  • 欢迎@SNSingh 如果对您有帮助,请将答案标记为已接受。
【解决方案3】:

可能需要进一步说明您想要完成的具体内容。

如果ListView 应该更长并且要滚动列表中的更多项目,我可能会建议使用RecyclerView。它的设置要多一点,但允许根据概念创建更复杂的布局。

如果你想进一步搜索,有很多很好的参考资料可以学习RecyclerView的方法:

【讨论】:

  • 感谢您的回复,但我必须在listView中实施
  • 它必须是ListView 有什么原因吗?仅仅因为您的布局文件中的设置现在有一个滚动陷阱,因为ScrollViewListView 都是可滚动的。 RecyclerView 可以通过将所有内容组合到一个大的可滚动列表中来帮助您解决这个问题。
【解决方案4】:

试试这个方法

<LinearLayout>   
      <LinearLayout>
           <LinearLayout>
            weight: __
            -----------
           </LinearLayout>
           <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ListView>
               h:match_parent
               w:wrap_content
               weight:__
            </ListView>
           </ScrollView>
           <LinearLayout>
               weight:__
            --------------
           </LinearLayout>
      </LinearLayout>

【讨论】:

  • listView 本身有滚动属性,为什么要为 listView 添加scrollView
【解决方案5】:


我已经在我的 Android 工作室中尝试过,我的工作正常,我可以滚动视图。我的与你相比有点不同。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">

        <LinearLayout
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="50dp"

            android:background="@color/colorAccent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:text="@string/hello" />

        </LinearLayout>

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@color/black_overlay"
            android:layout_below="@+id/first" />

        <LinearLayout
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="550dp"

            android:background="@color/colorAccent"
            android:layout_below="@+id/listview"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:text="@string/hello" />

        </LinearLayout>


    </RelativeLayout>


</ScrollView>

【讨论】:

  • Listview本身有滚动行为,不需要添加scrollview。
  • listView 本身具有滚动属性,我想滚动整个linearlayout 并且应该滚动listView 的中间。
  • 我已经更新了一个示例图像,这就是我想要实现的
  • 抱歉未能理解
猜你喜欢
  • 2017-10-15
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2012-09-12
  • 2020-12-19
  • 1970-01-01
相关资源
最近更新 更多