安卓入门学习之ListView
在安卓的数据列表显示的时候,通常会用到ListView,所以列出下学习时候遇到的
1,用系统自定义的ListView
首先在xml文件中建立listview
<ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView>然后在MainAvtivity中建立数据,此时的数据一般都是提前准备好的,一般是网上下载的或者从数据库中提取的
private String[] data = { "Apple" , "Banana" , "Orange" , "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState); requestWindowFeature(Window. FEATURE_NO_TITLE); 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); }
<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="10dp" />
建立一个类,继承适配器类
public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId ; public FruitAdapter(Context context, int resource, List<Fruit> objects) { super( context, resource, objects); // TODO Auto-generated constructor stub resourceId= resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub /*return super.getView(position, convertView, parent);*/ //获取当前的fruit实例 Fruit fruit= getItem( position); View view= LayoutInflater.from(getContext()).inflate(resourceId , null); ImageView fruitImage= (ImageView) view.findViewById(R.id. fruit_image); TextView fruitNmae=(TextView) view.findViewById(R.id. fruit_name); fruitImage.setImageResource( fruit.getImageId()); fruitNmae.setText( fruit.getName()); return view; }
public class MainActivity extends Activity { /*private String[] data = { "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };*/ private List<Fruit> fruitList = new ArrayList<Fruit>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState); /*requestWindowFeature(Window.FEATURE_NO_TITLE);*/ 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);*/ //初始化水果 initFruit(); FruitAdapter adapter= new FruitAdapter(MainActivity. this, R.layout.fruit_item , fruitList ); ListView listView=(ListView) findViewById(R.id.list_view ); listView.setAdapter( adapter); } private void initFruit() { // TODO Auto-generated method stub Fruit apple = new Fruit( "Apple", R.drawable.apple_pic ); fruitList.add( apple); Fruit banana = new Fruit( "Banana", R.drawable.banana_pic ); fruitList.add( banana); Fruit orange = new Fruit( "Orange", R.drawable.orange_pic ); fruitList.add( orange); Fruit watermelon = new Fruit( "Watermelon", R.drawable.watermelon_pic ); fruitList.add( watermelon); Fruit pear = new Fruit( "Pear", R.drawable. pear_pic); fruitList.add( pear); Fruit grape = new Fruit( "Grape", R.drawable.grape_pic ); fruitList.add( grape); Fruit pineapple = new Fruit( "Pineapple", R.drawable.pineapple_pic ); fruitList.add( pineapple); Fruit strawberry = new Fruit( "Strawberry", R.drawable.strawberry_pic ); fruitList.add( strawberry); Fruit cherry = new Fruit( "Cherry", R.drawable.cherry_pic ); fruitList.add( cherry); Fruit mango = new Fruit( "Mango", R.drawable.mango_pic ); fruitList.add( mango); }主要是listview完成初始化,然后产生一个自定义的适配器进行装配数据,传给ListView
方法中每次都将布局重新加载了一遍,当 ListView 快速滚动的时候这就会成为性能的瓶颈。
仔细观察,getView()方法中还有一个 convertView 参数,这个参数用于将之前加载好的
布局进行缓存, 以便之后可以进行重用
public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId; public FruitAdapter(Context context, int resource, List<Fruit> objects) { super(context, resource, objects); // TODO Auto-generated constructor stub resourceId=resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub /*return super.getView(position, convertView, parent);*/ //获取当前的fruit实例 Fruit fruit= getItem(position); /* * convertView主要是将之前加载好的布局进行缓存 */ View view; ViewHolder viewHolder; if(convertView==null) { view= LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder=new ViewHolder(); viewHolder.fruitImage=(ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName=(TextView) view.findViewById(R.id.fruit_name); } else { view =convertView; viewHolder=(ViewHolder) view.getTag(); } /* ImageView fruitImage= (ImageView) view.findViewById(R.id.fruit_image); TextView fruitNmae=(TextView) view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitNmae.setText(fruit.getName());*/ viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder{ ImageView fruitImage; TextView fruitName; } }
主要是新增了一个ViewHolder类来放相应的实例,同时由于converView是存放布局缓存的,所以在判断其是否为空的时候可以一并判断控件是否需要绘制
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。