簡體   English   中英

CardView中的ListView內部未顯示其全長

[英]ListView Inside CardView not Showing its full length

您好,我已經在CardView內實現了ListView,但是現在我遇到的問題是,CardView沒有在“添加列表項”上擴展。 有人可以幫助我,如何解決這個問題? 我在stackoverflow上瀏覽了不同的帖子,但是找不到解決方案:(我的代碼在下面

//這是我的Listview適配器類

public class EmploymentHistoryAdapter extends BaseAdapter {
public ArrayList<EmploymentHistory> mEmploymentHistories;
private Context context;

public EmploymentHistoryAdapter(ArrayList<EmploymentHistory> mEmploymentHistories, Context context) {
    this.mEmploymentHistories = mEmploymentHistories;
    this.context = context;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = null;
    EmploymentHistory empHistory = mEmploymentHistories.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_employee_history_row, parent, false);
    } else {

        v = convertView;

    }
    TextView hospital = (TextView) v.findViewById(R.id.hospital);
    TextView department = (TextView) v.findViewById(R.id.department);
    TextView startDate = (TextView) v.findViewById(R.id.startDate);
    TextView endDate = (TextView) v.findViewById(R.id.endDate);

    TextView hospitalName = (TextView) v.findViewById(R.id.hospitalName);
    hospitalName.setText(empHistory.getHospital());
    TextView departmentName = (TextView) v.findViewById(R.id.departmentName);
    departmentName.setText(empHistory.getDepartment());
    TextView startDate1 = (TextView) v.findViewById(R.id.startDate1);
    startDate1.setText(empHistory.getStartDate());
    TextView endDate1 = (TextView) v.findViewById(R.id.endDate1);
    endDate1.setText(empHistory.getEndDate());

    hospital.setVisibility(View.VISIBLE);
    department.setVisibility(View.VISIBLE);
    startDate.setVisibility(View.VISIBLE);
    endDate.setVisibility(View.VISIBLE);

    return v;
}

}

//這是我填充ListView的地方

 private void populateEmploymentHistoryList() {
    listEmployeeHistory = (ListView) findViewById(R.id.listEmployeeHistory);

    empHistory = dbHelper.getAllEmploymentHistory();
    employmentHistoryAdapter = new EmploymentHistoryAdapter(empHistory,this);
    listEmployeeHistory.setAdapter(employmentHistoryAdapter);
}

//這是我的布局文件,在其中添加了ListView,並且具有list_row的自定義設計

<android.support.v7.widget.CardView
            android:id="@+id/employmentHistoryCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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="wrap_content">

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

                <TextView
                    android:id="@+id/addEmployment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/employmentHistory"
                    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 Employment"
                    android:textColor="#0e89d0" />

                <ListView
                    android:id="@+id/listEmployeeHistory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/addEmployment">

                </ListView>

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

您需要在xml代碼中為ListView指定一定的高度。 將數據填充到其中后,WRAP_CONTENT不會擴展您的ListView。 指定一些高度,例如200dp或其他值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM