Android ScrollView和设置布局的问题

Android ScrollView和设置布局的问题,android,listview,android-linearlayout,android-scrollview,Android,Listview,Android Linearlayout,Android Scrollview,这里我是android开发的新手。如果有不清楚的地方,不要介意 我对这个问题做了足够的研究,在将其标记为重复之前,请更新解决方案 在这里,我添加了一个带有布局安排的图像,我想在这里实现它 此处,列表视图仅显示为1项。如果我滚动,所有项目都将以此高度显示的方式滚动 这里是我的xml结构 <LinearLayout>(parent) <ScrollView> <LinearLayout> <Lin

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

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

在这里,我添加了一个带有布局安排的图像,我想在这里实现它

此处,
列表视图
仅显示为1项。如果我滚动,所有项目都将以此高度显示的方式滚动

这里是我的
xml
结构

<LinearLayout>(parent)
     <ScrollView>
          <LinearLayout>
               <LinearLayout>
                weight: __
                -----------
               </LinearLayout>
               <ListView>
                   h:match_parent
                   w:wrap_content
                   weight:__
               </ListView>
               <LinearLayout>
                   weight:__
                --------------
               </LinearLayout>
          </LinearLayout>
     </ScrollView>
</LinearLayout>
(父级)
重量:__
-----------
h:匹配你的父母
w:包装内容
重量:__
重量:__
--------------
我从数据库中获取了大量数据集,必须在两个
线性布局的中间显示这些数据集。现在我使用的是
ListView
,但似乎不可能像我预期的那样在这里使用

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

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


可能需要更多的澄清,以明确您想要实现的目标

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

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

  • 这可能会帮助您设置问题的解决方案
  • Android开发者网站教程,用于
    RecyclerView
  • 来自代码路径的帖子,帮助我一起补充了所有内容。
试试这种方法

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

重量:__
-----------
h:匹配你的父母
w:包装内容
重量:__
重量:__
--------------
试试这个

<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>
(父级)
w:包装内容
h:包装内容
w:不匹配
h:不匹配
体重:1
嵌套滚动:true
w:不匹配
h:不匹配
体重:1

我已经在我的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需要此方法

    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();
        }


    }
}

希望我的回答能有所帮助

我不明白所有的,你能解释更多吗?我已经更新了问题,我想在2
linearLayout
中间显示数据集,这是我从DB获得的。检查我下面的ans@snsingh,我希望它能有所帮助,请看这是否有帮助:[link],这应该有帮助[link]Listview本身具有滚动行为无需添加滚动视图。
Listview
本身具有滚动属性,我想滚动整个
linearlayout
,并且两个
Listview
的中间部分都应该滚动。我用一个示例图像进行了更新,这就是我想要实现的。很抱歉没有理解。在这种情况下,在
listView
中滚动可以正常工作,但是
scrollView
没有滚动。@SNSingh使用NestedScrollView而不是ScrollView@SNSingh检查一下谢谢现在两个都很好,但是现在
listView
height
与1项相同。如果我想增加高度怎么办?
listView
本身就有滚动属性,为什么只为listView添加
scrollView
。感谢您的响应,但我必须在
listView
中实现它,这一定是
listView
的原因吗?因为
ScrollView
ListView
都是可滚动的,所以现在布局文件中的设置有一个滚动陷阱。
RecyclerView
可以将所有内容合并到一个大的可滚动列表中,从而帮助您解决这个问题。谢谢@parambir,现在大小已经调整。欢迎@SNSingh,如果答案对您有帮助,请将其标记为已接受。
recyclerView.HasFixedSize();