【问题标题】:Populate list of custom view using ListFragment使用 ListFragment 填充自定义视图列表
【发布时间】:2013-04-30 02:54:53
【问题描述】:

我正在尝试使用片段在列表视图中显示元素。我创建了我的自定义视图如下

list_row.xml 的图形表示

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" 
            android:contentDescription="@string/image_desc"/>
    </LinearLayout>

    <!-- Menu name -->

    <TextView
        android:id="@+id/menu_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="@string/menu_name"
        android:textColor="#040404"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- Description -->

    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/thumbnail"
        android:layout_below="@id/menu_name"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="@string/description"
        android:textColor="#343434"
        android:textSize="10sp"
        tools:ignore="SmallSp" />

    <!-- Price -->

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/menu_name"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:text="@string/price"
        android:textColor="#10bcc9"
        android:textSize="10sp"
        android:textStyle="bold"
        tools:ignore="SmallSp" />

</RelativeLayout>

在 fragment.xml 文件中,我只是将列表视图放在线性布局中

fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

我有一个菜单对象数组,其中包含填充 list_row.xml 所需的字段,例如菜单名称、描述、价格和图像。我找不到用 list_row 元素填充 fragment.xml 的 listView 的方法。任何帮助或想法将不胜感激。

PS:我认为在fragment.xml而不是android:id="@+id/listView1"字段中,我必须写android:id="@id/list",因为我使用的是ListFragment

【问题讨论】:

  • 要将 list_row.xml 用于您的列表视图,只需创建一个扩展 ArrayAdapter 的类,然后覆盖 onCreateView()
  • 感谢您的帮助。我解决了我的问题

标签: android android-listview android-custom-view android-listfragment


【解决方案1】:

我解决了我的问题(正如 system32 在评论中建议的那样)创建一个 CustomArrayAdapter 类,并将其设置为我的 listView 的适配器。

首先我在 fragment.xml 中将 android:id="@+id/listView1" 更改为 android:id="@android:id/list"

CustomArrayAdapter.java

public class CustomArrayAdapter extends ArrayAdapter<Menu> {

    Context context;

    public CustomArrayAdapter(Context context, int textViewResourceId, List<Menu> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtMenuName;
        TextView txtMenuDesc;
        TextView txtPrice;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Menu rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_row, null);
            holder = new ViewHolder();
            holder.txtMenuName = (TextView) convertView.findViewById(R.id.menu_name);
            holder.txtMenuDesc = (TextView) convertView.findViewById(R.id.description);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
            holder.imageView = (ImageView) convertView.findViewById(R.id.list_image);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtMenuDesc.setText(rowItem.getDescription());
        holder.txtMenuName.setText(rowItem.getName());
        holder.txtPrice.setText(String.valueOf(rowItem.getPrice()) + " TL");
        //holder.imageView.setImageResource(rowItem.getImageId());

        return convertView;
    }

}

然后我在我的 Fragment 类中使用它

    public static class Fragment extends ListFragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        private ListView listView;
        private ArrayList<Menu> menuItems;
        private CustomArrayAdapter mAdapter;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment,
                    container, false);
            listView = (ListView) rootView.findViewById(android.R.id.list);

            return rootView;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            int num = getArguments().getInt(ARG_SECTION_NUMBER);
            // GlobalList is a class that holds global variables, arrays etc
            // getMenuCategories returns global arraylist which is initialized in GlobalList class
            menuItems = GlobalList.getMenuCategories().get(num).getMenu();
            mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, menuItems);
            listView.setAdapter(mAdapter);
        }
    }

【讨论】:

  • 我尝试使用您的方法,但在我的应用程序中不起作用。我在 LogCat 中发现了这个错误:08-06 19:15:55.099: E/AndroidRuntime(14285): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
  • @inankupeli 即使我有同样的问题老兄你能检查一下我发布的问题stackoverflow.com/questions/23513118/…
  • 你给了我解决我最后一次内存泄漏所需的线索。在扩展 ListFragment 的 onCreate 中设置 ArrayAdapter 会导致泄漏,onActivityCreated 可以避免泄漏。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
相关资源
最近更新 更多