在Android日常开发中经常会使用到LisView。ListView是一种用来创建并管理一组垂直方向的上的View的View Group,它可以用来显示一个列表的条目。下面介绍一下关于ListView的简单使用。
第一步添加ListView布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/list_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="14sp"/> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/list_head" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:textStyle="bold" android:gravity="center" android:textSize="16sp"/> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/list_foot" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:textStyle="bold" android:gravity="center" android:textSize="16sp"/> </RelativeLayout>
package com.example.androidtest; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * ListView 适配器 * * @author syz * @date 2016-4-7 */ public class MyAdapter extends BaseAdapter { private Context context; private List<String> list; private LayoutInflater inflater; public MyAdapter(Context context, List<String> list) { this.context = context; this.list = list; } @Override public int getCount() { return list == null?0:list.size(); } @Override public Object getItem(int position) { return list == null?null:list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { HolderView holderView = null; if(convertView == null){ holderView = new HolderView(); inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.list_view_item, parent, false); holderView.tv_name = (TextView) convertView.findViewById(R.id.list_name); convertView.setTag(holderView); }else{ holderView = (HolderView) convertView.getTag(); } holderView.tv_name.setText(list.get(position)); return convertView; } class HolderView{ TextView tv_name; } }
package com.example.androidtest; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { private ListView listView; private MyAdapter adapter; private List<String> list; private View headView; private View footView; private TextView tv_head,tv_foot; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); initView(); } protected void initView(){ listView = (ListView) findViewById(R.id.list_view); headView = LayoutInflater.from(this).inflate(R.layout.list_view_head, null); tv_head = (TextView) headView.findViewById(R.id.list_head); footView = LayoutInflater.from(this).inflate(R.layout.list_view_foot, null); tv_foot = (TextView) footView.findViewById(R.id.list_foot); initData(); } protected void initData(){ tv_head.setText("This is ListView head"); tv_foot.setText("This is ListView foot"); list = getDataList(); adapter = new MyAdapter(this, list); listView.setAdapter(adapter); listView.addHeaderView(headView); listView.addFooterView(footView); listView.setOnItemClickListener(this); } protected List<String> getDataList(){ List<String> list = new ArrayList<String>(); for (int i = 0; i < 20; i++) { list.add("This is item "+i); } return list; } @Override protected void onDestroy() { super.onDestroy(); } /** * ListView ItemClick event * */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /** * postion 计数从0开始包括head和foot,所以head为0,foot未list.size()+1,即最后一个 * */ if(position == 0){ Toast.makeText(this, "You click ListView head", Toast.LENGTH_LONG).show(); }else if(position == list.size()+1){ Toast.makeText(this, "You click ListView foot", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this, "You click item "+(position-1), Toast.LENGTH_LONG).show(); } } }