一、ListView的初级优化


public classMainActivity extends Activity {

privateListView lv;

privateList<DataBean> list;//DateBean是描述数据的类,list用于承载ListView要显示的数据

         @Override

         protectedvoid onCreate(Bundle savedInstanceState) {

                  super.onCreate(savedInstanceState);

                  setContentView(R.layout.activity_main);

                  list= new ArrayList<Data>();

                  lv= (ListView) findViewById(R.id.lv);

                  lv.setAdapter(newLvAdapter());

                 

         }

         classLvAdapter extends BaseAdapter {//以内部类的形式编写。

//如果不以内部类的形式,则需在LvAdapter的构造方法中传入context和承载数据的集合或//数组。

                  @Override

                  publicint getCount() {

                          returnlist.size();//返回数据的总条数

                  }

 

                  @Override

                  publicObject getItem(int position) {

                          returnlist.get(position);//获得当前位置上数据对象

                  }

 

                  @Override

                  publiclong getItemId(int position) {

                          System.out.println(position);

                          returnposition;//返回item的id

                  }

 

                  @Override//利用自带参数优化ListView:convertView当第一个item完全移除屏幕时//才不为null,

                                   //里面是刚刚移出的item布局的引用。

                  publicView getView(int position, View convertView, ViewGroup parent) {

                          //item中的控件

                          ImageViewiv;

                          TextViewtv;

                          if(convertView==null) {

                                   convertView= View.inflate(getApplicationContext(), R.layout.lv_item, null);

                                   iv= (ImageView) view.findViewById(R.id.iv);

                                   tv= (TextView) view.findViewById(R.id.tv);

                          }else{

                                   iv= (ImageView) convertView.findViewById(R.id.iv);

                                   tv= (TextView) convertView.findViewById(R.id.tv);

                          }

                          iv.setBackgroundResource(list.get(position).getIcon());

                          tv.setText(list.get(position).getText());

                          returnview;

                  }

         }

}

convertView原理:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值