• 02-10Android学习进度报告十


        今天我学习了有关ListView的基础知识,主要是学习了其中界面展示的基本方法。

    首先看一个简单的列表实现代码:

    public class Animal {
        private String aName;
        private String aSpeak;
        private int aIcon;
    
        public Animal() {
        }
    
        public Animal(String aName, String aSpeak, int aIcon) {
            this.aName = aName;
            this.aSpeak = aSpeak;
            this.aIcon = aIcon;
        }
    
        public String getaName() {
            return aName;
        }
    
        public String getaSpeak() {
            return aSpeak;
        }
    
        public int getaIcon() {
            return aIcon;
        }
    
        public void setaName(String aName) {
            this.aName = aName;
        }
    
        public void setaSpeak(String aSpeak) {
            this.aSpeak = aSpeak;
        }
    
        public void setaIcon(int aIcon) {
            this.aIcon = aIcon;
        }
    }

    然后学习了表头表尾分割线的设置:

    首先来看属性:

    • footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条,默认为true
    • headerDividersEnabled:是否在headerView(表头)前绘制一个分隔条,默认为true
    • divider:设置分隔条,可以用颜色分割,也可以用drawable资源分割
    • dividerHeight:设置分隔条的高度

    翻遍了了API发现并没有可以直接设置ListView表头或者表尾的属性,只能在Java中写代码 进行设置了,可供我们调用的方法如下:

    • addHeaderView(View v):添加headView(表头),括号中的参数是一个View对象
    • addFooterView(View v):添加footerView(表尾),括号中的参数是一个View对象
    • addHeaderView(headView, null, false):和前面的区别:设置Header是否可以被选中
    • addFooterView(View,view,false):同上

    对了,使用这个addHeaderView方法必须放在listview.setAdapter前面,否则会报错。

    代码示例:

    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    
        private List<Animal> mData = null;
        private Context mContext;
        private AnimalAdapter mAdapter = null;
        private ListView list_animal;
        private LinearLayout ly_content;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = MainActivity.this;
            list_animal = (ListView) findViewById(R.id.list_animal);
            //动态加载顶部View和底部View
            final LayoutInflater inflater = LayoutInflater.from(this);
            View headView = inflater.inflate(R.layout.view_header, null, false);
            View footView = inflater.inflate(R.layout.view_footer, null, false);
    
            mData = new LinkedList<Animal>();
            mData.add(new Animal("狗说", "你是狗么?", R.mipmap.ic_icon_dog));
            mData.add(new Animal("牛说", "你是牛么?", R.mipmap.ic_icon_cow));
            mData.add(new Animal("鸭说", "你是鸭么?", R.mipmap.ic_icon_duck));
            mData.add(new Animal("鱼说", "你是鱼么?", R.mipmap.ic_icon_fish));
            mData.add(new Animal("马说", "你是马么?", R.mipmap.ic_icon_horse));
            mAdapter = new AnimalAdapter((LinkedList<Animal>) mData, mContext);
            //添加表头和表尾需要写在setAdapter方法调用之前!!!
            list_animal.addHeaderView(headView);
            list_animal.addFooterView(footView);
    
            list_animal.setAdapter(mAdapter);
            list_animal.setOnItemClickListener(this);
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(mContext,"你点击了第" + position + "项",Toast.LENGTH_SHORT).show();
        }
    }

    这就是今天所学知识。

  • 相关阅读:
    GCC编绎详解
    GUN C/C++ __attribute__ 用法 转
    rust 参考的资料 转
    Eclipse环境安装rust
    GNU Debugger for Windows----GDB
    minGW cygwin gnuwin32
    tdm-gcc
    GNU tools
    The MinGW and mingw-w64 projects.----GCC
    crosstool-NG
  • 原文地址:https://www.cnblogs.com/yang2000/p/12323492.html
Copyright © 2020-2023  润新知