Cardview里面的ListView没有显示完整的数据

时间:2016-05-16 05:06:08

标签: android listview

如何在cardview里面解决这个问题。在下图中我有listview,但问题是我没有显示完整的数据。只有第一行可见: this is the image. inside the red circle part i have 3 rows but it is not visible

//这是我的代码

 <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

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

                <TextView
                    android:id="@+id/qualification"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:background="#E0E0E0"
                    android:gravity="center"
                    android:text="Qualifications"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title"
                    android:textColor="#4A148C" />

                <TextView
                    android:id="@+id/addqualification"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/qualification"
                    android:layout_marginTop="15dp"
                    android:background="?attr/selectableItemBackground"
                    android:clickable="true"
                    android:drawableLeft="@drawable/ic_add"
                    android:drawablePadding="20dp"
                    android:drawableStart="@drawable/ic_add"
                    android:paddingLeft="30dp"
                    android:text="Add Qualifications"
                    android:textColor="#0e89d0" />

                <ListView
                    android:id="@+id/listQualification"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/addqualification">

                </ListView>

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

//这是我的list_row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >
<TextView
    android:id="@+id/course"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:textStyle="bold"
    android:layout_marginLeft="10dp"
    android:text="Course:"
    android:visibility="gone"
    />
<TextView
    android:id="@+id/courseName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/course"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="Course Name"
    android:maxLength="255"
    android:maxLines="4"
    android:textSize="10sp"
    android:ellipsize="middle"
    />
 <TextView
     android:id="@+id/university"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/courseName"
     android:text="University:"
     android:layout_marginLeft="10dp"
     android:textSize="10sp"
     android:textStyle="bold"
     android:visibility="gone"
     android:layout_marginTop="3dp"/>
    <TextView
        android:id="@+id/universityName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/courseName"
        android:layout_toRightOf="@+id/university"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="3dp"
        android:textSize="10sp"
        android:text="University Name"
        android:ellipsize="middle"
        android:maxLines="4"
        />
    <TextView
        android:id="@+id/year"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/universityName"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="3dp"
        android:textSize="10sp"
        android:textStyle="bold"
        android:text="Year:"
        android:visibility="gone"/>
    <TextView
        android:id="@+id/year1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/universityName"
        android:layout_toRightOf="@+id/year"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="10dp"
        android:textSize="10sp"
        android:ellipsize="middle"
        android:maxLines="4"
        android:text="Year" />
</RelativeLayout>

//这是我的适配器类

 public class QualificationAdapter extends BaseAdapter{
public ArrayList<Qualification> mQualification;
Context context;

public QualificationAdapter(ArrayList<Qualification> mQualification, Context 

context) {
    this.context = context;
    this.mQualification = mQualification;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = null;
    Qualification qualification = mQualification.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)  

        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_qualification_row, parent, 

    false);
    } else {
        v = convertView;
    }
    TextView course = (TextView) v.findViewById(R.id.course);
    TextView university = (TextView) v.findViewById(R.id.university);
    TextView year = (TextView) v.findViewById(R.id.year);

    TextView courseName = (TextView) v.findViewById(R.id.courseName);
    courseName.setText(qualification.getCourse());

    TextView universityName = (TextView)   

    v.findViewById(R.id.universityName);
    universityName.setText(qualification.getUniversity());

    TextView year1 = (TextView) v.findViewById(R.id.year1);
    year1.setText(qualification.getYear());

    course.setVisibility(View.VISIBLE);
    university.setVisibility(View.VISIBLE);
    year.setVisibility(View.VISIBLE);

    return v;

}

}

2 个答案:

答案 0 :(得分:0)

您需要提供listView的自定义高度。

<ListView
.....
android:layout_height="150dp"/>

答案 1 :(得分:0)

为此只需使用自定义ListView。

创建Java文件 MyListView

    public class MyListView extends ListView {

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}

,然后在您的布局中替换列表视图:

<yourpackagename.MyListView
        ...       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... />