【问题标题】:ListView shows only one item in a ScrollView?ListView 在 ScrollView 中只显示一项?
【发布时间】:2018-09-07 07:45:00
【问题描述】:

我有一个 ListView,我在其中更改了行的外观,但 listview 的大小为一行,而不是全屏。 我的滚动视图正在工作,但列表视图不工作。

activity_graph_view.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_graph_table_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="00dp"
    android:background="@color/background"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

        </LinearLayout>

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbar"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp">

                <LinearLayout
                    android:id="@+id/layoutTableOne"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:visibility="visible">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="Table View"
                        android:textSize="@dimen/subheading"
                        android:textColor="@color/subheading"
                        android:textAllCaps="false" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <ListView
                            android:id="@+id/lv_spirometer"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:divider="@color/mdtp_button_selected"
                            android:dividerHeight="1dp" />

                    </LinearLayout>

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:visibility="visible"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:weightSum="1">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.9"
                            android:padding="10dp"
                            android:text="Graph View"
                            android:textAllCaps="false"
                            android:textColor="@color/subheading"
                            android:textSize="@dimen/subheading" />

                    </LinearLayout>

                    <com.jjoe64.graphview.GraphView
                        android:id="@+id/grapfinal"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_weight="1" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="5dp"
                        android:weightSum="1">

                        <LinearLayout
                            android:id="@+id/grphtextMain"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <LinearLayout
                                android:id="@+id/grphtextColor"
                                android:layout_width="10dp"
                                android:layout_height="10dp"
                                android:layout_marginTop="3dp"
                                android:orientation="vertical" />

                            <TextView
                                android:id="@+id/grphtext"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="5dp"
                                android:text="Hello"
                                android:textColor="@color/subheading"
                                android:textSize="14dp" />

                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/grphtextMain2"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <LinearLayout
                                android:id="@+id/grphtextColor2"
                                android:layout_width="10dp"
                                android:layout_height="10dp"
                                android:layout_marginTop="3dp"
                                android:background="@color/red_btn_bg_color"
                                android:orientation="vertical" />

                            <TextView
                                android:id="@+id/grphtext2"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="5dp"
                                android:text="Hello"
                                android:textColor="@color/subheading"
                                android:textSize="14dp" />

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:visibility="visible"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:weightSum="1">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="2">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.9"
                            android:padding="10dp"
                            android:text="Graph View"
                            android:textAllCaps="false"
                            android:textColor="@color/subheading"
                            android:textSize="@dimen/subheading" />

                    </LinearLayout>

                    <com.jjoe64.graphview.GraphView
                        android:id="@+id/grapfinal1"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_weight="1" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="5dp"
                        android:weightSum="1">

                        <LinearLayout
                            android:id="@+id/grphtextMain1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <LinearLayout
                                android:id="@+id/grphtextColor1"
                                android:layout_width="10dp"
                                android:layout_height="10dp"
                                android:layout_marginTop="3dp"
                                android:background="@color/red_btn_bg_color"
                                android:orientation="vertical" />

                            <TextView
                                android:id="@+id/grphtext1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="5dp"
                                android:text="Hello"
                                android:textColor="@color/subheading"
                                android:textSize="14dp" />

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

spirometer_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:cardBackgroundColor="#fff4f4f3"
        app:cardElevation="10dp"
        app:cardPreventCornerOverlap="true"
        card_view:cardCornerRadius="8dp">

        <LinearLayout
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_date"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_fvc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="FVC 3.15 L" />

                <TextView
                    android:id="@+id/tv_fev1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="FEV1 2.44 L" />

                <TextView
                    android:id="@+id/tv_pef"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="PEF 3.74 L/s" />

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

CustomAdapter1.java

public class CustomAdapter1 extends BaseAdapter {
        ArrayList<GetUserSpirometer> arrayList;

        public CustomAdapter1(ArrayList<GetUserSpirometer> arrayList){
            this.arrayList=arrayList;
            Log.e("arraylist length",""+arrayList.size());
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater= getLayoutInflater();
            ViewHolder1 holder=new ViewHolder1();
            convertView = inflater.inflate(R.layout.spirometer_item, parent, false);
            holder.tv_date=(TextView)convertView.findViewById(R.id.tv_date);
            holder.tv_fvc=(TextView)convertView.findViewById(R.id.tv_fvc);
            holder.tv_fev1=(TextView)convertView.findViewById(R.id.tv_fev1);
            holder.tv_pef=(TextView)convertView.findViewById(R.id.tv_pef);

            String[] arr = getDate(Long.parseLong(arrayList.get(position).get_date()), "MMM dd, yyy/hh:mm a").split("/");
            holder.tv_date.setText(arr[0] + "\n" + arr[1]);
            holder.tv_fvc.setText(arrayList.get(position).get_userfvc());
            holder.tv_fev1.setText(arrayList.get(position).get_userfev1());
            holder.tv_pef.setText(arrayList.get(position).get_userpef());
            convertView.setTag(holder);

            return convertView;
        }
    }

项目不滚动。

如何解决这个问题。请帮忙。

提前致谢。

【问题讨论】:

  • 试试在你的spirometer_item.xml中将LinearLayout的高度更改为android:layout_height="wrap_content"
  • 使用 NestedScroll View 而不是 ScrollView。
  • @NileshRathod 不工作
  • @Prabal.PX 使用recyclerView 代替ListViewNestedScroll View 代替ScrollView
  • 可滚动布局(scrollview、recyclerview)的高度应为“wrap_content”,与父级不匹配

标签: android listview scrollview


【解决方案1】:

那你需要修改itemView的xml文件,

 android:layout_height="match_parent"

而不是喜欢,

 android:layout_height="wrap_content"

【讨论】:

  • 不用改layout_width
【解决方案2】:
Simple change in spirometer_item.xml

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


....Your design code 

 </LinearLayout>

【讨论】:

    【解决方案3】:

    很长一段时间以来,我都遇到了同样的问题。然后我找到了一个适合我的解决方案。 添加一个 ListViewHelper java 类。下面是 ListViewHelper.java 的代码

    package com.molescope;
    
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    public class ListViewHelper {
        public static void getListViewSize(ListView listView){
            ListAdapter adapter = listView.getAdapter();
            if(adapter!=null){
                int totalHeight = 0;
    
                //setting list adapter in loop tp get final size
                for (int i=0; i<adapter.getCount(); i++){
                    View listItem = adapter.getView(i, null, listView);
                    listItem.measure(0,0);
                    totalHeight += listItem.getMeasuredHeight();
                }
                //setting listview items in adapter
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                params.height = totalHeight + (listView.getDividerHeight() * 
        (adapter.getCount()-1));
                listView.setLayoutParams(params);
    
            }else{
                return;
            }
        }
    }
    

    添加此 java 文件后,在代码中将适配器设置为 listview 的任何位置,在该行之后添加以下代码:

      ListView myList=(ListView) findViewById(R.id.listView);
      myList.setAdapter(new ArrayAdapter<String>. 
      (this,android.R.layout.simple_list_item_1, listview_array));
      ListViewHelper.getListViewSize(myList);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      相关资源
      最近更新 更多