填充使用ListFragment自定义视图列表 [英] Populate list of custom view using ListFragment

查看:351
本文介绍了填充使用ListFragment自定义视图列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示使用中的一个片段列表视图元素。我创造了我的自定义视图如下

I am trying to show elements in a list view using Fragments. I created my custom view as follow

图形重新presentation 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之文件,我只是把一个列表视图的线性布局中

In fragment.xml file, I simply put a list view inside a linear layout

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像菜单名称,描述,价格和形象。我无法找到一个方法来填补fragment.xml之的ListView控件与list_row元素。任何帮助或想法是AP preciated。

I have an array of menu objects, that has the necessary fields to populate list_row.xml like menu name, description, price and image. I couldn't find a way to fill fragment.xml's listView with the list_row elements. Any help or idea would be appreciated.

PS:我觉得在fragment.xml之,而不是机器人:ID =@ + ID / ListView1的字段,我必须写机器人:ID =@ ID /列表,因为我使用ListFragment

PS: I think in fragment.xml instead of android:id="@+id/listView1" field, I have to write android:id="@id/list" since I'm using ListFragment

推荐答案

我解决我的问题(如SYSTEM32建议在评论)创建CustomArrayAdapter类,并将其设置为我的ListView适配器。

I solved my problem (as system32 suggests on comment) creating a CustomArrayAdapter class, and setting it as the adapter for my listView.

首先,我改变了机器人:ID =@ + ID / ListView1的机器人:ID =@机器人:ID /列表在fragment.xml之。

First I changed android:id="@+id/listView1" to android:id="@android:id/list" in fragment.xml.

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;
    }

}

然后,我在我的片段类使用它

Then I use it in my Fragment class

    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);
        }
    }

这篇关于填充使用ListFragment自定义视图列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆