你的位置:首页 > 操作系统

[操作系统]第一行代码笔记之——ListView

1.ListView 是啥

ListView  就是一个能显示一排一排的东西View控件

 

2.使用ListView的步骤

  1. 创建一个class,代表你每一行想放的东东
  2. 给你每一行的东东一个布局文件,在activity的布局文件中加一个ListView  控件
  3. New一个ArrayList,用来储存你想放的东东们
  4. 在Activity中写一个初始化函数init(),初始化东东,然后把东东add到ArrayList中
  5. 如果是创建复杂控件,要建一个继承于ArrayAdapter的类,再到里面添加复杂功能
  6. New一个你的ArrayAdapter,把每一行的那个
  7. New一个ListView  ,并findViewById,把ArrayAdapter给它。

 

3.最简单的ListView

     1.在

<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools" android:layout_width="match_parent"  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  <ListView    android:id="@+id/list_view"    android:layout_width="match_parent"    android:layout_height="match_parent"/></RelativeLayout>

      2.

          (1)在MainActivity中用一个Sting[]储存每行数据

          (2)把数据给ArrayAdapter

          (3)把ArrayAdapter给ListView 

public class MainActivity extends Activity {  private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};//要储存的数据  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);//把每一行内容的    ListView listView=(ListView)findViewById(R.id.list_view);    listView.setAdapter(adapter);//把ArrayAdapter给ListView  }

 

效果图:

 

 

4.ListView的事件监听器

listView.setOnItemClickListener(new OnItemClickListener() {      @Override      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {        Fruit fruit=fruitList.get(position);        Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_LONG).show();      }    });

 

5.复杂的ListView

1.新建一个Fruit类

   作用:帮助我们储存与获得“水果名称”,与“图片ID”

public class Fruit{  private String name;  private int imageId;  public Fruit(String name,int imageId)  {    this.name=name;    this.imageId=imageId;  }  public String getName()  {    return name;  }  public int getImageId()  {    return imageId;  }}

 

2.新建一个

   作用:来作为每行内容的布局文件

<??><LinearLayout ="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent" android:layout_height="match_parent">  <ImageView    android:id="@+id/fruit_image"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />  <TextView    android:id="@+id/fruit_name"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:layout_marginLeft="10dip"/></LinearLayout>

 

3.初始化数据

    步骤:

           ①new一个ArrayList<Fruit>

           ②在MainActivity中写一个函数init()。

 

    作用:初始化数据,并将数据add到ArrayList<Fruit>中

public class MainActivity extends Activity {  private List<Fruit> fruitList=new ArrayList<Fruit>();//①new一个ArrayList<Fruit>  @Override  protected void onCreate(Bundle savedInstanceState)   {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    init();//初始化数据  }  private void init()  {    Fruit apple=new Fruit("Apple",R.drawable.apple);fruitList.add(apple);//将数据add到ArrayList<Fruit>中    Fruit banana=new Fruit("Banana",R.drawable.banana);fruitList.add(banana);    Fruit orange=new Fruit("Orange",R.drawable.orange);fruitList.add(orange);    Fruit watermelon=new Fruit("Watermelon",R.drawable.watermelon);fruitList.add(watermelon);    Fruit pear=new Fruit("Pear",R.drawable.pear);fruitList.add(pear);    Fruit grape=new Fruit("Grape",R.drawable.grape);fruitList.add(grape);    Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple);fruitList.add(pineapple);    Fruit strawberry=new Fruit("Strawberry",R.drawable.strawberry);fruitList.add(strawberry);    Fruit cherry=new Fruit("Cherry",R.drawable.cherry);fruitList.add(cherry);    Fruit mango=new Fruit("Mango",R.drawable.mango);fruitList.add(mango);  }

 

4.新建一个继承于ArrayAdapter<Fruit>的FruitAdapter类

   作用:用来设定水果名称与水果图片

public class FruitAdapter extends ArrayAdapter<Fruit>{  private int resourceId;//等会要把R.layout.fruit_item的值赋给resourceId    class ViewHolder//用来暂存“水果名称”的TextView控件与“水果图片”的ImageView控件,避免每次都重新加载布局,优化程序的流畅度  {    ImageView fruitImage;    TextView fruitName;  }    public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objects)  {    super(context,textViewResourceId,objects);    resourceId=textViewResourceId;//把R.layout.fruit_item的值赋给resourceId  }  public View getView(int position,View convertView,ViewGroup parent)//这个函数不是太清楚,大概是用来createView的,对于每一个添加进ArrayList<Fruit>的Fruit对象,都会执行一边  {    Fruit fruit=getItem(position);//实例化在ArrayList<Fruit>中第“position”个当前Fruit对象    View view;    ViewHolder viewHolder;    if (convertView==null)//如果布局从来没有被加载过    {      view=LayoutInflater.from(getContext()).inflate(resourceId,null);//使用布局填充器来把fruit_item布局文件转为View      viewHolder=new ViewHolder();//新建一个ViewHolder      viewHolder.fruitImage=(ImageView)view.findViewById(R.id.fruit_image);//从View中获取ImageView,并暂存新建的ViewHolder中      viewHolder.fruitName=(TextView)view.findViewById(R.id.fruit_name);//从View中获取TextView,并暂存新建的ViewHolder中      view.setTag(viewHolder);//使用setTag把查找的view缓存起来方便多次重用    }    else//布局被加载过    {      view=convertView;      viewHolder=(ViewHolder)view.getTag();//把之前暂存的ViewHolder赋给viewHolder    }    viewHolder.fruitImage.setImageResource(fruit.getImageId());//设定水果图片    viewHolder.fruitName.setText(fruit.getName());//设定水果名称    return view;  }}

 

5把R.layout.fruit_item和初始化完毕的ArrayList<Fruit>给FruitAdapter,然后把FruitAdapter给ListView

public class MainActivity extends Activity{  private List<Fruit> fruitList=new ArrayList<Fruit>();//①new一个ArrayList<Fruit>  @Override  protected void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    init();//初始化数据    FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);//把R.layout.fruit_item和初始化完毕的ArrayList<Fruit>给FruitAdapter    ListView listView=(ListView)findViewById(R.id.list_view);    listView.setAdapter(adapter);//把FruitAdapter给ListView  }  private void init()  {    Fruit apple=new Fruit("Apple",R.drawable.apple);fruitList.add(apple);//将数据add到ArrayList<Fruit>中    Fruit banana=new Fruit("Banana",R.drawable.banana);fruitList.add(banana);    Fruit orange=new Fruit("Orange",R.drawable.orange);fruitList.add(orange);    Fruit watermelon=new Fruit("Watermelon",R.drawable.watermelon);fruitList.add(watermelon);    Fruit pear=new Fruit("Pear",R.drawable.pear);fruitList.add(pear);    Fruit grape=new Fruit("Grape",R.drawable.grape);fruitList.add(grape);    Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple);fruitList.add(pineapple);    Fruit strawberry=new Fruit("Strawberry",R.drawable.strawberry);fruitList.add(strawberry);    Fruit cherry=new Fruit("Cherry",R.drawable.cherry);fruitList.add(cherry);    Fruit mango=new Fruit("Mango",R.drawable.mango);fruitList.add(mango);  }}

 

效果图: