• Android Adapter的getViewTypeCount和getItemViewType


    Adapter的getViewTypeCount和getItemViewType

    不同的项目布局(item layout)

    我们再举一个稍微复杂的例子,在上例的list中加入一些分隔线

    你需要做这些:

    1. 重(@Override)写 getViewTypeCount() – 返回你有多少个不同的布局
    2. 重写 getItemViewType(int) – 由position返回view type id
    3. 根据view item的类型,在getView中创建正确的convertView

    以下是代码:

    public class MultipleItemsList extends ListActivity {
     
        private MyCustomAdapter mAdapter;
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mAdapter = new MyCustomAdapter();
            for (int i = 1; i < 50; i++) {
                mAdapter.addItem("item " + i);
                if (i % 4 == 0) {
                    mAdapter.addSeparatorItem("separator " + i);
                }
            }
            setListAdapter(mAdapter);
        }
     
        private class MyCustomAdapter extends BaseAdapter {
     
            private static final int TYPE_ITEM = 0;
            private static final int TYPE_SEPARATOR = 1;
            private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
     
            private ArrayList mData = new ArrayList();
            private LayoutInflater mInflater;
     
            private TreeSet mSeparatorsSet = new TreeSet();
     
            public MyCustomAdapter() {
                mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
     
            public void addItem(final String item) {
                mData.add(item);
                notifyDataSetChanged();
            }
     
            public void addSeparatorItem(final String item) {
                mData.add(item);
                // save separator position
                mSeparatorsSet.add(mData.size() - 1);
                notifyDataSetChanged();
            }
     
            @Override
            public int getItemViewType(int position) {
                return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
            }
     
            @Override
            public int getViewTypeCount() {
                return TYPE_MAX_COUNT;
            }
     
            @Override
            public int getCount() {
                return mData.size();
            }
     
            @Override
            public String getItem(int position) {
                return mData.get(position);
            }
     
            @Override
            public long getItemId(int position) {
                return position;
            }
     
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                int type = getItemViewType(position);
                System.out.println("getView " + position + " " + convertView + " type = " + type);
                if (convertView == null) {
                    holder = new ViewHolder();
                    switch (type) {
                        case TYPE_ITEM:
                            convertView = mInflater.inflate(R.layout.item1, null);
                            holder.textView = (TextView)convertView.findViewById(R.id.text);
                            break;
                        case TYPE_SEPARATOR:
                            convertView = mInflater.inflate(R.layout.item2, null);
                            holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                            break;
                    }
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
                holder.textView.setText(mData.get(position));
                return convertView;
            }
     
        }
     
        public static class ViewHolder {
            public TextView textView;
        }
    }

  • 相关阅读:
    OSG快速生成一个带有纹理的四边形Geometry
    Excel计算一列的和sum(A:A)
    如何区分SNAT和DNAT
    openstack 常用命令
    Access an instance through a console
    OpenStack网络指导手册 -基本网络概念
    vlan与交换机端口模式Access,Hybrid,Trunk
    网卡的混杂模式
    What is the difference between provider network and self-service network in OpenStack?
    C/C++程序终止时执行的函数——atexit()函数详解
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4323853.html
Copyright © 2020-2023  润新知