zoukankan      html  css  js  c++  java
  • 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;
        }
    }

  • 相关阅读:
    Python编码和文件操作
    Python的list和tuple及dictionary
    Python代码对比
    python的基础知识
    day2 springcloud组件(nacos注册/配置中心 feign组件请求调用 gateway网关)
    springcloud与springboot版本对应关系
    git配置
    day1 分布式基础概念
    spring中的依赖注入(DI)笔记
    20210223 爱生气的书店老板(滑动窗口)
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4323853.html
Copyright © 2011-2022 走看看