接触android不久,对于listView的使用摸索出两种方法,不到之处还请大家指正。
ListView作为重要的展现控件,很多场合中需要满足动态加载Item,相应点击Item事件和灵活布局三种要求。
直接继承BaseAdapter类,重写getCount(),getItem(int position), getItemId(int position), getView(int position, View convertView, ViewGroup parent)方法,如果数据发生变化或需要动态加载新的Item时,调用notifyDataSetChanged方法即可。代码示例如下:
public class UserListAdapter extends BaseAdapter{
private ArrayList
private Context mContext;
public UserListAdapter(Context context, ArrayList
mContext = context;
this.userList = userList;
}
public int getCount() {
return userList.size() ;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
UserItemView userView;
UserInfo user = userList.get(position);
if (convertView == null)
userView = new UserItemView(mContext, user);
else
userView = (UserItemView) convertView;
return userView;
}
}
如上代码中UserItemView(mContext, user)的实现方法正是本文介绍的重点。
方法一:UserItemView继承LinearLayout类(或者其它布局类如RelativeLayout等)在初始化函数中集成setOnClickListener方法以实现响应点击事件;在代码中加载新的控件和位置关系等,以实现布局,代码示例如下:
public class UserItemView extends LinearLayout{
private ImageView userImageView;
private TextView userNameView;
private UserInfo user;
private Context mContext;
private LinearLayout firstLayout;
public UserItemView(Context context, final UserInfo user) {
super(context);
mContext = context;
this.user = user;
setLayout(context);
this.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// 响应点击事件
}
});
}
private void setLayout(Context context) {
firstLayout = new LinearLayout(context);
firstLayout.setOrientation( LinearLayout.VERTICAL );
userImageView = getUserIcon(user.getId() , user.getIconId() );
firstLayout.addView(userImageView);
userNameView = new TextView(context);
userNameView.setText(user.getName() );
firstLayout.addView(userNameView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(firstLayout, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
上面一种方法是用代码的方法来实现布局,比较复杂,可复用性也较差,所以下面介绍另一种方法:
直接让UserItemView继承OnItemClickListener方法以实现响应点击事件,加入一个layout文件以实现布局。
最终代码是下面这个样子:
在项目中调用UserListAdapter:
if (userListAdapter == null) {
userListAdapter = new UserListAdapter(SlimTaskListActivity.this, mUserList);
userListView.setAdapter(userListAdapter); // userListView是与ListView绑定的控件。
userListView.setOnItemClickListener(userListAdapter); //绑定点击事件
}
taskListAdapter.notifyDataSetChanged(); //列表将根据mUserList中的内容重新更新。
下面是UserListAdapter类的具体实现:
public class UserListAdapter extends BaseAdapter implements OnItemClickListener{
private ArrayList
private LayoutInflater inflater; //用于加载layout文件
public UserListAdapter(Context context, ArrayList
this.inflater = LayoutInflater.from(context);
this.userList = userList;
}
public int getCount() {
return userList.size() ;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.user_list_item_view, null);
setTaskItemContent(convertView, mUserList.get(position));
return convertView;
}
private void setTaskItemContent(View convertView, UserInfo user) {
((TextView) convertView.findViewById(R.id.user_name)).setText( user.getName() );
}
@Override
public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) {
}
}