标签:
public class MainActivity extends Activity {
private ListView lv1;private ListView lv2;private List<String> mList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mList = new ArrayList<String>();for (int i = 0; i < 20; i++) {mList.add(new String("可点击的tv--" + i));}//**************************************************************************************************************************lv1 = (ListView) findViewById(R.id.lv1);lv1.setAdapter(new MyAdapter());lv1.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(MainActivity.this, "onItemClick,position=" + position + ",id=" + id, 0).show();}});//**************************************************************************************************************************//给ListView添加头尾lv2 = (ListView) findViewById(R.id.lv2);TextView mTextView = new TextView(this);mTextView.setText("我是【先】添加的的头部");mTextView.setBackgroundColor(0xff4EEE94);ImageView mImageView = new ImageView(this);mImageView.setImageResource(R.drawable.ic_launcher);lv2.addHeaderView(mTextView);//添加以后,listView 的position = 0 的位置对应的是此mTextViewlv2.addHeaderView(mImageView);//注意:所有添加的header和footer的id都是-1,position的值和普通item规则一致。lv2.setHeaderDividersEnabled(true);//控制头部是否显示分割线。默认为truelv2.setAdapter(new MyAdapter());//addHeaderView要放在setAdapter之前,而addFooterView放在前后都可以//最后加一个透明的控件,那么最后那条分割线【可能】就出来了,这是解决某些分割线不能正常显示的一个方法View footerView = new View(this);LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 50);footerView.setBackgroundColor(0x00112233);//透明footerView.setLayoutParams(layoutParams);lv2.addFooterView(footerView);//lv2.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(MainActivity.this, "onItemClick,position=" + position + ",id=" + id, 0).show();}});}//*************************************************************************************************************************private class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {return mList.size();}@Overridepublic Object getItem(int position) {return mList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View view, ViewGroup parent) {//简化起见,这里没有使用viewholderif (view == null) view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);//按钮Button index = (Button) view.findViewById(R.id.index);index.setText(position + "");index.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View position) {Toast.makeText(MainActivity.this, "控件的onClick," + position, 0).show();}});//可点击的TextViewTextView name = (TextView) view.findViewById(R.id.name);name.setText(mList.get(position));name.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View position) {Toast.makeText(MainActivity.this, "控件的onClick," + position, 0).show();}});return view;}}}
标签:
原文地址:http://www.cnblogs.com/baiqiantao/p/5274976.html