zoukankan      html  css  js  c++  java
  • android 27 ListView

    效果:

     

    上图中ArrarAdapter是数组的适配器,CursorAdapter是游标适配器,用于操作数据库的数据。

    ListView是垂直列表,数据源是集合或者数组,这些View都是安卓里的AdapterView的子类,都是支持适配器的View,AdapterView是通过Adapter(适配器类)像AdapterView的子类视图填充数据,BaseAdapter是所有适配器类的基类。适配器是MVC的C,View是V,数据是M,

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ListView
            android:id="@+id/lvGeneral"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#ccc"
            android:dividerHeight="2dp"/>  分割线的高度
    
    </RelativeLayout>

    MainActivity.java

    package com.sxt.day05_01;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.sxt.day05_01.entity.GeneralBean;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        ListView mlvGeneral;
        List<GeneralBean> mGenerals;//代表十个军事家的集合
        GeneralAdapter mAdapter;
        int[] resid={//图片数组,
                //里面存的是图片的id整型值
                //public static final int baiqi=0x7f020000;
                // public static final int caocao=0x7f020001;
            R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
            R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
            R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
            R.drawable.zhuyuanzhang
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initData();//初始化数据
            initView();
        }
        
        private void initView() {
            mlvGeneral=(ListView) findViewById(R.id.lvGeneral);//main.xml中的ListView
            mAdapter=new GeneralAdapter();//创建适配器
            mlvGeneral.setAdapter(mAdapter);//关联适配器
        }
    
        private void initData() {
            //将资源中的字符串组数转换为Java数组,generals是int类型,代表strings.xml中定义的string数组<string-array/>标签
            String[] names=getResources().getStringArray(R.array.generals);//getResources获取res资源,
            /*
             strings.xml
             <resources>
            <string name="app_name">Day05_01-ListViewDemo</string>
            <string name="action_settings">Settings</string>
            <string name="hello_world">Hello world!</string>
            <string-array name="generals">
                <item>白起</item>
                <item>曹操</item>
                <item>成吉思汗</item>
                <item>韩信</item>
                <item>李世民</item>
                <item>努尔哈赤</item>
                <item>孙膑</item>
                <item>孙武</item>
                <item>朱元璋</item>
                <item>岳飞</item>
            </string-array>
            </resources>*/
            mGenerals=new ArrayList<GeneralBean>();
            for (int i = 0; i < names.length; i++) {
                GeneralBean bean=new GeneralBean(resid[i], names[i]);
                mGenerals.add(bean);
            }
        }
    
        //定义适配器
        class GeneralAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {//返回所有集合数据数组的长度
                return mGenerals.size();
            }
    
            @Override
            public GeneralBean getItem(int position) {//返回某一行的数据
                return mGenerals.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            //返回ListView的一行给安卓系统,安卓系统拿到View后进行绘制,position某一行View在ListView的索引,convertView缓存当前列表中所有的列表项在内存中,比如数据有100个但是ListView只有10个则每次只显示10个,屏幕滚动的时候再显示后面的数据,但是每次屏幕只显示10个,
            //parent就是ListView
            public View getView(int position, View convertView, ViewGroup parent) {
                //拿到ListViewItem的布局,转换为View类型的对象,item_generals是int类型,代表item_generals.xml文件
                //返回行布局LinearLayout,将R.layout.item_generals布局转换为View的Java对象
                View layout=View.inflate(MainActivity.this, R.layout.item_generals, null);
                //找到显示军事家头像的ImageView,findViewById前面不加layout则在activity_main.xml中找,
                ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);
                //找到显示军事家名字的TextView
                TextView tvName=(TextView) layout.findViewById(R.id.tvName);
                //获取军事中下标是position的军事家对象
                GeneralBean bean=mGenerals.get(position);
                //显示军事家头像
                ivThumb.setImageResource(bean.getResid());
                //显示军事家的姓名
                tvName.setText(bean.getName());
                
                return layout;//返回给安卓系统,则绘制一行,getView被调用getCount次,
            }
            
        }
    }
    item_generals.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <ImageView 
            android:id="@+id/ivThumb"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/baiqi"/>
        <TextView 
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:layout_marginLeft="10dp"   左边距离
            android:text="白起"   
            android:textSize="20sp"    
            android:gravity="center_vertical"/>    内部垂直居中
    </LinearLayout>

    GeneralBean.java

    public class GeneralBean {
    
        private int resid;//图片的id值
        private String name;//军事家的姓名
        public int getResid() {
            return resid;
        }
        public void setResid(int resid) {
            this.resid = resid;
        }
  • 相关阅读:
    机器学习:简介
    对API的理解
    软件的运行
    大数据:数据库概念及分类
    Python:easygui的安装、导入、使用、设置
    Python:模块详解及import本质
    Python:urllib模块的urlretrieve方法
    jQuery操作checkbox实例
    ASP.NET MVC 路由调试工具Router Debugger
    认识Visual Studio 条件编译
  • 原文地址:https://www.cnblogs.com/yaowen/p/4886828.html
Copyright © 2011-2022 走看看