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

  • 相关阅读:
    将小度WiFi改造为无线网卡(小度WiFi能够接收WiFi信号)
    百度8秒教育片
    MATLAB r2014a 下载+安装+激活
    lsmod
    ipython与python的区别
    ssh远程登录Ubuntu报错:Permission denied, please try again.
    windows系统安装ubuntu后,grub中没有windows启动项
    面向对象三大特征
    进程和线程
    tar 命令
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4323853.html
Copyright © 2020-2023  润新知