ListView在ScrollView中仅显示一项?

时间:2018-09-07 07:45:01

标签: android listview scrollview

我有一个ListView,在这里我更改了行的外观,但是listview的大小为一行,而不是全屏。 而且我的scrollview正常工作,但是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;
        }
    }

项目不滚动。

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

提前谢谢。

3 个答案:

答案 0 :(得分:1)

然后您需要更改itemView的xml文件

 android:layout_height="match_parent"

代替like,

 android:layout_height="wrap_content"

答案 1 :(得分:0)

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>

答案 2 :(得分:0)

这么长时间以来我一直在遇到同样的问题。然后我找到了对我有用的解决方案。 添加一个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 myList=(ListView) findViewById(R.id.listView);
  myList.setAdapter(new ArrayAdapter<String>. 
  (this,android.R.layout.simple_list_item_1, listview_array));
  ListViewHelper.getListViewSize(myList);