标签:click listener method not private out undle save extends
***ListView的常用方法
实例:设置一个ListView的页面,其页面头部是图片,尾部是Button按钮
方式一:
首先在对应的布局文件中添加ListView控件,然后可在主活动文件中对该页面添加头部和尾部相应控件,如下:
public class MainActivity extends Activity {
private ListView lv;
private List<String> list;
private ArrayAdapter<String> adapter;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
lv = (ListView) findViewById(R.id.lv);
list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("item"+i);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
//添加图片控件
ImageView iv = new ImageView(MainActivity.this);
iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
iv.setImageResource(R.drawable.ic_launcher);
lv.addHeaderView(iv);//将参数中指定的控件View添加到ListView的头部
//注意:addHeaderView()方法必须在setAdapter()方法之前设置
//添加button控件
Button bt = new Button(MainActivity.this);
bt.setText("加载更多.....");
bt.setTextSize(20);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int index = list.size();
for (int i = index; i < index+10; i++) {
list.add("item"+i);
}
adapter.notifyDataSetChanged();//该方法是如果适配器的内容改变时强制调用getView来刷新每个Item的内容
}
});
lv.addFooterView(bt);//将参数中指定的控件View添加到ListView的尾部
//注意:addFooterView()方法必须在setAdapter()方法之前设置
lv.setAdapter(adapter);
lv.setEmptyView(tv); //表示TextView中国如果没有显示的数据时显示参数指定的View视图
}
}
方式二:
当然也可以通过在创建两个布局文件的形式展示同样的效果,不需要在主活动文件中直接添加控件,修改如下:
首先在lv_header.xml中添加ImaageView控件,lv_footer.xml中添加Button控件,主布局文件中添加ListView控件
public class OtherActivity extends Activity {
private ListView lv;
private List<String> list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("item"+i);
}
adapter = new ArrayAdapter<String>(OtherActivity.this,android.R.layout.simple_list_item_1, list);
View headerView = LayoutInflater.from(OtherActivity.this).inflate(R.layout.lv_header, null);//将lv_header.xml布局文件转换成View视图
ImageView iv = (ImageView) headerView.findViewById(R.id.iv);//注意:这里必须通过headerView来调用findViewById方法
iv.setImageResource(R.drawable.bg_us);
lv.addHeaderView(iv);
View footerView = LayoutInflater.from(OtherActivity.this).inflate(R.layout.lv_footer, null);
Button bt = (Button) footerView.findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int index = list.size();
for (int i = index; i < index+10; i++) {
list.add("item"+i);
}
adapter.notifyDataSetChanged();
}
});
lv.addFooterView(bt);
lv.setAdapter(adapter);
}
}
标签:click listener method not private out undle save extends
原文地址:https://www.cnblogs.com/SanguineBoy/p/9748326.html