ListView中的膨胀视图不可见

时间:2016-01-26 18:09:14

标签: android listview

我正在尝试创建一个包含动态数量项目的膨胀对话框。 这些项目我也充气。

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    int anzJobViews = getArguments().getInt("param");

    View rootView = inflater.inflate(R.layout.dialog_job_chooser, container, false);
    //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    ListView listview = (ListView) rootView.findViewById(R.id.dialog_chooser_listview);

    for (int i = 0; i < anzJobViews; i++) {
        listview.addFooterView( new JobItemView(context));
        Log.i("JobChooser","Item added");
    }

    return rootView;
}

视图(包含一些简单的TextViews)

private void init() {

    inflate(getContext(), R.layout.job_item_view, this);

}

显示对话框时,我看不到任何内容。 对话框的xml布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:background="@drawable/round_corners_selected"
    android:backgroundTint="@color/washedOutGreen">

    <ListView
        android:id="@+id/dialog_chooser_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>

要添加到列表中的项目。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/blue_focused">

    <TextView
        android:id="@+id/tv_jobname_area"
        android:text="defaultName"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_jobvalue_area"
        android:text="0 /h"
        android:layout_margin="5dp"
        android:layout_toRightOf="@+id/tv_jobname_area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

使用getView功能设置列表视图

public View getView (int position, View convertView, ViewGroup list)  {
        View element;
        if (convertView == null) {
         element = View.inflate(context, R.layout.items, null);
        }
        else {
            element = convertView;
        }
//setup here something
//e.g.
TextView tv = (TextView) v.findViewById(R.id.tv);
tv.setText("myText");
    return element;
    }

答案 1 :(得分:0)

为什么不使用适配器填充列表视图?这里有一个例子:http://www.pcsalt.com/android/listview-using-baseadapter-android/

    public MyBaseAdapter(Context context, ArrayList myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

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

    @Override
    public ListData getItem(int position) {
        return myList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder mViewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        ListData currentListData = getItem(position);

        mViewHolder.tvTitle.setText(currentListData.getTitle());
        mViewHolder.tvDesc.setText(currentListData.getDescription());
        mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());

        return convertView;
    }

    private class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;

        public MyViewHolder(View item) {
            tvTitle = (TextView) item.findViewById(R.id.tvTitle);
            tvDesc = (TextView) item.findViewById(R.id.tvDesc);
            ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
        }
    }
}

此外,如果您在pre kitkat环境中,则只能在设置适配器之前使用addFooterView。

  

注意:首次引入时,只能在使用setAdapter(ListAdapter)设置适配器之前调用此方法。从KITKAT开始,可以随时调用此方法。如果ListView的适配器没有扩展HeaderViewListAdapter,它将被WrapperListAdapter的支持实例包装。

参考:http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)