【问题标题】:ListView Inside CardView not Showing its full lengthCardView 内的 ListView 未显示其完整长度
【发布时间】:2016-04-01 14:08:45
【问题描述】:

您好,我已经在 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>

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要在 xml 代码中为 ListView 指定一定的高度。当数据膨胀到其中时,WRAP_CONTENT 不会扩展您的 ListView。指定一些高度,例如 200dp 之类的。

    【讨论】:

    • 是的,但情况不同。不同用户不会有相同的列表行。如果列表项的数量有限,您的答案有效。
    • 你能给我任何其他有效的解决方案吗?这对我不起作用
    • 你可以做一件事。根据您对 RecyclerView 的每个项目的要求动态设置高度。这样,您的 ListView 将根据 RecyclerView 中每个项目的要求具有高度。
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2017-02-13
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多